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 ...
随机推荐
- struts2随笔
1.struts.properties配置常量等同于struts.xml中配置(置于类加载路径下面)struts.multipart.maxSize文件上传最大大小struts.action.exte ...
- C#学习笔记15
1.平台互操作性和不安全的代码:C#功能强大,但有些时候,它的表现仍然有些“力不从心”,所以我们只能摒弃它所提供的所有安全性,转而退回到内存地址和指针的世界. C#通过3种方式对此提供支持. (1)第 ...
- 在HTML代码中使用freemarker
在HTML代码中使用freemarker 1.freemarker中显示某对象的属性使用${user.name}. 但如果name为null,freemarker就会报错.如果需要判断对象是否为空: ...
- css text-shadow
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- eclipse遇到的异常
1. Widget disposed too early for part com.kompakar.ehealth.ui.emr.mstr.medicaldocumentaudit.Medical ...
- [转]Tomcat7基于Redis的Session共享
转自:http://blog.csdn.net/catoop/article/details/48603891 目前,为了使web能适应大规模的访问,需要实现应用的集群部署.集群最有效的方案就是负载均 ...
- angular attrs.$observe和$scope.$watch的区别
http://stackoverflow.com/questions/14876112/difference-between-the-observe-and-watch-methods https:/ ...
- SQL Server ->> 内置标量函数TRY_PARSE、TRY_CAST和TRY_CONVERT的各自特点和区别
SQL Server到了目前的2014版本有三个函数是用来转换数据格式的.虽说之前版本中已经有CAST和CONVERT这两个函数来干这个事情.问题是,一旦往目标数据类型转换失败就会造成报错. TRY_ ...
- pt-duplicate-key-checker使用
pt-duplicate-key-checker工具可以检测表中重复的索引,对于一些业务量很大的表,而且开发不规范的情况下有用.基本用法: 看一下我们的测试表: mysql> desc new_ ...
- PowerBI主题制作
简单主题: { "name": "St Patricks Day", "dataColors": ["#568410", ...