MyBatis—Spring 项目

目前大部分的 Java 互联网项目,都是用 Spring MVC + Spring + MyBatis 搭建平台的。

使用 Spring IoC 可以有效的管理各类的 Java 资源,达到即插即拔的功能;通过 Spring AOP 框架,数据库事务可以委托给 Spring 管理,消除很大一部分的事务代码,配合 MyBatis 的高灵活、可配置、可优化 SQL 等特性,完全可以构建高性能的大型网站。

毫无疑问,MyBatis 和 Spring 两大框架已经成了 Java 互联网技术主流框架组合,它们经受住了大数据量和大批量请求的考验,在互联网系统中得到了广泛的应用。使用 MyBatis-Spring 使得业务层和模型层得到了更好的分离,与此同时,在 Spring 环境中使用 MyBatis 也更加简单,节省了不少代码,甚至可以不用 SqlSessionFactory、 SqlSession 等对象,因为 MyBatis-Spring 为我们封装了它们。

摘自:《Java EE 互联网轻量级框架整合开发》

第一步:创建测试工程

第一步,首先在 IDEA 中新建一个名为【MybatisAndSpring】的 WebProject 工程:

然后在【src】中创建 4 个空包:

  • cn.wmyskxz.dao(放置 DAO 数据交互层处理类)
  • cn.wmyskxz.mapper(放置 Mapper 代理接口)
  • cn.wmyskxz.pojo(放置 Java 实体类)
  • cn.wmyskxz.test(放置测试类)

接着新建源文件夹【config】,用于放置各种资源配置文件:

  • 在【config / mybatis】下创建一个空的名为 “SqlMapConfig.xml” 的 MyBatis 全局配置文件
  • 在【config / spring】下创建一个空的名为 “applicationContext.xml” 的 Spring 资源配置文件
  • 在【config / sqlmap】下创建一个空的名为 “UserMapper.xml” 的 Mapper 映射文件。
  • 在【config】下创建两个 properties 属性文件,分别为 “db.properties” 和 “log4j.properties”,用于数据库连接和日志系统参数设置。

再在【web】文件夹下新建一个【WEB-INF】默认安全文件夹,并在其下创建一个【classes】和【lib】,并将项目的输出位置,改在【classes】下:

工程的完整初始结构如下:

第二步:引入依赖 jar 包

第二步,就是要准备项目的依赖 jar 包:

在【WEB-INF】文件夹下的【lib】文件夹中放置上面列举的 jar 包,然后添加依赖。

第三步:编写 Spring 配置文件

第三步,编写 Spring 的配置文件:

  • 加载数据库连接文件 “db.properties” 中的数据,建立数据源
  • 配置 sqlSessionFactory 会话工厂对象
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties"/> <!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载 MyBatis 的配置文件 -->
<property name="configLocation" value="mybatis/SqlMapConfig.xml"/>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
  • 头部的信息就是声明 xml 文档配置标签的规则的限制与规范。
  • “context:property-placeholder” 配置是用于读取工程中的静态属性文件,然后在其他配置中使用时,就可以采用 “${属性名}” 的方式获取该属性文件中的配置参数值。
  • 配置了一个名为 “dataSrouce” 的 bean 的信息,实际上是连接数据库的数据源。
  • 设置 sqlSessionFactory 的 bean 实现类为 MyBatis 与 Spring 整合 jar 包中的 SqlSessionFactoryBean 类,在其中只需要注入两个参数:一个是 MyBatis 的全局配置文件,一个是上面配置的数据源 bean

第四步:编写 MyBatis 配置文件

第四步,在【mybatis】包下编写 MyBatis 的全局配置文件 SqlMapConfig.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> <!-- settings -->
<settings>
<!-- 打开延迟加载的开关 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 将积极加载改为消极加载(即按需加载) -->
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 打开全局缓存开关(二级缓存)默认值就是 true -->
<setting name="cacheEnabled" value="true"/>
</settings> <!-- 别名定义 -->
<typeAliases>
<package name="cn.wmyskxz.pojo"/>
</typeAliases> <!-- 加载映射文件 -->
<mappers>
<!-- 通过 resource 方法一次加载一个映射文件 -->
<mapper resource="sqlmap/UserMapper.xml"/>
<!-- 批量加载mapper -->
<package name="cn.wmyskxz.mapper"/>
</mappers>
</configuration>

在该配置文件中:

  • 通过 settings 配置了一些延迟加载和缓存的开关信息
  • typeAliases 中设置了一个 package 的别名扫描路径,在该路径下的 Java 实体类都可以拥有一个别名(即首字母小写的类名)
  • 在 mappers 配置中,使用 mapper 标签配置了即将要加载的 Mapper 映射文件的资源路径,当然也可以使用 package 标签,配置 mapper 代理接口所在的包名,以批量加载 mapper 代理对象。
  • 注意: 有了 Spring 托管数据源,在 MyBatis 配置文件中仅仅需要关注性能化配置。

第五步:编写 Mapper 以及其他配置文件

第五步,编写 Mapper 映射文件,这里依然定义 Mapper 映射文件的名字为 “UserMapper.xml” (与 SqlMapConfig.xml 中配置一致),为了测试效果,只配置了一个查询类 SQL 映射:

<?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">
<mapper namespace="test">
<select id="findUserById" parameterType="_int" resultType="user">
SELECT * FROM USER WHERE id = #{id}
</select>
</mapper>

在该配置中,输出参数的映射为 “user” ,这是因为之前在 SqlMapConfig.xml 中配置了 “cn.wmyskxz.pojo” 包下的实体类使用别名(即首字母小写的类名),所以这里只需在 “cn.wmyskxz.pojo” 包下,创建 “finduserById” 对应的 Java 实体类 User:

package cn.wmyskxz.pojo;

import java.io.Serializable;

public class User implements Serializable {
private int id;
private String username; /* getter and setter */
}
  • 实现 Serializable 接口是为之后使用 Mapper 动态代理做准备,这里没有使用动态代理。

在数据库资源 “db.properties” 中配置了数据库的连接信息,以 “key=value” 的形式配置,String 正是使用 “${}” 获取其中 key 对应的 value 配置的:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root

另外日志配置和之前的配置一样,我就直接黏贴了:

# Global logging configuration
# 在开发环境下日志级别要设置成 DEBUG ,生产环境设为 INFO 或 ERROR
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

第六步:编写 DAO 层

第六步,进行数据库交互(Data Access Object)层的编写。

由于该项目只对 User 用户查询,所以 DAO 层就只有一个类,在 “cn.wmyskxz” 包下创建 DAO 层的 interface 接口,其中定义了 findUserById 方法,参数为用户的 id 值(int 类型):

package cn.wmyskxz.dao;

import cn.wmyskxz.pojo.User;

public interface UserDAO {

	// 根据 id 查询用户信息
public User findUserById(int id) throws Exception;
}

然后在同一个包下创建 UserDAO 接口的实现类 UserDAOImpl:

package cn.wmyskxz.dao;

import cn.wmyskxz.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport; public class UserDAOImpl extends SqlSessionDaoSupport implements UserDAO { @Override
public User findUserById(int id) throws Exception {
// 继承 SqlSessionDaoSupport 类,通过 this.getSqlSession() 得到 sqlSession
SqlSession sqlSession = this.getSqlSession();
User user = sqlSession.selectOne("test.findUserById", id);
return user;
}
}
  • 有几点解释:
  • UserDAOImpl 不仅实现了 UserDAO 接口,而且继承了 SqlSessionDaoSupport 类。
  • SqlSessionDaoSupport 类是 MyBatis 与 Spring 整合的 jar 包中提供的,在该类中已经包含了 sqlSessionFactory 对象作为其成员变量,而且对外提供 get 和 set 方法,方便 Spring 从外部注入 sqlSessionFactory 对象。
  • UserDAOImpl 类要成功获取 sqlSessionFactory 对象,还需要在 Spring 配置文件 applicationContext.xml 中添加 userDAO 的 bean 配置,将其中定义的 sqlSessionFactory 对象当做参数注入进去,这样 UserDAOImpl 继承 SqlSessionDaoSupport 类才会起到作用:
<!-- 原始 DAO 接口 -->
<bean id="userDAO" class="cn.wmyskxz.dao.UserDAOImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
  • 注意: DAO 实现类继承了 SqlSessionDaoSupport 父类后,就无须自己定义获取 SqlSession 会话实例类方法了,该父类会默认加载数据源信息并提供获取 SqlSession 类的方法。

第七步:编写 Service 测试类

在 “cn.wmyskxz.test” 包下创建【UserServiceTest】测试类:

package cn.wmyskxz.test;

import cn.wmyskxz.dao.UserDAO;
import cn.wmyskxz.pojo.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserServiceTest { private ApplicationContext applicationContext; // 在执行测试方法之前首先获取 Spring 配置文件对象
// 注解@Before在执行本类所有测试方法之前先调用这个方法
@Before
public void setup() throws Exception {
applicationContext = new
ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
} @Test
public void testFindUserById() throws Exception {
// 通过配置资源对象获取 userDAO 对象
UserDAO userDAO = (UserDAO) applicationContext.getBean("userDAO");
// 调用 UserDAO 的方法
User user = userDAO.findUserById(1);
// 输出用户信息
System.out.println(user.getId() + ":" + user.getUsername());
}
}

运行测试方法,输出结果如下:


动态代理 + 注解实现

上面的实例程序并没有使用 Mapper 动态代理和注解来完成,下面我们就来试试如何用动态代理和注解:

第一步:编写 UserQueryMapper

在【mapper】下新建一个【UserQueryMapper】代理接口,并使用注解:

package cn.wmyskxz.mapper;

import cn.wmyskxz.pojo.User;
import org.apache.ibatis.annotations.Select; public interface UserQueryMapper { @Select("SELECT * FROM USER WHERE id = #{id}")
public User findUserById(int id) throws Exception;
}
  • 注意: 在默认情况下,该 bean 的名字为 userQueryMapper(即首字母小写)

现在有了代理类,我们需要通知 Spring 在这里来扫描到该类,Mapper 扫描配置对象需要用专门的扫描器:

<!-- Mapper 扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描 cn.wmyskxz.mapper 包下的组件 -->
<property name="basePackage" value="cn.wmyskxz.mapper"/>
</bean>

第二步:编写测试类

这一次我们获取的不再是 userDAO 对象,而是定义的 Mapper 代理对象 userQueryMapper:

package cn.wmyskxz.test;

import cn.wmyskxz.mapper.UserQueryMapper;
import cn.wmyskxz.pojo.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserServiceTest { private ApplicationContext applicationContext; // 在执行测试方法之前首先获取 Spring 配置文件对象
// 注解@Before在执行本类所有测试方法之前先调用这个方法
@Before
public void setup() throws Exception {
applicationContext = new
ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
} @Test
public void testFindUserById() throws Exception {
// 通过配置资源对象获取 userDAO 对象
UserQueryMapper userQueryMapper = (UserQueryMapper) applicationContext.getBean("userQueryMapper");
// 调用 UserDAO 的方法
User user = userQueryMapper.findUserById(1);
// 输出用户信息
System.out.println(user.getId() + ":" + user.getUsername());
}
}

运行测试方法,得到正确结果:

可以看到,查询结果和之前非 Mapper 代理的查询结果一样。

  • 原理: 在 applicationContext.xml 配置文件中配置的 mapper 批量扫描器类,会从 mapper 包中扫描出 Mapper 接口,自动创建代理对象并且在 Spring 容器中注入。自动扫描出来的 Mapper 的 bean 的 id 为 mapper 类名(首字母小写),所以这里获取的就是名为 “userQueryMapper” 的 mapper 代理对象。

参考资料:

  • 《Java EE 互联网轻量级框架整合开发》
  • 《Spring MVC + MyBatis开发从入门到项目实战》
  • 全能的百度和万能的大脑

欢迎转载,转载请注明出处!

简书ID:@我没有三颗心脏

github:wmyskxz

欢迎关注公众微信号:wmyskxz_javaweb

分享自己的Java Web学习之路以及各种Java学习资料

MyBatis 与 Spring 整合的更多相关文章

  1. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

  2. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

  3. Mybatis+struts2+spring整合

    把student项目改造成ssm  struts2 +mybatis+spring 1,先添加spring支持:类库三个,applicationContext.xml写在webinf下四个命名空间,监 ...

  4. mybatis与spring整合(基于配置文件)

    本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池. 1.编写数据访问接口(UserDa ...

  5. mybatis与spring整合时读取properties问题的解决

    在学习mybatis与spring整合是,想从外部引用一个db.properties数据库配置文件,在配置文件中使用占位符进行引用,如下: <context:property-placehold ...

  6. Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6964162.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(八)--My ...

  7. Mybatis第五篇【Mybatis与Spring整合】

    Mybatis与Spring整合 既然我们已经学了Mybatis的基本开发了,接下来就是Mybatis与Spring的整合了! 以下使用的是Oracle数据库来进行测试 导入jar包 aopallia ...

  8. Mybatis(六) Spring整合mybatis

    心莫浮躁~踏踏实实走,一步一个脚印,就算不学习,玩,能干嘛呢?人生就是那样,要找点有意思,打发时间的事情来做,而钻研技术,动脑动手的过程,还是比其他工作更有意思些~ so,努力啥的都是强迫自己做自以为 ...

  9. MyBatis学习(三)---MyBatis和Spring整合

    想要了解MyBatis基础的朋友可以通过传送门: MyBatis学习(一)---配置文件,Mapper接口和动态SQL http://www.cnblogs.com/ghq120/p/8322302. ...

随机推荐

  1. pat 喊山

    L3-008. 喊山 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 喊山,是人双手围在嘴边成喇叭状,对着远方高山发出" ...

  2. [Chrome 浏览器快捷键]——“你是键盘党吗?”

    标签页和窗口快捷键 操作 快捷键 打开新窗口 Ctrl + n 在隐身模式下打开新窗口 Ctrl + Shift + n 打开新的标签页,并跳转到该标签页 Ctrl + t 重新打开最后关闭的标签页, ...

  3. location和location.href跳转url的区别

    使用 location = url  跳转,如果本地之前已经载入过该页面并有缓存,那么会直接读取本地的缓存,缓存机制是由本地浏览器设置决定的.状态码为:  200 OK (from cache) . ...

  4. Java基础学习笔记五 Java基础语法之面向对象

    面向对象 理解什么是面向过程.面向对象 面向过程与面向对象都是我们编程中,编写程序的一种思维方式.面向过程的程序设计方式,是遇到一件事时,思考“我该怎么做”,然后一步步实现的过程.例如:公司打扫卫生( ...

  5. 2017-2018-1 20155215 第五周 mybash的实现

    题目要求 使用fork,exec,wait实现mybash 写出伪代码,产品代码和测试代码 发表知识理解,实现过程和问题解决的博客(包含代码托管链接) 学习fork,exec,wait fork ma ...

  6. Beta冲刺第六天

    一.昨天的困难 没有困难. 二.今天进度 1.林洋洋:更新申请ip为域名,去除druid数据源统计 2.黄腾达:协作详情中添加成员对话框优化 3.张合胜:修复侧栏菜单mini状态下不能显示问题 三.明 ...

  7. 20162330 实验一 《Java开发环境的熟悉》 实验报告

    2016-2017-2 实验报告目录: 1 2 3 4 5 20162330 实验一 <Java开发环境的熟悉> 实验报告 课程名称:<程序设计与数据结构> 学生班级:1623 ...

  8. 展示博客(Beta版本)

    团队:xjbz 1. 团队成员博客,源码仓库地址. coding:https://git.coding.net/z404395979/xjbz.git 钟平辉(071):http://www.cnbl ...

  9. TOTP算法 基于时间的一次性密码

    /** Copyright (c) 2011 IETF Trust and the persons identified as authors of the code. All rights rese ...

  10. 第一章 IDEA的使用

    第一章   IDEA的使用 1.为什么要使用idea 最智能的IDE IDEA相对于eclipse来说最大的优点就是它比eclipse聪明.聪明到什么程度呢?我们先来看几个简单的例子. A.智能提示重 ...