MyBatis(9)整合spring
具体的感兴趣可以参考:MyBatis
此时此刻,没用的话不再多说了,直接开始代码工程吧!
整体的代码实现:
具体使用到的我们在进行细说
基本上理解一边就能会使用整合

准备工作:
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/shopping
jdbc.username=root
jdbc.password=12345
log4j.properties
#Global logging configuration
# 在开发环境下日志级别要设成
log4j.rootLogger = DEBUG, stdout
# 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
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
使用dbcp数据库连接池
<!-- 数据源,使用dbcp -->
<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>
不需要在类中单独进行配置
<!-- sqlSessinFactory -->
<!-- org.mybatis.spring.SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="mybatis/SqlMapConfig.xml" />
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
SqlMapConfig.xml
<?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.MrChengs.po"/>
</typeAliases> <!-- 加载 映射文件 -->
<mappers>
<mapper resource="sqlmap/User.xml"/>
</mappers>
</configuration>
其他的设置项在使用的时候在进行添加,,,,,,
一.dao开发
Userser.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="test">
<select id="findUserByID" parameterType="int" resultType="com.MrChengs.po.User">
select * from user where id=#{id}
</select>
</mapper>
UserDao.java
public interface UserDao {
//根据id查询用户信息
public User findUserById(int id) throws Exception;
}
UserDaoImp.java
public class UserDaoImp extends SqlSessionDaoSupport implements UserDao {
@Override
public User findUserById(int id) throws Exception {
SqlSession sqlSession = this.getSqlSession();
User user =sqlSession.selectOne("test.findUserByID", id);
return user;
}
}
<bean id="userDao" class="com.MrChengs.UserDao.UserDaoImp">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
测试:
public class Test {
private ApplicationContext applicationContext;
public ApplicationContext getApplication(){
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
return applicationContext;
}
//Dao开发
@org.junit.Test
public void testDao() throws Exception{
UserDao userdao = (UserDao) getApplication().getBean("userDao");
User user = userdao.findUserById(1);
System.out.println(user);
}
}
DEBUG [main] - ==> Preparing: select * from user where id=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
DEBUG [main] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3bf7ca37]
DEBUG [main] - Returning JDBC Connection to DataSource
User [id=1, username=王五, birthday=null, sex=2, address=null]
二.Mapper代理开发
UserMapper.java
//相当于dao接口
public interface UserMapper {
//根据id查询用户信息
public User findUserById(int id) throws Exception;
}
UserMapper.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.MrChengs.mapper.UserMapper">
<select id="findUserById" parameterType="int" resultType="com.MrChengs.po.User">
SELECT * FROM USER WHERE id=#{value}
</select>
</mapper>
注意上述的两个文件在一个路径下。
SqlMapConfig.xml
<!-- 加载 映射文件 -->
<mappers>
<mapper resource="sqlmap/User.xml"/>
<package name="com.MrChengs.mapper"/>
</mappers>
通过MapperFactoryBean创建代理对象
<!-- mapper配置
MapperFactoryBean:根据mapper接口生成代理对象
-->
<!-- mapperInterface指定mapper接口 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.MrChengs.mapper.UserMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
测试:
public class TestMapper {
private ApplicationContext applicationContext;
public ApplicationContext getApplication(){
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
return applicationContext;
}
//Mapper开发
@org.junit.Test
public void testmapper() throws Exception{
UserMapper user = (UserMapper) getApplication().getBean("userMapper");
User u = user.findUserById(1);
System.out.println(u);
}
}
DEBUG [main] - ==> Preparing: SELECT * FROM USER WHERE id=?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
DEBUG [main] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7c729a55]
DEBUG [main] - Returning JDBC Connection to DataSource
User [id=1, username=王五, birthday=null, sex=2, address=null]
如果有很多个类都需要对每个mapper进行配置,此时需要大量的工程???
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.MrChengs.mapper.UserMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
。。。。。
通过MapperScannerConfigurer进行mapper扫描(建议使用)
applicationContext.xml
说明:
name="basePackage"
指定扫描的包名
如果扫描多个包,每个包中间使用半角逗号分隔
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.MrChengs.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean>
其余的测试不变......

MyBatis(9)整合spring的更多相关文章
- MyBatis之整合Spring
MyBatis之整合Spring 整合思路: 1.SqlSessionFactory对象应该放到spring容器中作为单例存在 2.传统dao的开发方式中,应该从spring容器中获得sqlSessi ...
- 【Mybatis】MyBatis之整合Spring(八)
创建环境 系统:macOS Java:1.8 软件:eclipse,maven,mysql 创建步骤 本例:创建一个Maven项目(SpringMVC+Spring+Mybatis),页面上展示员工列 ...
- Mybatis整合Spring
根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持.因此由Mybatis社区自己开发了一个My ...
- 框架整合——Spring与MyBatis框架整合
Spring整合MyBatis 1. 整合 Spring [整合目标:在spring的配置文件中配置SqlSessionFactory以及让mybatis用上spring的声明式事务] 1). 加入 ...
- Mybatis整合Spring -- typeAliasesPackage
Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持. 因此由M ...
- 160330、Mybatis整合Spring
转自csdn文章 http://haohaoxuexi.iteye.com/blog/1843309 Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前, ...
- MyBatis 学习-与 Spring 集成篇
根据官方的说法,在 ibatis3,也就是 Mybatis3 问世之前,Spring3 的开发工作就已经完成了,所以 Spring3 中还是没有对 Mybatis3 的支持.因此由 Mybatis 社 ...
- SSM框架-----------SpringMVC+Spring+Mybatis框架整合详细教程
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
- mybatis入门_一对多,多对多映射以及整合spring框架
一.一对多映射. 1.1 一对多映射之根据多的一方关联查询一的一方 示例:查询出具体的订单信息,同时也查询出来订单的用户信息. 引入的订单表如下所示: 框选出来的为具体的外键. 订单的Pojo类如下所 ...
- 1.springMVC+spring+Mybatis的整合思路
SSM整合的过程:就是把一些东西交给spring管理,也就是添加配置文件的一个过程.那么有哪些东西我们要交给spring管理呢?大概有以下几个: 1.数据源(可配置数据库连接池) 2.SqlSessi ...
随机推荐
- 铵钮提交事件PostBack之后,一些动态加载的物件丢失
今早起来,发现skype有网友留言,情况大约如下,不过Insus.NET还是先感谢网友的测试.http://www.cnblogs.com/insus/p/3193619.html 如果你有看此篇博 ...
- show_space
create or replace procedure show_space( p_segname_1 in varchar2,p_space in varchar2 default 'AUTO',p ...
- 《Maven实战》关联实际工作的核心知识
通读了<Maven实战>这本书,由于在实际的工作中,对其有一定的操作上的经验.因此,再回头去通读这本书,就能够更加精准的把握里面的核心知识了. 以下我主要从两点去介绍之—— 1> m ...
- 互联网轻量级框架SSM-查缺补漏第九天
简言: 第九章 Spring Ioc的概念 IoC(Inversion of Control)控制反转:比如想喝橙汁,在没有饮品店的日子,最直观的做法是买果汁机.橙汁.这是你自己“主动”创造的过程,也 ...
- Ajax实现页面跳转与结果返回
ajax实现页面局部跳转与结果返回 1.带有结果返回的提交过程 这里用一个提交按钮来演示,HTML代码为: <input type="button" class=" ...
- git基本命令集合
以下内容不适合初学者 括号中表示需要自己填写 v1.0 git add git commit -m git commit -a -m git commit -amend git clone git l ...
- 【Immutable】拷贝与JSON.parse(JSON.stringify()),深度比较相等与underscore.isEqual(),性能比较
样本:1MB的JSON文件,引入后生成500份的一个数组: 结果如下: 拷贝性能: JSON.parse(JSON.stringify()) 的方法:2523.55517578125ms immuta ...
- BZOJ4698: Sdoi2008 Sandy的卡片(后缀数组 二分)
题意 题目链接 Sol 不要问我为什么发两篇blog,就是为了骗访问量 后缀数组的也比较好想,先把所有位置差分,然后在height数组中二分就行了 数据好水啊 // luogu-judger-enab ...
- CentOS 7运维管理笔记(4)----安装ftp服务器
在CentOS 7下安装ftp服务器,可以使局域网内的主机拥有共享文件的一个站点. 在Linux系统下,vsftp是一款应用比较广泛的FTP软件,其特点是小巧轻快,安全易用.目前在开源操作系统中常用的 ...
- c# 利用反射 从json字符串 动态创建类的实例 并动态为实例成员赋值
转自 http://hi.baidu.com/wjinbd/item/c54d43d998beb33be3108fdd 1 创建自己要用的类 class stu { string _name; int ...