sm-Spring與Mybatis的整合

Spring與Mybatis的整合,主要包括兩個方面,一是將Mybatis的會話工廠交由Spring進行創建管理,二是將mapper接口交由Spring統一管理。


接下來通過項目進行說明。

新建java工程,項目結構如下:


數據庫資源配置文件不再敘述,在之前的mybatis文章中已經多次提及,本項目從數據庫的連接至接口的開發進行講解。

本項目通過一個數據查詢案例進行講解。


1、編寫po類

package com.bs.po;
/*
 *@Author swxctx
 *@time 2017年4月25日
 *@Explain:數據表po對象
 *字段:
 */
public class Data {
	private int num;
	private String temp;
	private String co;
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public String getTemp() {
		return temp;
	}
	public void setTemp(String temp) {
		this.temp = temp;
	}
	public String getCo() {
		return co;
	}
	public void setCo(String co) {
		this.co = co;
	}
}

2、編寫mybatis配置文件SqlMapConfig.xml,與Spring結合時,配置文件其實只需要對mybatis進行一些設置即可。

<?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.bs.po"/>
	</typeAliases>
	
	<!-- 加載mapper -->
	<mappers>
		<package name="com.bs.mapper"/>
	</mappers>
	
</configuration>

3、編寫mapper接口

package com.bs.mapper;
/*
 *@Author swxctx
 *@time 2017年3月14日
 *@Explain:
 */

import com.bs.po.Data;


public interface DataMapper{
	//根據num查詢數據信息
	public Data findDataByNum(int num)throws Exception;
	
}

4、編寫mapper.xml配置文件

<?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="com.bs.mapper.DataMapper">
	
	<!-- 根據num查詢數據信息 -->
	<select id="findDataByNum" parameterType="int" resultType="com.bs.po.Data">
		select *from Data where num=#{num}
	</select>
	
</mapper>

5、編寫Spring配置文件applicationContext.xml,此配置文件主要配置數據庫連接相關信息以及mapper接口

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.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>	
	
	<!-- mapper配置 -->
	<!-- 讓spring管理sqlsessionfactory 使用mybatis-spring.jar -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		
		<!-- 數據庫連接池 -->
		<property name="dataSource" ref="dataSource" />
		
		<!-- 加載mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
	</bean>
	
	<!-- mapper配置(單個加載) -->
	<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		指定mapper接口
		<property name="mapperInterface" value="com.bs.mapper.UserMapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean> -->
	
	<!-- mapper掃描(自動掃描) -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 掃描的包名 -->
		<property name="basePackage" value="com.bs.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>

</beans>

6、在上面,配置已經完成,接下來可以測試了

public class DaoUserTest {
	private ApplicationContext applicationContext;
	
	@Before
	public void setUp() throws Exception {
		//得到spring容器
		applicationContext = new ClassPathXmlApplicationContext("config/spring/applicationContext.xml");
	}

	@Test
	public void testFindDataByNum() throws Exception{
		DataMapper dataMapper = (DataMapper)applicationContext.getBean("dataMapper");
		Data data = dataMapper.findDataByNum(1);
		System.out.println(data.getTemp());
	}
}

結果如下所示:






發佈了436 篇原創文章 · 獲贊 106 · 訪問量 57萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章