Spring+MyBatis整合、介面實現元件兩種方法、整合流程
Spring+Mybatis
mybatis-spring.jar整合包
MapperFactoryBean元件,封裝了根據Mapper對映器介面生成實現元件的功能
結合
先把包都引入 ioc aop dao dbcp 當然在之前的Mybatis下增加的
當然還有配置檔案
還有引入 mybatis-spring.jar
在Spring配置中
<!-- 可以根據給定的Mapper介面生成實現元件 -->
<bean id="costDao" class="org.mybatis.spring.mapper.MapperFactoryBean" >
<property name="mapperInterface" value="dao.CostDao"></property>
</bean>
實現一個元件就要寫一個這個bean
那個dao.CostDao介面就是之前寫的介面。讓他根據這個介面實現元件
SqlSessionFactoryBean元件,封裝了建立SqlSession
用dbcp的連線池
就是不用MyBatis的配置檔案了。用的dbcp的連線池
即:用這個dbcp連線池的bean
<!-- 定義dbcp的DataSource -->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql:///jsd1507db"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
替代:mybatis的配置檔案
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/jsd1507db" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
這幾個關係
DataSource(dbcp)
SqlSessionFactoryBean(ssf)
注入dataSource
注入sql定義檔案
MapperFactoryBean(costDAO)
注入sqlsessionfactory
注入CostDao介面
<!-- 可以根據給定的Mapper介面生成實現元件 -->
<bean id="costDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="dao.CostDao"></property>
<!-- 指定sqlsession資源 -->
<property name="sqlSessionFactory" ref="ssf"></property>
</bean>
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入dataSource 用連線池獲取-->
<property name="dataSource" ref="dbcp"></property>
<!-- 注入SQL語句檔案 用*號代替,那麼就可以載入多個xml檔案了 -->
<property name="mapperLocations" value="classpatch:entity/*.xml"></property>
</bean>
<!-- 定義dbcp的DataSource -->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql:///jsd1507db"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
一定要注意!!!!!!!!!!!!
編寫的Mapper介面的名字要和Mapper.xml檔案中的namespace相同才可以
<mapper namespace="dao.CostDao">
要把包也寫上。
利用MapperFactoryBean
主要是配置檔案:
<!-- 可以根據給定的Mapper介面生成實現元件 -->
<bean id="costDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="dao.CostDao"></property>
<!-- 指定sqlsession資源 -->
<property name="sqlSessionFactory" ref="ssf"></property>
</bean>
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入dataSource 用連線池獲取-->
<property name="dataSource" ref="dbcp"></property>
<!-- 注入SQL語句檔案 用*號代替,那麼就可以載入多個xml檔案了 -->
<property name="mapperLocations" value="entity/CostDao.xml"></property>
</bean>
<!-- 定義dbcp的DataSource -->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql:///jsd1507db"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
測試類:
package test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.CostDao;
import entity.Cost;
public class TestCostDao {
public static void main(String[] args){
String conf ="applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
CostDao costDao = ac.getBean("costDao",CostDao.class);
List<Cost> list =costDao.findAll();
for(Cost cost:list){
System.out.println("name:"+cost.getName());
}
}
}
sql語句檔案Mapper
<?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="dao.CostDao">
<select id="findAll" resultType="entity.Cost">
select * from cost
</select>
<select id="findPage" parameterType="int" resultType="entity.Cost">
select * from cost limit #{page},5
</select>
<select id="findMap" resultType="Map">
select cost_id,name from cost
</select>
</mapper>
MapperScannerConfigurer
這個元件封裝了批量生成Mapper元件的功能
批量為每一個dao生成
如下配置
<!-- 可以根據指定路徑批量生成Dao實現 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 掃描dao下所有介面,批量生成實現 -->
<property name="basePackage" value="dao"></property>
<!-- 自動注入sqlsessionfactory -->
<property name="sqlSessionFactory" ref="ssf"></property>
</bean>
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入dataSource 用連線池獲取-->
<property name="dataSource" ref="dbcp"></property>
<!-- 注入SQL語句檔案 用*號代替,那麼就可以載入多個xml檔案了 -->
<property name="mapperLocations" value="entity/CostDao.xml"></property>
</bean>
<!-- 定義dbcp的DataSource -->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql:///jsd1507db"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
也會生成一個costDAO元件直接就可以用。測試類就是原來的測試類
<!-- 若不是想讓全部的dao裡的都實現的話就如下。有定義的註解的才會實現 .沒有就不會實現-->
<property name="annotationClass" value="dao.annotation.mybatisdao"></property>
即建立個介面的話如果有@mybatisdao 。這個是自己的寫的。就是上面value的值的最後。
Dao介面實現可以是框架欄位給生成。(就是利用如上的兩種方式)
也可以是自己編寫利用 SqlSessionTemplate元件
整合流程、總結
先匯入包:
匯入配置檔案
編寫對應資料庫表的實體類 entity.User
package entity;
import java.io.Serializable;
public class User implements Serializable{
private Integer id;
private String username;
private String pwd;
private String name;
private String gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
然後編寫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="dao.UserDao">
<select id="findByName" parameterType="String" resultType="entity.User">
select * from user where username=#{username}
</select>
</mapper>
編寫介面Mapper
package dao;
import entity.User;
public interface UserDao {
public User findByName(String name);
}
!!!進行配置:
<?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" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 定義dbcp -->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="url" value="jdbc:mysql:///jsd1507db"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入dataSource 用連線池獲取-->
<property name="dataSource" ref="dbcp"></property>
<!-- 注入SQL語句檔案 用*號代替,那麼就可以載入多個xml檔案了 -->
<property name="mapperLocations" value="sql/UserDao.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 掃描dao下所有介面,批量生成實現 -->
<property name="basePackage" value="dao"></property>
<!-- 自動注入sqlsessionfactory -->
<property name="sqlSessionFactory" ref="ssf"></property>
</bean>
</beans>
測試類
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dao.UserDao;
import entity.User;
public class TestUserDao {
public static void main(String[] args){
String conf ="applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
UserDao userDao = ac.getBean("userDao",UserDao.class);
User user = userDao.findByName("Recar");
System.out.println("pwd: "+user.getPwd());
}
}
一定要注意細節配置。還有 <mapper namespace="dao.UserDao">
的namespace一定要是包加介面名!!!