本文建立在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. Centos 搭建DNS服务器

    1:安装DNS服务 yum install bind -y 2:修改/etc/named.conf 配置文件 listen-on port 53 { any; }; listen-on-v6 port ...

  2. WPF DispatcherTimer GC回收

    static DispatcherTimer GCTimer = new DispatcherTimer(); public static void BeginGC() { GCTimer.Inter ...

  3. MySQL 5.7.27 MGR 单主/多主+ ProxySQL

    1 MySQL 5.7.27 MGR 多主环境 基础信息如下: centos 6.5/vbox 实例名 A B C IP 10.15.7.29 10.15.7.28 10.15.7.27 实例端口号 ...

  4. Oracle中 ORA-12704:字符集不匹配

    前言 在使用Union all连接时,若A集合中某列为nvarchar2或nvarchar类型,而B集合中无此列,用‘ ’ 来代替是会报字符集不匹配 1 select '中国','China',cas ...

  5. 报错:java.lang.NoClassDefFoundError: com/google/inject/Injector

    使用testng report,导入jar包:reportng.jar和velocity-dep-1.4.jar后,执行脚本,报错如下: 缺少依赖的jar包:guice-4.0.jar 导入依赖的ja ...

  6. C++ 多文件编译简述:头文件、链接性、声明与定义

    目录 Commen Sense 头文件 链接性 static 与链接性控制 extern 与外部链接性 Reference Commen Sense C++ 在编译时对每个翻译单元(Translati ...

  7. Angular5 tslint错误:The selector of the component “XXXComponent” should be used as element

    错误描述 在项目中自己封装了一个 select 组件 @Component({ selector: '[app-choosen-select]', templateUrl: './selectcomm ...

  8. flask config笔记

    #从flask这个包中导入Flask这个类 #Flask这个类是项目的核心,以后很多操作都是基于这个类的对象 #注册url.注册蓝图等都是基于这个类的对象 from flask import Flas ...

  9. 如何在linux下安装idea

    [通过官方安装包安装] 在 http://www.jetbrains.com/ 官网下载对应版本. ultimate 旗舰版 community 社区版 然后解压到本地对应目录,打开idea目录下的b ...

  10. Linux下面MariaDB 管理命令基础使用

    MariaDB 是 MySQL 的一个分,由于某些原因,使之取代了Mysql成为了 RHEL/CentOS 7 的默认数据库.针对数据库的操作我们经常做的操作就是增删查改,接下来就介绍下 MariaD ...