Spring整合Mybatis(实例代码+详解)

时间:2019-02-11
本文章向大家介绍Spring整合Mybatis(实例代码+详解),主要包括Spring整合Mybatis(实例代码+详解)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.工程预览

 

 

2. 先准备一下MyBatis的配置文件SqlMapConfig.xml吧

 

    在Spring和MyBatis整合的时候,不会在MyBatis的配置文件里面设置数据库的连接了。和Spring整和JDBC一样,交给Spring处理。

<?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>
		<package name="com.mybatis_demo.pojo"/>
	</typeAliases>
	
	<!-- mapper映射文件位置 -->
	<mappers>
		<package name="com.mybatis_demo.mapper"/>
	</mappers>
</configuration>

 

3.准备Spring配置文件ApplicationContext.xml

 

未整合时,创建工厂、SqlSession、接口实现类时需要不停重复代码。如

		//加载核心配置文件
		Reader asFile = Resources.getResourceAsReader("sqlMapConfig.xml");
		//创建sqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(asFile);
		//创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//sqlSession帮助接口生成实现类
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    使用配置文件以后可以一次完成,不需要重复累赘的去复制代码量。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 加载数据库配置文件 -->
   	<context:property-placeholder location="classpath:db.properties" />

	<!-- 数据库连接 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	</bean>
	
	<!-- mybatis工厂sqlSessionFactory创建,不需要在到代码中每次重复的写,完事!类似于AOP思想 	-->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 注入连接池 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 核心配置文件的位置 -->
		<property name="ConfigLocation" value="classpath:sqlMapConfig.xml"></property>
	</bean>
 
    <!--	 mapper动态代理开发,sqlSession和Mapper实现类创建,不需要在到代码中每次重复的写,完事!类似于AOP思想 。
但是这种啊必须拿到确定的接口。   -->		 
	<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		注入工厂
		<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>
		注入接口
		<property name="mapperInterface" value="com.mybatis_demo.mapper.UserMapper"></property>
	</bean>
</beans>

 

4.准备数据库外接的配置文件:db.properties

 

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root

 

5.准备pojo文件、mapper文件、以及junit文件

 

pojo文件、mapper文件不再列出,点击查看工程Demo

junit文件:

package com.mybatis_demo.junit;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mybatis_demo.mapper.UserMapper;
import com.mybatis_demo.pojo.User;

public class TestDemo {
	
	@Test
	public void demo() {
		//spring老规矩,配置文件
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		UserMapper mapper = ac.getBean(UserMapper.class);
		User user = mapper.queryUserById(10);
		System.out.println(user);
	}
}

 

 

 

以上写法是为了理解配置过程!!!

但是注意ApplicationContext.xml文件中最后配置动态代理接口的时候,特意配置了单独的UserMapper的接口。假如实际操作中有几十个几百个数据库表单的接口呢?将上面的mapper动态代理改进,采用下面的基本包扫描

	<!-- 改进的动态代理,扫描包式开发 
		不需要特意注入sqlSessionFactory,会自动在applicationContext内扫描
		也不需要id,会自动在调用时弹出所有接口实现类
	-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
		<!-- 基本包 -->
		<property name="basePackage" value="com.mybatis_demo.mapper"></property>
	</bean>