Mybatis与Spring整合

mybatis与spring如何整合?

整合思路:
1.SqlsessionFactory对象应该放到Spring容器中作为单利存在;
2、传统dao的开发方式中,应该从spring容器中获得sqlsession对象。
3、Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象。
4、数据库的连接以及数据库连接池事务管理都交给spring容器来完成。

整合步骤:

1.创建一个java工程。

mybatis-spring工程结构图

2.导入jar包。

mybatis-spring所需jar包图

3.mybatis的配置文件sqlmapConfig.xml

mapper包扫描可以直接交给Spring管理
此处是应用于传统dao开发

<?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>
	<mappers>
		<mapper resource="mybatis/user.xml"/>
	</mappers>
</configuration>

4.编写Spring的配置文件

主要以下配置:
1.数据库连接池的配置;
2.sqlSessionFactory的配置(其中配置数据库源,SqlMapConfig路径,别名配置);
3.动态包扫描

<?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"
		destroy-method="close">
		<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>
	<!-- sqlSessionFactory配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!--配置mybatis的核心文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml" />
		<!-- 配置数据库的数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 配置别名包扫描 -->
		<property name="typeAliasesPackage" value="ctgu.mybatis.pojo" />
	</bean>
	<!-- ============传统dao开发 接口-类继承模式===================== -->
	<bean id="UserDao" class="ctgu.mybatis.dao.impl.UserDaoImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	<!-- ============动态dao开发 接口-配置文件模式==================== -->

	<!-- =============单个接口配置 ===============================================================
		相当于mybatis中sqlMapConfig中的.class扫描(dao动态代理 接口+关系映射文件) <mapper class="Mapper.mapper"/>============================================================ -->
	<!-- <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"
		abstract="true" lazy-init="true"> <property name="sqlSessionFactory" ref="sqlSessionFactory"
		/> </bean> <bean parent="baseMapper"> <property name="mapperInterface" value="ctgu.mybatis.Mapper.mapper"
		/> </bean> -->

	<!-- =============包扫描=============好像只能用一种,两个同时配置将会报错====================================================
		相当于mybatis中sqlMapConfig中的<package name="Mapper"/> basePackage:配置映射包扫描,多个包时用","或者";"分隔(Value配置) -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="ctgu.mybatis.Mappe;ctgu.ssm.mapper" />
		<!-- optional unless there are multiple session factories defined -->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
</beans>

4.1数据库连接及连接池

db.properties配置

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

4.2 sqlsessionFactory对象,配置到spring容器中

在applicationContext.xml中配置

<!-- sqlSessionFactory配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!--配置mybatis的核心文件 -->
		<property name="configLocation" value="classpath:SqlMapConfig.xml" />
		<!-- 配置数据库的数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 配置别名包扫描 -->
		<property name="typeAliasesPackage" value="ctgu.mybatis.pojo" />
	</bean>

测试:

package ctgu.ssm.Test;

import static org.junit.Assert.fail;

import java.util.Date;
import java.util.List;

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

import ctgu.ssm.mapper.UserMapper;
import ctgu.ssm.pojo.User;
import ctgu.ssm.pojo.UserExample;
import ctgu.ssm.pojo.UserExample.Criteria;
/**
 * 这个是逆向工程的生成测试类
 * testSelectByExample是用来拼装查询条件 的
 *
 * @author rong
 *
 */
public class UserMapperTest {
	private ApplicationContext applicationContext;
	@Before
	public void init(){
		this.applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	@Test
	public void testDeleteByPrimaryKey() {
		 UserMapper mapper = applicationContext.getBean(UserMapper.class);
		 mapper.deleteByPrimaryKey(40);
	}

	@Test
	public void testInsertSelective() {
		 UserMapper mapper = applicationContext.getBean(UserMapper.class);
		 User user = new User();
		 user.setUsername("大虎32");
		 user.setAddress("宜昌");
		 user.setSex("1");
//		 mapper.insert(user)  与insertSelective的区别在于,insertSelective会判断其是否为空,只会将非空数据插入
		 mapper.insertSelective(user);
	}

	@Test
	public void testSelectByExample() {
		 UserMapper mapper = applicationContext.getBean(UserMapper.class);
		 /*
		  * 用来拼装查询条件 的
		  */
		 UserExample example = new UserExample();
		 //需求:查询姓大的男性
		 Criteria criteria = example.createCriteria();
		 criteria.andUsernameLike("%大%");//注意:此时必须手动添加%%
		 criteria.andSexEqualTo("1");
		 List<User> users = mapper.selectByExample(example);
		 for (User user : users) {
			System.out.println(user);
		}
	}

	@Test
	public void testSelectByPrimaryKey() {
		 UserMapper mapper = applicationContext.getBean(UserMapper.class);
		 ctgu.ssm.pojo.User user = mapper.selectByPrimaryKey(31);
		 System.out.println(user);
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值