您的位置:首页 > 编程语言 > Java开发

Spring与MyBatis整合

2016-06-02 00:00 267 查看
Dao对象有两种生成方法:
一.采用Mapper接口由MyBatis框架生成
- DataSource-->SqlSessionFactoryBean-->MapperScannerConfigurer
以上可以实现将MyBatis框架生成的Dao对象放入Spring容器

编程步骤:



1.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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<!-- 创建DataSource对象 -->
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="system"/>
<property name="password" value="12345"/>
<property name="url" value="jdbc:oracle:thin:@localhost :1521:xe"/>
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
</bean>

<!-- 定义SqlSessionFactoryBean,创建SqlSessionFactory对象 -->
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 数据库连接参数 -->
<property name="dataSource" ref="dbcp"/>

<!-- SQL定义文件参数 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>

<!-- 根据Mapper接口生成实现对象,默认接口名首字母小写做id属性 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定扫描Mapper接口的路径 -->
<property name="basePackage" value="com.tedu.dao"/>

<!-- 指定带标记的才算有效Mapper接口 -->
<property name="annotationClass" value="com.tedu.annotation.MyBatisDao"/>

<property name="sqlSessionFactory" ref="ssf"/>
</bean>

<!-- 下面的方法一次只能注入一个bean -->
<!-- <bean id="costDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.tedu.dao.CostDAO"/>
ref:引用另外的bean
<property name="sqlSessionFactory" ref="ssf"/>
</bean> -->
</beans>

2.编写实体类:
public class Cost implements Serializable{
private Integer cost_id;
private String name;
private Integer base_duration;
private Double base_cost;
private Double unit_cost;
private String status;
private String descr;
private Timestamp creatime;
private Timestamp startime;
private String cost_type;
...get/set方法
}

3.SQL定义文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.tedu.dao.CostDAO">

<select id="findAll" resultType="com.tedu.entity.Cost">
SELECT * FROM cost
</select>

</mapper>

4.映射器接口:
import com.tedu.annotation.MyBatisDao;

@MyBatisDao//带该自定义注解标记的才会生成实现对象
public interface CostDAO {
public List<Cost> findAll();
}

自定义注解:
package com.tedu.annotation;

/**
* 自定义注解标记@MyBatisDao
* @author Administrator
*
*/
public @interface MyBatisDao {
}

5.定义Bean(在主配置文件applicationContext中已配置)

6.测试类:
public class TestCostDAO {

@Test//测试id=costDao的组件
public void testCostDao(){
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
CostDAO dao = ac.getBean("costDAO",CostDAO.class);
List<Cost> costs = dao.findAll();
if(costs!=null){
for(Cost c:costs){
System.out.println(c.getName());
}
}
}

@Test
//测试SqlSessionFactory对象
public void testFactory(){
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//获取ssf对象
SqlSessionFactory factory = ac.getBean("ssf",SqlSessionFactory.class);
SqlSession session = factory.openSession();
System.out.println(session);
session.close();
}
}

二.程序员自己编写Dao实现类
- DataSource-->SqlSessionFactoryBean-->SqlSessionTemplate-->
给自己编写的Dao注入

//扫描
public class CostDao{
//注入//private SqlSessionFactory ssf;
private SqlSessionTemplate template;

public List<User> findAll(){
// SqlSession session = ssf.openSession();
// List list = session.selectList("findAll");
// session.close();
// return list;
return template.selectList("findAll");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: