Spring+MyBatis整合过程

步骤:

  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

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

配置数据源

1 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
2         <property name="driverClassName" value="${driverClassName}"></property>
3         <property name="url" value="${url}"></property>
4         <property name="username" value="${jdbc.username}"></property>
5         <property name="password" value="${jdbc.password}"></property>
6     </bean>
View Code

配置SqlSessionFactoryBean

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

配置MapperScannerConfigurer

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

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

 1 <?xml version="1.0" encoding="UTF-8" ?>  
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5     <!-- 将底层日志打印 -->
 6     <settings>
 7         <setting name="logImpl" value="STDOUT_LOGGING" />
 8     </settings>
 9     <!-- 分页 -->
10     <plugins>
11         <plugin interceptor="com.github.pagehelper.PageHelper"></plugin>
12     </plugins>
13     <!-- 指定SQL定义文件 -->
14     <mappers>
15         <mapper class="com.xdl.mapper.DeptMapper" />
16     </mappers>
17 </configuration> 
View Code

写实体类

 1 package com.xdl.entity;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Dept implements Serializable {
 6     /**
 7      * 
 8      */
 9     private static final long serialVersionUID = 1L;
10     private Integer deptno;// 类型和名称与表保持一致
11     private String dname;
12     private String loc;
13 
14     public Dept(String dname, String loc) {
15         super();
16         this.dname = dname;
17         this.loc = loc;
18     }
19 
20     public Dept() {
21         super();
22     }
23 
24     public Dept(Integer deptno, String dname, String loc) {
25         super();
26         this.deptno = deptno;
27         this.dname = dname;
28         this.loc = loc;
29     }
30 
31     public Integer getDeptno() {
32         return deptno;
33     }
34 
35     public void setDeptno(Integer deptno) {
36         this.deptno = deptno;
37     }
38 
39     public String getDname() {
40         return dname;
41     }
42 
43     public void setDname(String dname) {
44         this.dname = dname;
45     }
46 
47     public String getLoc() {
48         return loc;
49     }
50 
51     public void setLoc(String loc) {
52         this.loc = loc;
53     }
54 
55 }
View Code

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

 1 package com.xdl.mapper;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.annotations.Insert;
 6 import org.apache.ibatis.annotations.Param;
 7 import org.apache.ibatis.annotations.Select;
 8 import org.apache.ibatis.annotations.Update;
 9 
10 import com.xdl.entity.Dept;
11 
12 public interface DeptMapper {
13     // 查询所有
14     @Select("select * from dept")
15     List<Dept> findAll();
16 
17     // 根据id查询单值
18     @Select("select * from dept where deptno = #{no}")
19     Dept findById(int id);
20 
21     // 根据id修改名字
22     @Update("update dept set dname = #{name} where deptno = #{no}")
23     int updateById(@Param("no") int id, @Param("name") String name);
24 
25     // 根据id修改名字和地址
26     @Update("update dept set dname = #{dname},loc = #{loc} where deptno = #{deptno}")
27     int update(Dept dept);
28 
29     // 插入名字和地址,序列自增长
30     @Insert("insert into dept (deptno,dname,loc) values (dept_seq.nextval,#{dname},#{loc})")
31     int insert(Dept dept);
32 }
View Code

最后使用junit测试

 1 package com.xdl.test;
 2 
 3 import java.util.List;
 4 
 5 import org.junit.Test;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 
 9 import com.github.pagehelper.Page;
10 import com.github.pagehelper.PageHelper;
11 import com.xdl.entity.Dept;
12 import com.xdl.mapper.DeptMapper;
13 
14 public class TestDept {
15     ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
16     DeptMapper deptDao = ioc.getBean("deptMapper", DeptMapper.class);
17     @Test
18     public void testDept() {
19         // 插入分页设置
20         Page<Dept> page = PageHelper.startPage(2, 3);
21         List<Dept> list = deptDao.findAll();
22         System.out.println("dept: " + list);
23         for (Dept dept : list) {
24             System.out.println("
" + dept.getDeptno() + ":" + dept.getDname() + ":" + dept.getLoc());
25         }
26         // getTotal 全部的
27         System.out.println("
总行数:" + page.getPageSize() + "
" + "总页数:" + page.getPageNum());
28     }
29 
30     @Test
31     public void testDeptById() {
32         Dept dept = deptDao.findById(10);
33         String str = "'查到了' + " + dept + "  +'条记录'";
34         System.out.println(str);
35         System.out.println(dept.getDname() + ":" + dept.getLoc());
36     }
37 
38     @Test
39     public void testUpdateBy() {
40         int row = deptDao.updateById(20, "wangcai");
41         String str = "'修改了' + " + row + "  +'条记录'";
42         System.out.println(str);
43     }
44 
45     @Test
46     public void testUpdate() {
47         int row = deptDao.update(new Dept(20, "wanghua", "sx"));
48         String str = "'插入了' + " + row + "  +'条记录'";
49         System.out.println(str);
50     }
51 
52     @Test
53     public void testInsert() {
54         int row = deptDao.insert(new Dept("wl", "g"));
55         System.out.println(row);
56     }
57 }
View Code

注意:

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

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

原文地址:https://www.cnblogs.com/resultset/p/9514262.html