前面几篇博文已经把Mybatis的相关知识点进行了记录,写的不好,照着尚硅谷的教程来了一遍,还是有收获的,一些以前没太弄懂的东西,现在已经理清了,有人需要尚硅谷教程视频的可以网上搜索下载,个人感觉还是不错的。本文记录下,Spring集成Mybatis的相关知识点,项目还是在GitHub上,重新创建Module进行记录。
集成步骤
- 创建项目
- 添加对应的jar包(Maven项目添加对应的依赖,配置Pom文件)
- 创建数据库
- 编写Java代码,创建对应表的JavaBean;
- 创建mapper文件;
- 编写对应mapper文件的接口文件;
- 创建Mybatis的配置文件;
- 创建Spring上下文初始化配置文件;
- 编写测试代码,进行单元测试;
- 整合log4j日志记录功能;
- 整合Mybatis插件打印完整SQL语句的功能;
参考文献:Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)
创建项目
项目结构如图所示,代码已上传至GitHub
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:stat="http://www.alibaba.com/schema/stat"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd">
<!--属性占位文件引入,可以通过${属性名}获得属性文件中的内容 -->
<context:property-placeholder location="classpath:db.properties" />
<!--定义一个jdbc数据源,创建一个驱动管理数据源的bean -->
<!-- 配置数据源,使用的是alibaba的Druid(德鲁伊)数据源 -->
<bean name="jdbcDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="0" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" />
<!-- <property name="poolPreparedStatements" value="true" /> <property
name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />
<!-- 监控数据库 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean>
<!--创建一个sql会话工厂bean,指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="jdbcDataSource" />
<!--类型别名包,默认引入com.zhangguo.Spring61.entities下的所有类 -->
<property name="typeAliasesPackage" value="com.taowd.entry"></property>
<!--指定sql映射xml文件的路径 -->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property>
</bean>
<!--自动扫描映射接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定sql会话工厂,在上面配置过的 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 指定基础包,即自动扫描com.zhangguo.Spring61.mapping这个包以及它的子包下的所有映射接口类 -->
<property name="basePackage" value="com.taowd.dao"></property>
</bean>
<!--下面的配置暂时未使用 -->
<context:component-scan base-package="com.taowd">
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>
测试用例以及测试结果
package com.taowd.test;
import java.util.List;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.taowd.dao.BookTypeDAO;
import com.taowd.entry.BookType;
public class TestMyBatisSpring {
private static final Logger logger = LoggerFactory.getLogger(TestMyBatisSpring.class);
private ApplicationContext ctx;
@Test
public void test01() {
ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
// 获得bean
BookTypeDAO bookTypeDao = ctx.getBean(BookTypeDAO.class);
// 访问数据库
List<BookType> booktypes = bookTypeDao.getAllBookTypes();
for (BookType bookType : booktypes) {
logger.info("测试结果:[" + bookType.toString() + "]");
}
}
}
测试结果