Spring和mybatis的整合

Posted 小不点啊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring和mybatis的整合相关的知识,希望对你有一定的参考价值。

一、搭建项目开发环境

1. 新建一个maven项目SpringMybatis,项目结构如下:

                                                                               

说明:

src/main/java 存放java代码和映射文件:

         com.study.springmybatis.dao 存放mapper接口

         com.study.springmybatis.mapper 存放mapper映射文件

         com.study.springmybatis.model 存放pojo类

         com.study.springmybatis.service 存放service接口和对应的实现类

src/test/java存放测试代码

src/main/resources 存放数据库配置文件和xml配置文件

2. 在pom.xml文件引入spring和mybatis相关的依赖,这里用的是阿里的maven远程仓库http://maven.aliyun.com/nexus/content/groups/public/

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3   <modelVersion>4.0.0</modelVersion>
  4 
  5   <groupId>com.study.springmybatis</groupId>
  6   <artifactId>SpringMybatis</artifactId>
  7   <version>0.0.1-SNAPSHOT</version>
  8   <packaging>jar</packaging>
  9 
 10   <name>SpringMybatis</name>
 11   <url>http://maven.apache.org</url>
 12 
 13   <properties>
 14     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 15   </properties>
 16 
 17   <dependencies>
 18     <!-- 添加Spring相关的依赖 begin-->
 19         <dependency>
 20             <groupId>org.springframework</groupId>
 21             <artifactId>spring-core</artifactId>
 22             <version>4.3.12.RELEASE</version>
 23         </dependency>
 24         
 25         <dependency>
 26             <groupId>org.springframework</groupId>
 27             <artifactId>spring-context</artifactId>
 28             <version>4.3.12.RELEASE</version>
 29         </dependency>
 30         
 31         <dependency>
 32             <groupId>org.springframework</groupId>
 33             <artifactId>spring-tx</artifactId>
 34             <version>4.3.12.RELEASE</version>
 35         </dependency>
 36         
 37         <dependency>
 38             <groupId>org.springframework</groupId>
 39             <artifactId>spring-jdbc</artifactId>
 40             <version>4.3.12.RELEASE</version>
 41         </dependency>
 42         
 43         <dependency>
 44             <groupId>org.springframework</groupId>
 45             <artifactId>spring-test</artifactId>
 46             <version>4.3.12.RELEASE</version>
 47         </dependency>
 48         
 49         <dependency>
 50           <groupId>org.springframework</groupId>
 51           <artifactId>spring-aop</artifactId>
 52           <version>4.3.12.RELEASE</version>
 53         </dependency>
 54         
 55         <dependency>
 56           <groupId>org.springframework</groupId>
 57           <artifactId>spring-beans</artifactId>
 58           <version>4.3.12.RELEASE</version>
 59         </dependency>
 60 
 61         <dependency>
 62           <groupId>org.apache.geronimo.bundles</groupId>
 63           <artifactId>aspectjweaver</artifactId>
 64           <version>1.6.8_2</version>
 65         </dependency>
 66    <!-- 添加Spring相关的依赖 end-->
 67          
 68    <!-- 添加mybatis的核心包 begin-->
 69         <dependency>
 70             <groupId>org.mybatis</groupId>
 71             <artifactId>mybatis</artifactId>
 72             <version>3.2.8</version>
 73         </dependency>
 74    <!-- 添加mybatis的核心包 end-->
 75         
 76    <!-- 添加mybatis与Spring整合的核心包 begin -->
 77         <dependency>
 78             <groupId>org.mybatis</groupId>
 79             <artifactId>mybatis-spring</artifactId>
 80             <version>1.2.0</version>
 81         </dependency>
 82    <!-- 添加mybatis与Spring整合的核心包 end -->
 83         
 84    <!--数据库连接相关包 begin  -->
 85         <dependency>
 86           <groupId>org.wisdom-framework</groupId>
 87           <artifactId>mysql-connector-java</artifactId>
 88           <version>5.1.34_1</version>
 89         </dependency>
 90         
 91         <dependency>
 92           <groupId>commons-dbcp</groupId>
 93           <artifactId>commons-dbcp</artifactId>
 94           <version>1.4</version>
 95         </dependency>
 96         
 97         <dependency>
 98           <groupId>commons-pool</groupId>
 99           <artifactId>commons-pool</artifactId>
100           <version>1.6</version>
101         </dependency>
102             
103         <dependency>
104           <groupId>c3p0</groupId>
105           <artifactId>c3p0</artifactId>
106           <version>0.9.1.2</version>
107         </dependency>
108    <!--数据库连接相关包 end  -->
109         
110    <!-- 其他附加包 begin-->
118         <dependency>
119           <groupId>commons-logging</groupId>
120           <artifactId>commons-logging</artifactId>
121           <version>1.1.3</version>
122         </dependency>    
123         
124         <dependency>
125             <groupId>junit</groupId>
126             <artifactId>junit</artifactId>
127             <version>4.12</version>
128             <scope>test</scope>
129         </dependency>
130     <!-- 其他附加包 end-->
131             
132   </dependencies>
133 </project>

二、创建表SQL和使用mybatis逆向工程生成代码

1.建表

1 CREATE TABLE `t_user` (
2   `id` int(11) NOT NULL AUTO_INCREMENT,
3   `username` varchar(30) NOT NULL COMMENT \'用户名称\',
4   `birthday` date DEFAULT NULL COMMENT \'生日\',
5   `sex` char(2) DEFAULT NULL COMMENT \'性别\',
6   `address` varchar(256) DEFAULT NULL COMMENT \'地址\',
7   PRIMARY KEY (`id`)
8 ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT=\'用户信息表\';

2. 使用mybatis逆向工程生成代码

   mybatis逆向工程能根据数据库的表结构生成对应的mapper映射文件,接口,实体类,具体操作方法可以看我的前一篇文章:mybatis逆向工程,然后修改生成文件的名称为符合java命名规范的名称

3. 生成逆向工程后的代码目录结构如下,这里使用的是spring的注解管理bean

   生成的mapper接口UserDao.java代码

 1 package com.study.springmybatis.dao;
 2 
 3 import org.springframework.stereotype.Repository;
 4 
 5 import com.study.springmybatis.model.UserModel;
 6 
 7 /**
 8  * 用户接口
 9  * 
10  * @author lgs
11  *
12  */
13 @Repository("userDao")
14 public interface UserDao {
15     int deleteByPrimaryKey(Integer id);
16 
17     int insert(UserModel record);
18 
19     int insertSelective(UserModel record);
20 
21     UserModel selectByPrimaryKey(Integer id);
22 
23     int updateByPrimaryKeySelective(UserModel record);
24 
25     int updateByPrimaryKey(UserModel record);
26 }

生成的mapper映射文件mapper-user.xml,因为之前对生成的实体和接口做了名称修改,所以这里也要对mapper-user.xml里面的内容做对应的修改,修改后的内容如下

  1 <?xml version="1.0" encoding="UTF-8" ?>
  2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 <mapper namespace="com.study.springmybatis.dao.UserDao" >
  4 
  5     <resultMap id="BaseResultMap" type="com.study.springmybatis.model.UserModel" >
  6         <id column="id" property="id" jdbcType="INTEGER" />
  7         <result column="username" property="username" jdbcType="VARCHAR" />
  8         <result column="birthday" property="birthday" jdbcType="DATE" />
  9         <result column="sex" property="sex" jdbcType="CHAR" />
 10         <result column="address" property="address" jdbcType="VARCHAR" />
 11     </resultMap>
 12 
 13     <sql id="Base_Column_List" >
 14         id, username, birthday, sex, address
 15     </sql>
 16 
 17     <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
 18         select 
 19         <include refid="Base_Column_List" />
 20         from t_user
 21         where id = #{id,jdbcType=INTEGER}
 22     </select>
 23 
 24     <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
 25         delete from t_user
 26         where id = #{id,jdbcType=INTEGER}
 27     </delete>
 28 
 29     <insert id="insert" parameterType="com.study.springmybatis.model.UserModel" >
 30         insert into t_user (id, username, birthday, 
 31             sex, address)
 32         values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{birthday,jdbcType=DATE}, 
 33             #{sex,jdbcType=CHAR}, #{address,jdbcType=VARCHAR})
 34     </insert>
 35 
 36     <insert id="insertSelective" parameterType="com.study.springmybatis.model.UserModel" >
 37         insert into t_user
 38         <trim prefix="(" suffix=")" suffixOverrides="," >
 39             <if test="id != null" >
 40                 id,
 41             </if>
 42             <if test="username != null" >
 43                 username,
 44             </if>
 45             <if test="birthday != null" >
 46                 birthday,
 47             </if>
 48             <if test="sex != null" >
 49                 sex,
 50             </if>
 51             <if test="address != null" >
 52                 address,
 53             </if>
 54         </trim>
 55         <trim prefix="values (" suffix=")" suffixOverrides="," >
 56             <if test="id != null" >
 57                 #{id,jdbcType=INTEGER},
 58             </if>
 59             <if test="username != null" >
 60                 #{username,jdbcType=VARCHAR},
 61             </if>
 62             <if test="birthday != null" >
 63                 #{birthday,jdbcType=DATE},
 64             </if>
 65             <if test="sex != null" >
 66                 #{sex,jdbcType=CHAR},
 67             </if>
 68             <if test="address != null" >
 69                 #{address,jdbcType=VARCHAR},
 70             </if>
 71         </trim>
 72     </insert>
 73 
 74     <update id="updateByPrimaryKeySelective" parameterType="com.study.springmybatis.model.UserModel" >
 75         update t_user
 76         <set >
 77             <if test="username != null" >
 78                 username = #{username,jdbcType=VARCHAR},
 79             </if>
 80             <if test="birthday != null" >
 81                 birthday = #{birthday,jdbcType=DATE},
 82             </if>
 83             <if test="sex != null" >
 84                 sex = #{sex,jdbcType=CHAR},
 85             </if>
 86             <if test="address != null" >
 87                 address = #{address,jdbcType=VARCHAR},
 88             以上是关于Spring和mybatis的整合的主要内容,如果未能解决你的问题,请参考以下文章

基于Spring+SpringMVC+MyBatis开发书评网Spring和MyBatis的整合

请教mybatis+spring+atomikos的整合问题

spring和mybatis的整合(包含原始dao的整合方式和mapper代理整合方式)

7.Spring:整合Mybatis

spring整合mybatis时运行测试代码报错,是怎么回事?

MyBatis与Spring的整合(Eclipse版本和IDEA版本)