spring+mybatis整合
来源:互联网 发布:知乎嗜血法医 编辑:程序博客网 时间:2024/05/01 18:10
spring和mybatis整合
1、需要spring通过单例的方式管理SqlSessionFactory.
2、spring和mybatis整合生成代理对象,使用SqlSessionFactory 创建SqlSession对象(spring和mybatis整合自动完成)
applicationContext.xml
原始Dao的开发(和spring整合之后)
1、mapper.xml
...
在SqlMapConfig.xml中加载User.xml
2.dao
dao的接口实现类中需要注入SqlSessionFactory,通过spring进行注入。
这里spring声明配置方式,配置dao的bean
3.整合
1、需要spring通过单例的方式管理SqlSessionFactory.
2、spring和mybatis整合生成代理对象,使用SqlSessionFactory 创建SqlSession对象(spring和mybatis整合自动完成)
3、持久层的mapper都需要spring进行管理。
环境
mybatis3.2.7的jar包
spring3.2.0的jar包
mybatis和spring的整合包:早期由spring提供,现在由mybatis提供
SqlSessionFactory在mybatis和spring的整合包里
A
<!-- 加载配置文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 数据库连接池:使用dbcp --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property><property name="maxActive" value="10"/><property name="maxIdle" value="5"/></bean><!-- SqlSessionFactory --><bean id="sqlSessionFactory" class="org.apache.ibatis.session.SqlSessionFactoryBean"><!-- 加载mybatis配置文件 --><property name="configLocation" value="mybatis/SqlMapConfig.xml"></property><!-- 数据源 --><property name="dataSource" ref=""></property></bean>
<!-- 加载配置文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 数据库连接池:使用dbcp --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property><property name="maxActive" value="10"/><property name="maxIdle" value="5"/></bean><!-- SqlSessionFactory --><bean id="sqlSessionFactory" class="org.apache.ibatis.session.SqlSessionFactoryBean"><!-- 加载mybatis配置文件 --><property name="configLocation" value="mybatis/SqlMapConfig.xml"></property><!-- 数据源 --><property name="dataSource" ref=""></property></bean>
原始Dao的开发(和spring整合之后)
1、mapper.xml
...
在SqlMapConfig.xml中加载User.xml
2.dao
package com.zzh.ssm.dao;import com.zzh.ssm.po.User; /** * UserDao.java * @author zhangzhenhua * @date 2016年9月28日 下午7:12:07 * @description TODO */public interface UserDao {/*根据id查询用户信息*/public User findUserById(int id) throws Exception; }
dao的接口实现类中需要注入SqlSessionFactory,通过spring进行注入。
这里spring声明配置方式,配置dao的bean
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {// 向dao实现类中注入SqlSessionFactory// 通过函数注入 _已省略//private SqlSessionFactory ssf;// public UserDaoImpl(SqlSessionFactory ssf) {// this.ssf = ssf;// }@Overridepublic User findUserById(int id) throws Exception {//通过继承类得到SqlSessionSqlSession ss = this.getSqlSession();return ss.selectOne("test.findUserById", id);} }
3.整合
<!-- 加载配置文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 数据库连接池:使用dbcp --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property><property name="maxActive" value="10"/><property name="maxIdle" value="5"/></bean><!-- SqlSessionFactory --><bean id="sqlSessionFactory" class="org.apache.ibatis.session.SqlSessionFactoryBean"><!-- 加载mybatis配置文件 --><property name="configLocation" value="mybatis/SqlMapConfig.xml"></property><!-- 数据源 --><property name="dataSource" ref=""></property></bean>
1 0
- Mybatis -- Mybatis整合Spring
- 【Mybatis】Mybatis整合spring
- 【Spring+Mybatis】Spring整合Mybatis
- MyBatis-----2、MyBatis整合Spring
- Spring整合Mybatis
- spring整合myBatis
- MyBatis-Spring框架整合
- MyBatis+Spring整合示例
- MyBatis+Spring整合示例
- MyBatis+Spring整合方案
- spring + struts2 + mybatis整合
- Spring整合Mybatis
- Mybatis整合Spring
- Spring Mybatis 整合
- spring整合MyBatis
- Mybatis整合Spring
- MyBatis+Spring整合示例
- maven+spring+mybatis整合
- Android Studio启动模拟器时老是黑屏,显示“Android”字样
- Delphi中TStringList类常用属性方法详解
- C++_const常量
- [知识总结(转)]flex布局实例篇
- 浅谈C++中指针和引用的区别
- spring+mybatis整合
- iOS基础之图片平铺
- Java函数参数传递方式详解
- Linux高级文件操作 -4
- JAVA之类加载机制与反射(一)
- 深入浅出RxJava(二:操作符)
- 怎样恢复文件默认打开方式
- about acoustic signal fundamentals & signal processing
- Android Studio 快捷键总结