Spring mvc 搭建Mybatis

本文建立在spring mvc已经搭建起来的基础上。

首先看要引入的jar包,其中高亮的是为了mybatis新引入的。

  1. <properties>
  2.       <spring.webmvc.version>4.3.0.RELEASE</spring.webmvc.version>
  3.       <junit.version>4.8.2</junit.version>
  4.       <jedis.version>2.8.1</jedis.version>
  5.       <log4j.version>1.2.16</log4j.version>
  6.       <slf4j.version>1.7.2</slf4j.version>
  7.       <spring.data.redis.version>1.7.2.RELEASE</spring.data.redis.version>
  8.       <spring.data.mongodb.version>1.8.0.RELEASE</spring.data.mongodb.version>
  9.       <mybatis.version>3.2.0</mybatis.version>
  10.       <dbcp.version>1.2.2</dbcp.version>
  11.    </properties>
  12.  
  13.  
  14.  
  15.    <dependencies>
  16.       <dependency>
  17.          <groupId>junit</groupId>
  18.          <artifactId>junit</artifactId>
  19.          <version>${junit.version}</version>
  20.          <scope>test</scope>
  21.       </dependency>
  22.       <dependency>
  23.          <groupId>org.springframework</groupId>
  24.          <artifactId>spring-webmvc</artifactId>
  25.          <version>${spring.webmvc.version}</version>
  26.       </dependency>
  27.       <dependency>
  28.          <groupId>redis.clients</groupId>
  29.          <artifactId>jedis</artifactId>
  30.          <version>${jedis.version}</version>
  31.       </dependency>
  32.       <dependency>
  33.          <groupId>org.springframework.data</groupId>
  34.          <artifactId>spring-data-redis</artifactId>
  35.          <version>${spring.data.redis.version}</version>
  36.       </dependency>
  37.       <dependency>
  38.          <groupId>org.springframework.data</groupId>
  39.          <artifactId>spring-data-mongodb</artifactId>
  40.          <version>${spring.data.mongodb.version}</version>
  41.       </dependency>
  42.  
  43.  
  44.  
  45.  
  46.       <dependency>
  47.          <groupId>log4j</groupId>
  48.          <artifactId>log4j</artifactId>
  49.          <version>${log4j.version}</version>
  50.       </dependency>
  51.       <dependency>
  52.          <groupId>org.slf4j</groupId>
  53.          <artifactId>slf4j-api</artifactId>
  54.          <version>${slf4j.version}</version>
  55.       </dependency>
  56.       <dependency>
  57.          <groupId>org.slf4j</groupId>
  58.          <artifactId>slf4j-log4j12</artifactId>
  59.          <version>${slf4j.version}</version>
  60.       </dependency>
  61.  
  62.       <dependency>
  63.          <groupId>org.mybatis</groupId>
  64.          <artifactId>mybatis</artifactId>
  65.          <version>${mybatis.version}</version>
  66.       </dependency>
  67.  
  68.       <dependency>
  69.          <groupId>org.mybatis</groupId>
  70.          <artifactId>mybatis-spring</artifactId>
  71.          <version>1.2.2</version>
  72.       </dependency>
  73.  
  74.  
  75.       <dependency>
  76.          <groupId>mysql</groupId>
  77.          <artifactId>mysql-connector-java</artifactId>
  78.          <version>5.1.30</version>
  79.       </dependency>
  80.  
  81.       <dependency>
  82.          <groupId>commons-dbcp</groupId>
  83.          <artifactId>commons-dbcp</artifactId>
  84.          <version>${dbcp.version}</version>
  85.       </dependency>
  86.  
  87.       <dependency>
  88.          <groupId>org.springframework</groupId>
  89.          <artifactId>spring-jdbc</artifactId>
  90.          <version>${spring.webmvc.version}</version>
  91.       </dependency>
  92.  
  93.       <dependency>
  94.          <groupId>com.fasterxml.jackson.core</groupId>
  95.          <artifactId>jackson-core</artifactId>
  96.          <version>2.4.3</version>
  97.       </dependency>
  98.  
  99.       <dependency>
  100.          <groupId>com.fasterxml.jackson.core</groupId>
  101.          <artifactId>jackson-databind</artifactId>
  102.          <version>2.4.3</version>
  103.       </dependency>
  104.  
  105.    </dependencies>

 

jdbc.properties:

 

  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://192.168.1.101:3306/test
  3. username=root
  4. password=root
  5. #\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
  6. initialSize=0
  7. #\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
  8. maxActive=20
  9. #\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2
  10. maxIdle=20
  11. #\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
  12. minIdle=1
  13. #\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
  14. maxWait=60000

 

mybatis.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"
  6.     xmlns:cache="http://www.springframework.org/schema/cache"
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  8.                         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  9.                         http://www.springframework.org/schema/context
  10.                         http://www.springframework.org/schema/context/spring-context-4.2.xsd
  11.                         http://www.springframework.org/schema/mvc
  12.                         http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
  13.                         http://www.springframework.org/schema/cache
  14.                         http://www.springframework.org/schema/cache/spring-cache-4.2.xsd ">
  15.  
  16.  
  17.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  18.         destroy-method="close">
  19.         <property name="driverClassName" value="${driver}" />
  20.         <property name="url" value="${url}" />
  21.         <property name="username" value="${username}" />
  22.         <property name="password" value="${password}" />
  23.         <!-- 初始化连接大小 -->
  24.         <property name="initialSize" value="${initialSize}"></property>
  25.         <!-- 连接池最大数量 -->
  26.         <property name="maxActive" value="${maxActive}"></property>
  27.         <!-- 连接池最大空闲 -->
  28.         <property name="maxIdle" value="${maxIdle}"></property>
  29.         <!-- 连接池最小空闲 -->
  30.         <property name="minIdle" value="${minIdle}"></property>
  31.         <!-- 获取连接最大等待时间 -->
  32.         <property name="maxWait" value="${maxWait}"></property>
  33.     </bean>
  34.  
  35.     <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
  36.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  37.         <property name="dataSource" ref="dataSource" />
  38.         <!-- 自动扫描mapping.xml文件 -->
  39.         <property name="mapperLocations" value="classpath:com/zjf/**/mybatis_*.xml"></property>
  40.     </bean>
  41.  
  42.     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
  43.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  44.         <property name="basePackage" value="com.zjf" />
  45.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  46.     </bean>
  47.  
  48.     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
  49.     <bean id="transactionManager"
  50.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  51.         <property name="dataSource" ref="dataSource" />
  52.     </bean>
  53.  
  54. </beans>

java代码目录结构:

注意:这里没有dao的impl,因为Mybatis会根据mapper.xml去创建一个impl。也就是说,mapper.xml就一个一个impl。

PersonController.java:

  1. package com.zjf.spring.mybatis;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9.  
  10. import com.zjf.spring.mybatis.model.Person;
  11. import com.zjf.spring.mybatis.service.IPersonService;
  12.  
  13. @Controller
  14. @RequestMapping("person")
  15. public class PersonController {
  16.  
  17.    @Autowired
  18.    private IPersonService personService;
  19.    @RequestMapping(value="/get/{id}")
  20.    public @ResponseBody Person get(@PathVariable Integer id)
  21.    {
  22.       Person person = personService.getPersonById(id);
  23.       return person;
  24.    }
  25. }

PersonServiceImpl.java:

  1. package com.zjf.spring.mybatis.service.impl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5.  
  6. import com.zjf.spring.mybatis.dao.IPersonDao;
  7. import com.zjf.spring.mybatis.model.Person;
  8. import com.zjf.spring.mybatis.service.IPersonService;
  9.  
  10. @Service(value="personService")
  11. public class PersonServiceImpl implements IPersonService {
  12.  
  13.    @Autowired
  14.    private IPersonDao personDao;
  15.  
  16.    public Person getPersonById(Integer id) {
  17.       // TODO Auto-generated method stub
  18.       return personDao.selectByPrimaryKey(id);
  19.    }
  20.  
  21. }

IPersonDao.java

  1. package com.zjf.spring.mybatis.dao;
  2.  
  3. import com.zjf.spring.mybatis.model.Person;
  4.  
  5. public interface IPersonDao {
  6.  
  7.     int deleteByPrimaryKey(Integer id);
  8.  
  9.     int insert(Person record);
  10.  
  11.     int insertSelective(Person record);
  12.  
  13.     Person selectByPrimaryKey(Integer id);
  14.  
  15.     int updateByPrimaryKeySelective(Person record);
  16.  
  17.     int updateByPrimaryKey(Person record);
  18. }

Person.java:

  1. package com.zjf.spring.mybatis.model;
  2.  
  3. public class Person {
  4.     private Integer id;
  5.  
  6.     private String name;
  7.  
  8.     private Integer age;
  9.  
  10.     private String address;
  11.  
  12.     public Integer getId() {
  13.         return id;
  14.     }
  15.  
  16.     public void setId(Integer id) {
  17.         this.id = id;
  18.     }
  19.  
  20.     public String getName() {
  21.         return name;
  22.     }
  23.  
  24.     public void setName(String name) {
  25.         this.name = name == null ? null : name.trim();
  26.     }
  27.  
  28.     public Integer getAge() {
  29.         return age;
  30.     }
  31.  
  32.     public void setAge(Integer age) {
  33.         this.age = age;
  34.     }
  35.  
  36.     public String getAddress() {
  37.         return address;
  38.     }
  39.  
  40.     public void setAddress(String address) {
  41.         this.address = address == null ? null : address.trim();
  42.     }
  43.  
  44.  
  45. }

mybatis_PersonMapper.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.zjf.spring.mybatis.dao.IPersonDao" >
  4.   <resultMap id="BaseResultMap" type="com.zjf.spring.mybatis.model.Person" >
  5.     <id column="id" property="id" jdbcType="INTEGER" />
  6.     <result column="name" property="name" jdbcType="VARCHAR" />
  7.     <result column="age" property="age" jdbcType="INTEGER" />
  8.     <result column="address" property="address" jdbcType="VARCHAR" />
  9.   </resultMap>
  10.   <sql id="Base_Column_List" >
  11.     id, name, age, address
  12.   </sql>
  13.   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  14.     select
  15.     <include refid="Base_Column_List" />
  16.     from t_person
  17.     where id = #{id,jdbcType=INTEGER}
  18.   </select>
  19.   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
  20.     delete from t_person
  21.     where id = #{id,jdbcType=INTEGER}
  22.   </delete>
  23.   <insert id="insert" parameterType="com.zjf.spring.mybatis.model.Person" >
  24.     insert into t_person (id, name, age,
  25.       address)
  26.     values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
  27.       #{address,jdbcType=VARCHAR})
  28.   </insert>
  29.   <insert id="insertSelective" parameterType="com.zjf.spring.mybatis.model.Person" >
  30.     insert into t_person
  31.     <trim prefix="(" suffix=")" suffixOverrides="," >
  32.       <if test="id != null" >
  33.         id,
  34.       </if>
  35.       <if test="name != null" >
  36.         name,
  37.       </if>
  38.       <if test="age != null" >
  39.         age,
  40.       </if>
  41.       <if test="address != null" >
  42.         address,
  43.       </if>
  44.     </trim>
  45.     <trim prefix="values (" suffix=")" suffixOverrides="," >
  46.       <if test="id != null" >
  47.         #{id,jdbcType=INTEGER},
  48.       </if>
  49.       <if test="name != null" >
  50.         #{name,jdbcType=VARCHAR},
  51.       </if>
  52.       <if test="age != null" >
  53.         #{age,jdbcType=INTEGER},
  54.       </if>
  55.       <if test="address != null" >
  56.         #{address,jdbcType=VARCHAR},
  57.       </if>
  58.     </trim>
  59.   </insert>
  60.   <update id="updateByPrimaryKeySelective" parameterType="com.zjf.spring.mybatis.model.Person" >
  61.     update t_person
  62.     <set >
  63.       <if test="name != null" >
  64.         name = #{name,jdbcType=VARCHAR},
  65.       </if>
  66.       <if test="age != null" >
  67.         age = #{age,jdbcType=INTEGER},
  68.       </if>
  69.       <if test="address != null" >
  70.         address = #{address,jdbcType=VARCHAR},
  71.       </if>
  72.     </set>
  73.     where id = #{id,jdbcType=INTEGER}
  74.   </update>
  75.   <update id="updateByPrimaryKey" parameterType="com.zjf.spring.mybatis.model.Person" >
  76.     update t_person
  77.     set name = #{name,jdbcType=VARCHAR},
  78.       age = #{age,jdbcType=INTEGER},
  79.       address = #{address,jdbcType=VARCHAR}
  80.     where id = #{id,jdbcType=INTEGER}
  81.   </update>
  82. </mapper>

注:mybatis_PersonMapper.xml中的配置将会映射成它配置的namespace接口的实现,然后每个节点(update select insert)都对应一个接口的方法。名字要与接口的方法名字一致。

最终效果:

posted on 2017-06-14 18:04  张小贱1987  阅读(233)  评论(0编辑  收藏  举报

导航