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

spring和mybatis整合的dao两种开放方式

2016-04-29 19:48 483 查看
spring是service层框架,这里的spring是指springframework,现在的spring已经在各个层次都有自己的一套解决方案,orm层有springdata,web有springmvc业务层有springframework,大家有时间可以自己研究下,这里只探讨spring和mybatis整合的dao开发

其实spring和mybatis整合时,dao的开发是围绕mybatis的,由于mybatis提供了mapper代理机制,所以除了原始开发另一种就是mapper代理开发

spring和mybatis整合的各自的jar包当然是少不了的,所以我们需要导入:

唯一需要注意的就是需要导入一个整合包:mybatis-spring-1.2.5.jar

如果要使用c3p0数据库连接池,需要导入相关jar包,如果使用二级缓存也需要导入相关jar包

逆向工程的的jar包也是必须的,这里贴出所有jar包:



1:利用数据库,逆向生成mapper.及po类,以及xml文件,就是dao



生成代码:



逆向工程可以参考,这篇博文:http://blog.csdn.net/do_bset_yourself/article/details/51276517

当然也可以参考官方文档:http://www.mybatis.org/generator/configreference/xmlconfig.html

,也可以自己手写dao代码,

2:整合配置文件,需要注意的是,数据库连接是配置在spring配置文件中的,mybatis的配置文件可以要也不可以不要,不过建议留下,为了程序的可读性着想,以及mapper映射文件还是建议在mybatis中配置,以及二级缓存,配置,逆向工程配置,db配置如下:

spring配置:

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

<!--                   配置数据库连接池 -->
<!--     加载jdbc配置 -->
<context:property-placeholder location="classpath:jdbcInfo.properties"/>
<bean id="dataSourse" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${username}"/>
<property name="password" value="${password}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="driverClass" value="${driver}"/>
<property name="acquireIncrement" value="5"/>
<property name="maxPoolSize" value="50"/>
<property name="initialPoolSize" value="5"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourse" />
<property name="configLocation" value="classpath:mybatis/configuration.xml"></property>
</bean>
<!-- 配置业务层的注入 -->
<bean id="studentService" class="com.leige.service.impl.StudentServiceImpl">
<!--
原始开发注入dao
<property name="studentDao" ref="studentDao"/>   -->
<!-- mapper代理开发注入mapper代理 -->
<property name="mapper" ref="studentMapper"></property>
</bean>
<!-- dao层开发有两种方式,一种是原始开发,
原始开发需要到实现类继承SqlSessionDaoSupport类
注入sqlsessionfactory类
一种是mapper代理开发 -->
<!-- <bean id="studentDao" class="com.leige.dao.impl.StudentDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean> -->
<!-- 另一种就是mapper代理开发 ,需要指定mapperInterface接口类型和sqlsessionFactory-->
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" scope="singleton">
<property name="mapperInterface" value="com.leige.dao.mapper.StudentMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>


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>

<!--  首先在全局配置中配置支持懒加载 -->

<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="logImpl" value="LOG4J"/>
<!--     二级缓存实现,虽mybatis是默认就是开启二级缓存的但是我还是习惯写在这里,告诉其他开发者
已经使用第三方二级缓存,建议二级缓存不要乱用,需要选择一些更新少的的对象来支持二级缓存
-->
<setting name="cacheEnabled" value="true"/>
</settings>

<typeAliases>
<package name="com.leige.domain"/>
</typeAliases>

<!--   将操作sql集合加入配置文件 -->
<mappers>
<package name="com.leige.dao.mapper"/>
</mappers>
</configuration>


二级缓存配置:

<?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>

<!--  首先在全局配置中配置支持懒加载 -->

<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="logImpl" value="LOG4J"/>
<!--     二级缓存实现,虽mybatis是默认就是开启二级缓存的但是我还是习惯写在这里,告诉其他开发者
已经使用第三方二级缓存,建议二级缓存不要乱用,需要选择一些更新少的的对象来支持二级缓存
-->
<setting name="cacheEnabled" value="true"/>
</settings>

<typeAliases>
<package name="com.leige.domain"/>
</typeAliases>

<!--   将操作sql集合加入配置文件 -->
<mappers>
<package name="com.leige.dao.mapper"/>
</mappers>
</configuration>


db配置

driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\:///test
username=root
password=


日志配置

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
#log4j.logger.org.mybatis.example.BlogMapper=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n


原始dao开发,需要写接口,接口代码不再贴出,service层没有写的必要,这里只做演示,service没有实际意义,dao开发方式不同,spring配置也不一样,我两种都写在xml中,大家可以仔细看一下:

dao实现类需要继承SqlSessionDaoSupport类,并在spring容器中注入sqlsesionfactory,父类中会自己创建sqlsession,我们在使用时获取就好,而且需要注意的是开启,关闭session也不需要我们处理了,方法执行完毕spring会自动关闭session**注意要将sqlsesionfactory配置为单例**

package com.leige.dao.impl;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.leige.dao.StudentDao;
import com.leige.domain.Student;

public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentDao {

@Override
public void add(Student student) {
getSqlSession().insert("com.leige.dao.StudentMapper.insert",student);
}

}


相应配置

<bean id="studentDao" class="com.leige.dao.impl.StudentDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>


**第二种dao开发方式:

这里dao就没有实体代

码了,只需要在service层调用即可,**

package com.leige.service.impl;

import com.leige.dao.mapper.StudentMapper;
import com.leige.domain.Student;
import com.leige.service.StudentService;

public class StudentServiceImpl implements StudentService {
//第一种
/*  StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
*/
//第二种
StudentMapper mapper;
public void setMapper(StudentMapper mapper) {
this.mapper = mapper;
}

@Override
public void add(Student stu) {
//studentDao.add(stu);
mapper.insert(stu);
}

}


mapper代理就是实体的dao实现类:

相关配置:

<!-- 另一种就是mapper代理开发 ,需要指定mapperInterface接口类型和sqlsessionFactory
class要指定org.mybatis.spring.mapper.MapperFactoryBean,这个类会自动根据我们的接mapper和mapper.xml生成代理-->
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.leige.dao.mapper.StudentMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>


综合测试类:

package com.leige.test;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.leige.dao.mapper.StudentMapper;
import com.leige.domain.Student;
import com.leige.domain.StudentExample;
import com.leige.service.StudentService;

public class App {
@Test
public void testReverse(){
SqlSession session=SqlUtils.getSession();
StudentMapper mapper=session.getMapper(StudentMapper.class);

StudentExample studentExample=new StudentExample();

studentExample.setOrderByClause("sid desc");
System.out.println( mapper.selectByExample(studentExample));
}
/**
* 测试原始dao开发
*/
@Test
public void testSpring(){
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationConext.xml");
StudentService service=(StudentService) context.getBean("studentService");
Student stu=new Student();
stu.setAge(19);
stu.setName("磊哥");
service.add(stu);
}
/**
* 测试mapper代理开发
*/
@Test
public void testSpring2(){
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/applicationConext.xml");
StudentService service=(StudentService) context.getBean("studentService");
Student stu=new Student();
stu.setAge(19);
stu.setName("磊哥2");
service.add(stu);
}
}


很抱歉,这里关于spring整合mybatis没有说太多,因为没有什么可说的,唯一需要说的就是整合包的导入,然后就是配置文件,配置我都贴在上面了,大家自己可以查看下,详细解释都写在配置文件中了

最后提一句,大家学习完mybatis建议接着学习springmvc,这样就可以很快做个小项目了,在学习中记不住东西,学完就忘记,是很正常的事,所以一定要做好笔记,忘记的时候,拿出来就立马可以做出来,这点很重要,最重要的是思想,虽然mybatis框架很简陋,但是功能很强大,也涉及了很多设计思想和模式,值得我们去学习.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis spring