本文建立在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)都对应一个接口的方法。名字要与接口的方法名字一致。

最终效果:

Spring mvc 搭建Mybatis的更多相关文章

  1. 搭建Spring、Spring MVC、Mybatis和Freemarker

    搭建Spring.Spring MVC.Mybatis和Freemarker 1.pom文件 <project xmlns="http://maven.apache.org/POM/4 ...

  2. spring MVC、mybatis配置读写分离

    spring MVC.mybatis配置读写分离 1.环境: 3台数据库机器,一个master,二台slave,分别为slave1,slave2 2.要实现的目标: ①使数据写入到master ②读数 ...

  3. spring mvc与mybatis收集到博客

    mybaits-spring 官方教程 http://mybatis.github.io/spring/zh/ SpringMVC 基础教程 框架分析 http://blog.csdn.net/swi ...

  4. Spring Mvc和Mybatis的多数据库访问配置过程

    Spring Mvc 加Mybatis的多数据库访问源配置访问过程如下: 在applicationContext.xml进行配置 <?xml version="1.0" en ...

  5. Spring、Spring MVC、MyBatis

    Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Sp ...

  6. IDEA下创建Maven项目,并整合使用Spring、Spring MVC、Mybatis框架

    项目创建 本项目使用的是IDEA 2016创建. 首先电脑安装Maven,接着打开IDEA新建一个project,选择Maven,选择图中所选项,下一步. 填写好GroupId和ArtifactId, ...

  7. 转载 Spring、Spring MVC、MyBatis整合文件配置详解

    Spring.Spring MVC.MyBatis整合文件配置详解   使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...

  8. Spring MVC、MyBatis整合文件配置详解

    Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...

  9. Mybaits-从零开始-Spring、Spring MVC、MyBatis整合(未万待续)

    Spring.Spring MVC.MyBatis整合(未万待续)

随机推荐

  1. 开源之TinyPinyin

    适用于Java和Android的快速.低内存占用的汉字转拼音库. https://github.com/promeG/TinyPinyin

  2. ListView 中如何优化图片?

    图片的优化策略比较多.1.处理图片的方式:如果 ListView 中自定义的 Item 中有涉及到大量图片的,一定要对图片进行细心的处理,因为图片占的内存是ListView 项中最头疼的,处理图片的方 ...

  3. 阶段3 3.SpringMVC·_01.SpringMVC概述及入门案例_03.入门程序之需求分析

  4. MySQL 数据库架构

    1 架构 master-slave性能,qps,tps,cpu,disk,memory,network,raid卡,fashion卡, 并发量:同一时刻需要db处理 连接量: 大表:1000w行或者表 ...

  5. github创建项目,并提交本地文件

    1.如图所示,不要点选"Initialize this repository with README",不然就看不到第二幅图的提示信息了 2.根据下面提示,初始化本地文件,然后上传

  6. P1936 【水晶灯火灵】

    lalala~~(才不会告诉你这是题面呢) 这题确实有点坑,第一遍穷举超时,然后就开始了漫漫找规律之路... 终于,在经过5分钟的纠结之后,我终于发现了这个神奇的规律,那就是 Fabonacci!!! ...

  7. js多张图片合成一张图,canvas(海报图,将二维码和背景图合并) -----vue

    思路:vue中图片合并 首先准备好要合并的背景图,和请求后得到的二维码, canvas画图,将两张背景图和一张二维码用canvas画出来, 将canvas再转为img 注意canvas和图片的清晰图和 ...

  8. mysql之general log 日志

    开启 general log 将所有到达MySQL Server的SQL语句记录下来. 一般不会开启开功能,因为log的量会非常庞大.但个别情况下可能会临时的开一会儿general log以供排障使用 ...

  9. 12.AutoMapper 之Null替换(NullSubstitution)

    https://www.jianshu.com/p/3f86f237d1db Null 替换(Null Substitution) Null 替换允许当源类型成员在成员链任何位置为Null时给目标成员 ...

  10. Jade学习(六)之使用html2jade反编译

    我们可以使用html2jade将html编译成jade 首先安装html2jade npm install html2jade -g 用法一:使用html2jade将html转为jade html2j ...