步骤:

  1.引入Spring+MyBatis开发包

    >spring(ioc aop dao)开发包

    >mybatis开发包,dbcp,驱动包

    >mybatis-spring.jar整合包

  2.引入Spring+MyBatis配置文件

    >applicationContext.xml

    >sqlmap-config.xml

  3.写实体类

  4.定义SQL和Mapper映射器接口

  5.在Spring中追加MyBatis整合配置

---------------------------------------------------------------------------------

在applicationContext.xml

引入外部db.properties

 <context:property-placeholder location="classpath:db.properties" />

配置数据源

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>

配置SqlSessionFactoryBean

 <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:sqlmap-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>

配置MapperScannerConfigurer

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描com.xdl.mapper下的Mapper接口创建对象 -->
<property name="basePackage" value="com.xdl.mapper"></property>
</bean>

在sqlmap-config.xml指定SQL定义文件以及打印日志和分页

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 将底层日志打印 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<!-- 分页 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper"></plugin>
</plugins>
<!-- 指定SQL定义文件 -->
<mappers>
<mapper class="com.xdl.mapper.DeptMapper" />
</mappers>
</configuration>

写实体类

 package com.xdl.entity;

 import java.io.Serializable;

 public class Dept implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer deptno;// 类型和名称与表保持一致
private String dname;
private String loc; public Dept(String dname, String loc) {
super();
this.dname = dname;
this.loc = loc;
} public Dept() {
super();
} public Dept(Integer deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
} public Integer getDeptno() {
return deptno;
} public void setDeptno(Integer deptno) {
this.deptno = deptno;
} public String getDname() {
return dname;
} public void setDname(String dname) {
this.dname = dname;
} public String getLoc() {
return loc;
} public void setLoc(String loc) {
this.loc = loc;
} }

写DeptMapper接口,里面使用标注实现增删改查(单值DQL返回对象,多值查询返回集合,DML返回int或void)

 package com.xdl.mapper;

 import java.util.List;

 import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import com.xdl.entity.Dept; public interface DeptMapper {
// 查询所有
@Select("select * from dept")
List<Dept> findAll(); // 根据id查询单值
@Select("select * from dept where deptno = #{no}")
Dept findById(int id); // 根据id修改名字
@Update("update dept set dname = #{name} where deptno = #{no}")
int updateById(@Param("no") int id, @Param("name") String name); // 根据id修改名字和地址
@Update("update dept set dname = #{dname},loc = #{loc} where deptno = #{deptno}")
int update(Dept dept); // 插入名字和地址,序列自增长
@Insert("insert into dept (deptno,dname,loc) values (dept_seq.nextval,#{dname},#{loc})")
int insert(Dept dept);
}

最后使用junit测试

 package com.xdl.test;

 import java.util.List;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xdl.entity.Dept;
import com.xdl.mapper.DeptMapper; public class TestDept {
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
DeptMapper deptDao = ioc.getBean("deptMapper", DeptMapper.class);
@Test
public void testDept() {
// 插入分页设置
Page<Dept> page = PageHelper.startPage(2, 3);
List<Dept> list = deptDao.findAll();
System.out.println("dept: " + list);
for (Dept dept : list) {
System.out.println("\n" + dept.getDeptno() + ":" + dept.getDname() + ":" + dept.getLoc());
}
// getTotal 全部的
System.out.println("\n总行数:" + page.getPageSize() + "\n" + "总页数:" + page.getPageNum());
} @Test
public void testDeptById() {
Dept dept = deptDao.findById(10);
String str = "'查到了' + " + dept + " +'条记录'";
System.out.println(str);
System.out.println(dept.getDname() + ":" + dept.getLoc());
} @Test
public void testUpdateBy() {
int row = deptDao.updateById(20, "wangcai");
String str = "'修改了' + " + row + " +'条记录'";
System.out.println(str);
} @Test
public void testUpdate() {
int row = deptDao.update(new Dept(20, "wanghua", "sx"));
String str = "'插入了' + " + row + " +'条记录'";
System.out.println(str);
} @Test
public void testInsert() {
int row = deptDao.insert(new Dept("wl", "g"));
System.out.println(row);
}
}

注意:

在配置db.properties里的账号和密码

在测试的时候回出现用户名或密码错误,这个原因是因为默认使用本地计算机用户名,为了区分系统用户名,所以在username和password前面加上xxx.

Spring+MyBatis整合过程的更多相关文章

  1. springMVC+spring+mybatis整合过程中遇到的问题

    今天在配置SSM整合的过程中遇到了几个错误,折腾了好久,具体如下 1.java.lang.IllegalArgumentException: Mapped Statements collection ...

  2. Spring+Mybatis整合过程中找不到.properties文件

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' ...

  3. 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)

    前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...

  4. Spring + mybatis整合方案总结 结合实例应用

    Spring + mybatis整合实例应用 项目结构图 (Spring3.0.2 +mybatis3.0.4) 方案一: 通过配置文件整合Spring和mybatis 应用数据库 -- --数据库 ...

  5. SpringMVC+Spring+Mybatis整合

    SpringMVC+Spring+Mybatis整合 导包 配置jdbc.properties.log4j.properties jdbc.driver=com.mysql.jdbc.Driver j ...

  6. SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。

    SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...

  7. Spring+Mybatis整合时 Failed to read candidate component class,Caused by:IllegalArgumentException

    Spring+Mybatis整合时Caused by: java.lang.IllegalArgumentException错误 org.springframework.beans.factory.B ...

  8. Springmvc+Spring+Mybatis整合开发(架构搭建)

    Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...

  9. spring 和 mybatis 整合过程 (包含分页)

    1.spring-mybatis.xml  : 配置 SqlSessionFactory 和  MapperScannerConfigurer  <bean id="sqlSessio ...

随机推荐

  1. jdk源码阅读笔记-HashMap

    文章出处:[noblogs-it技术博客网站]的博客:jdk1.8源码分析 在Java语言中使用的最多的数据结构大概右两种,第一种是数组,比如Array,ArrayList,第二种链表,比如Array ...

  2. with管理文件操作上下文

    目录 with管理文件操作上下文(掌握) with管理文件操作上下文(掌握) 之前我们使用open()方法操作文件,但是open打开文件后我们还需要手动释放文件对操作系统的占用.但是其实我们可以更方便 ...

  3. gem安装redis库时报错

    报错一: [root@Redis-1 software]# /usr/local/ruby/bin/gem install redis-3.2.2.gem ERROR: Loading command ...

  4. asp.net core系列 54 IS4用客户端凭据保护API

    一. 概述 本篇开始进入IS4实战学习,从第一个示例开始,该示例是 “使用客户端凭据保护API”,这是使用IdentityServer保护api的最基本场景.该示例涉及到三个项目包括:Identity ...

  5. 【官网翻译】性能篇(四)为电池寿命做优化——使用Battery Historian分析电源使用情况

    前言 本文翻译自“为电池寿命做优化”系列文档中的其中一篇,用于介绍如何使用Battery Historian分析电源使用情况. 中国版官网原文地址为:https://developer.android ...

  6. 《前端之路》之 JavaScript 进阶技巧之高阶函数(下)

    目录 第二章 - 03: 前端 进阶技巧之高阶函数 一.防篡改对象 1-1:Configurable 和 Writable 1-2:Enumerable 1-3:get .set 2-1:不可扩展对象 ...

  7. Java8新特性之二:方法引用

    上一节介绍了Java8新特性中的Lambda表达式,本小节继续讲解Java8的新特性之二:方法引用.方法引用其实也离不开Lambda表达式. 1.方法引用的使用场景 我们用Lambda表达式来实现匿名 ...

  8. C#处理json实战

    昨天看到技术群中发了一个查询天气的api,http://www.sojson.com/open/api/weather/json.shtml?city=南昌 点进去看,发现服务器传回来一个天气信息的j ...

  9. Visual Studio动态生成版权信息

    Visual Studio动态生成版权信息 VS2008 1.1,类文件模板:在安装目录打开CS模板文件夹(D:\Program Files (x86)\Microsoft Visual Studio ...

  10. Android拦截并获取WebView内部POST请求参数

    起因: 有些时候自家APP中嵌入的H5页面并不是自家的.但是很多时候又想在H5不知情的情况下获取H5内部请求的参数,这应该怎么做到呢? 带着这个疑问,就有了这篇博客. 实现过程: 方案一: 最开始想到 ...