MyBatis-Spring is a submodule of the MyBatis framework, which provides seamless integration with the popular dependency injection framework, Spring.

1. If you are using the Maven build tool, you can configure Mybatis' spring dependency as follows:

<properties>
<spring.version>3.2.3.RELEASE</spring.version>
</properties> <dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
</dependencies>

2. The JavaBean:

package com.huey.hello.mybatis.beans;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@NoArgsConstructor
@AllArgsConstructor
public class Department implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L; private int did;
private String deptName;
private String address;
private String tel;
}

3. The mapper interface:

package com.huey.hello.mybatis.mapper;

import com.huey.hello.mybatis.beans.Department;

public interface DepartmentMapper {

    public int insertDepartment(Department department);

    public Department getDepartmentById(int did);

}

4. Configure mapped statements in Mapper XML files:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--注意:此处的命名空间是 DepartmentMapper 的全限定类名-->
<mapper namespace="com.huey.hello.mybatis.mapper.DepartmentMapper"> <cache size="512"></cache> <!-- ResultMaps 被用来将 SQL SELECT 语句的结果集映射到 JavaBean 的属性中 -->
<resultMap type="Department" id="deptMap">
<!-- 映射主键 -->
<id property="did" column="did" />
<!-- 映射普通字段 -->
<result property="deptName" column="dept_name"/>
<result property="address" column="address"/>
<result property="tel" column="tel"/>
</resultMap> <!-- 添加部门记录 -->
<!-- id 名称需要与 DepartmentMapper 中的方法签名一致 -->
<!-- Department 这一别名在 mybatis-config.xml 中配置 -->
<insert id="insertDepartment" parameterType="Department" useGeneratedKeys="true" keyProperty="did">
insert into department(dept_name, address, tel)
values(#{deptName}, #{address}, #{tel})
</insert> <!-- 根据 ID 查询部门记录 -->
<select id="getDepartmentById" parameterType="int" resultMap="deptMap" useCache="true">
select * from department where did=#{did}
</select> </mapper>

5. Configure the mybatis-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> <!-- 设置别名 -->
<typeAliases>
<typeAlias type="com.huey.hello.mybatis.beans.Department" alias="Department" />
<typeAlias type="com.huey.hello.mybatis.beans.Employee" alias="Employee" />
</typeAliases> <!-- mapper 对应的 xml 配置文件 -->
<mappers>
<package name="com.huey.hello.mybatis.mapper"/>
</mappers> </configuration>

6. jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis
jdbc.username=root
jdbc.password=root

7. Configure  the applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 启动@AspectJ支持 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 自动扫描指定包及其子包下的所有Bean类 -->
<context:component-scan base-package="com.huey.hello.mybatis" />
<!-- 将配置值具体化到一个属性文件中,并且使用属性文件的key名作为占位符 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <!-- 配置 SqlSessionFactory 交由 Spring 管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean> <!-- 配置 SqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean> <!-- 扫描并注册包中的映射器 Mapper 接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.huey.hello.mybatis.mapper" />
</bean> <!-- 将事务交由 Spring 管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 启用 Spring @Transactional 注解 -->
<tx:annotation-driven /> </beans>

8. The business logic

package com.huey.hello.mybatis.serv;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.huey.hello.mybatis.beans.Department;
import com.huey.hello.mybatis.mapper.DepartmentMapper; @Service("deptService")
@Transactional
public class DepartmentService { @Resource
SqlSession sqlSession; public int createDepartment(Department department) {
int result = 0;
DepartmentMapper deptMapper = sqlSession.getMapper(DepartmentMapper.class);
result = deptMapper.insertDepartment(department);
return result;
} public Department getDepartmentById(int did) {
Department department = null;
DepartmentMapper deptMapper = sqlSession.getMapper(DepartmentMapper.class);
department = deptMapper.getDepartmentById(did);
return department;
} }

9. AbstractJUnit4SpringContextTests

package com.huey.hello.spring;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; @ContextConfiguration(locations={"classpath*:applicationContext*.xml"})
public class SpringTest extends AbstractJUnit4SpringContextTests { }

10. JUnit Test:

package com.huey.hello.mybatis.serv;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import com.huey.hello.mybatis.beans.Department;
import com.huey.hello.spring.SpringTest; public class DepartmentServiceTest extends SpringTest { @Autowired
DepartmentService deptService; @Test
public void testCreateDepartment() {
Department department = new Department(0, "组织部", "XX 路 ZZ 号", "011-99999");
deptService.createDepartment(department);
System.out.println(department);
} @Test
public void testGetDepartmentById() throws Exception {
int did = 1001;
Department department = deptService.getDepartmentById(did);
if (department != null) {
System.out.println(department);
}
} }

MyBatis(3.2.3) - Integration with Spring的更多相关文章

  1. Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

    原文链接:Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials Sentinel Getting Sta ...

  2. mybatis实战教程(mybatis in action)之六:与Spring MVC 的集成

    前面几篇文章已经讲到了mybatis与spring 的集成.但这个时候,所有的工程还不是web工程,虽然我一直是创建的web 工程.今天将直接用mybatis与Spring mvc 的方式集成起来,源 ...

  3. 搭建Spring + SpringMVC + Mybatis框架之二(整合Spring和Mybatis)

    整合Spring和Mybatis 首先给出完整的项目目录: (1)引入项目需要的jar包 使用http://maven.apache.org作为中央仓库即可. Spring核心包,mybatis核心包 ...

  4. OpenCMS integration with Spring MVC--reference

    ref from:http://blogs.indrajitpingale.com/?p=8 http://blog.shinetech.com/2013/04/09/integrating-spri ...

  5. SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis

    一:DAO实体编码 1:首先,在src目录下,新建org.myseckill.entity包,用于存放实体类: 2:实体类设计 根据前面创建的数据库表以及映射关系,创建实体类. 表一:秒杀商品表 对应 ...

  6. mybatis第二天_拓展——与spring整合以及逆向工程

    一.整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得sqlsession对象. 3.Mapper代 ...

  7. SSM框架整合的详细过程(含每一步的分析及代码)。实质上是SpringMVC与mybatis的整合,应为spring与SpringMVC不需要整合。

    为了更好的学习 springmvc和mybatis整合开发的方法,需要将springmvc和mybatis进行整合. 整合目标:控制层采用springmvc.持久层使用mybatis实现. 1.1 需 ...

  8. mybatis 热部署xml文件(spring boot和springmvc两种方式)

    参考:http://thinkgem.iteye.com/blog/2304557 步骤:1.创建两个java类 (1)MapperRefresh.java   :用于刷新mapper (2)SqlS ...

  9. mybatis学习(十一)——springmvc++spring+mybatis整合

    做任何一个项目都以一个需求,这里先定义一下需求:利用三大框架查询酒店列表. 一.搭建开发环境 1.创建一个web项目 我这里用的是 jdk1.8+tomact7.0 2.创建hotel表 CREATE ...

随机推荐

  1. Android系统中Parcelable和Serializable的区别

    转载:https://greenrobot.me/devpost/android-parcelable-serializable/ 进行Android开发的时候,我们都知道不能将对象的引用传给Acti ...

  2. 创建类模式(一):工厂方法(Factory Method)

    定义 此模式的核心精神是封装类中不变的部分,提取其中个性化善变的部分为独立类,通过依赖注入以达到解耦.复用和方便后期维护拓展的目的. 定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中.核心 ...

  3. 用 JavaScript 修改样式元素

    利用 <style> 元素,我们可以在网页中嵌入样式表.如果需要动态增加 <style> 元素,似乎可以用如下的 JavaScript 代码: var style = docu ...

  4. java实现简单的素数判断

    素数的这个问题由来已久,大学刚接触语言的时候遇到过找素数的问题,找工作笔试的时候也遇到过素数的问题,今天就特地写这篇博文,缅怀一下. 一.什么是素数? 除了1和它本身以外不再有其他的除数整除. 二.判 ...

  5. python的一些总结5

    上面4都是水的 恩每篇都一点知识点 用来写给不耐烦的人看..哈哈这篇 争取不水. 上面4篇如果 掌握 基本上是 80%常用的代码了. 1.下面讲一下 比较常用的代码: macro(jinja 上的功能 ...

  6. Windbg命令

    (1)!runaway命令显示每个线程消费的时间 参考:http://blog.csdn.net/hgy413/article/details/7564252 (2)!wow64exts.sw 关闭6 ...

  7. 设计模式奠基石——UML关系转化为代码

    1.继承关系(泛化关系) [说明]:继承关系是子类(派生类)继承父类(基类),或者子接口继承父接口的关系.即子类对象"is a" 父类对象,比方鸟是动物. [UML图]: 图解:A ...

  8. C# 循环获取目录

    #region 获取目录 /// <summary> /// 获取指定文件夹下所有子目录及文件 /// </summary> /// <param name=" ...

  9. mahout算法源码分析之Itembased Collaborative Filtering(三)RowSimilarityJob验证

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 本篇分析上篇的分析是否正确,主要是编写上篇输出文件的读取以及添加log信息打印相关变量. 首先,编写下面 ...

  10. 【剑指offer】八皇后问题

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/26614999 剑指offer上解决八皇后问题,没实用传统的递归或非递归回溯法,而是用了非常 ...