MyBatis学习(四)MyBatis和Spring整合
MyBatis和Spring整合
思路
1、让spring管理SqlSessionFactory
2、让spring管理mapper对象和dao。
使用spring和mybatis整合开发mapper代理及原始dao接口。
自动开启事务,自动关闭 sqlsession.
3、让spring管理数据源( 数据库连接池)
创建整合工程

加入jar包
添加Folder--lib
1、mybatis3.2.7本身的jar包
2、数据库驱动包
3、spring3.2.0
4、spring和mybatis整合包
从mybatis的官方下载spring和mybatis整合包mybatis-spring-1.2.2.jar

log4j.properties
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
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>
<mappers>
<package name="com.cxz.mybatis.mapper"/>
</mappers>
</configuration>
applicationContext.xml
1、数据库属性文件和数据源(dbcp连接池)
2、SqlSessionFactory
3、mapper扫描
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<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>
<!-- SqlsessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
</bean>
<!--
MapperScannerConfigurer:mapper的扫描器,将包下边的mapper接口自动创建代理对象,
自动创建到spring容器中,bean的id是mapper的类名(首字母小写)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置扫描包的路径
如果要扫描多个包,中间使用半角逗号分隔
-->
<property name="basePackage" value="com.cxz.mybatis.mapper"/>
<!-- 使用sqlSessionFactoryBeanName -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
开发mapper.xml和mapper.java
<?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.cxz.mybatis.mapper.UserMapper">
<select id="findUserById" parameterType="int" resultType="com.cxz.mybatis.pojo.User" >
SELECT * FROM USER WHERE id= #{id}
</select>
</mapper>
package com.cxz.mybatis.mapper;
import com.cxz.mybatis.pojo.User;
public interface UserMapper {
//根据id查询用户信息
public User findUserById(int id) throws Exception;
}
测试程序
public class UserMapperTest {
private ApplicationContext applicationContext;
@Before
public void setUp() throws Exception {
//创建spring容器
applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
@Test
public void testFindUserById() throws Exception {
//创建代理对象
UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
User user = userMapper.findUserById(33);
System.out.println(user);
}
}
MyBatis学习(四)MyBatis和Spring整合的更多相关文章
- Mybatis学习(六)————— Spring整合mybatis
一.Spring整合mybatis思路 非常简单,这里先回顾一下mybatis最基础的根基, mybatis,有两个配置文件 全局配置文件SqlMapConfig.xml(配置数据源,全局变量,加载映 ...
- MyBatis学习(二):与Spring整合(非注解方式配置MyBatis)
搭建SpringMVC的-->传送门<-- 一.环境搭建: 目录结构: 引用的JAR包: 如果是Maven搭建的话,pom.xml的配置如下: <?xml version=" ...
- mybatis学习四 mybatis的三种查询方式
<select id="selAll" resultType="com.caopeng.pojo.Flower"> select * from fl ...
- MyBatis学习系列三——结合Spring
目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBat ...
- MyBatis学习总结-MyBatis快速入门的系列教程
MyBatis学习总结-MyBatis快速入门的系列教程 [MyBatis]MyBatis 使用教程 [MyBatis]MyBatis XML配置 [MyBatis]MyBatis XML映射文件 [ ...
- MyBatis学习(三)---MyBatis和Spring整合
想要了解MyBatis基础的朋友可以通过传送门: MyBatis学习(一)---配置文件,Mapper接口和动态SQL http://www.cnblogs.com/ghq120/p/8322302. ...
- Mybatis学习(7)spring和mybatis整合
整合思路: 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spr ...
- Mybatis 学习笔记1 不整合Spring的方式使用mybatis
两种方式都包含了: package com.test.mybatis; import java.util.List; import org.apache.ibatis.io.Resources; im ...
- Mybatis插件扩展以及与Spring整合原理
@ 目录 前言 正文 插件扩展 1. Interceptor核心实现原理 2. Mybatis的拦截增强 Mybatis与Spring整合原理 1. SqlSessionFactory的创建 2. 扫 ...
- (原创)mybatis学习四,利用mybatis自动创建代码
在使用mybatis的过程中,我们可以直接利用MyBatis生成器自动生成实体类.DAO接口和Mapping映射文件,然后copy到工程中即可 需要的jar包如下 下载路径如下:下载jar包 其中的g ...
随机推荐
- 关于GIL
1同一时刻只有一个线程通过一个线程到解释器运行 2在多核上会有些不一样 不仅仅会降低python的效率 并且还会影响到整个机器系统的效率 python的gil是每100条cpu指令开始check 如果 ...
- telnet建立http连接获取网页HTML内容
利用telnet可以与服务器建立http连接,获取网页,实现浏览器的功能.它对于需要对http header进行观察和测试到时候非常方便.因为浏览器看不到http header. 步骤如下: 1. 运 ...
- iOS多线程学习
在 iOS 中其实目前有 4 套多线程方案,他们分别是: Pthreads NSThread GCD NSOperation & NSOperationQueue 所以接下来,我会一一讲解这些 ...
- 关于WEB项目的一点想法
有点失落.迷茫,差点在上班的时候发了火.原因是之前离职的一位同事,在代码里不加注释,而且百般偷懒,致使很多应该的验证没有验证,很多应该考虑到的情况没有考虑.因为是老员工,我相比他来说是新员工.气势上总 ...
- Hibernate的关联映射关系
一:多对一 <many-to-one 1.name:当前类的属性名(关联映射的类) 2.column:属性多对应的类的对应的表的外键(连接条件) 3.class:属性所对应的类的权限定名 4.n ...
- XML编码utf-8有中文无法解析或乱码 C#
XML的encoding="UTF-8" ,含有中文的话(部分)会出现乱码. 网上还是很多这类问题跟解决办法的. 表现为用ie或者infopath之类的xml软件打不开这个xml, ...
- 烂泥:nginx、php-fpm、mysql用户权限解析
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://ilanni.blog.51cto.com/526870/1561097 本文首发 ...
- Q: ossfs挂载时如何设置权限?
Q: ossfs挂载时如何设置权限? 如果要允许其他用户访问挂载文件夹,可以在运行ossfs的时候指定allow_other参数: ossfs your_bucket your_mount_point ...
- [NHibernate]N+1 Select查询问题分析
目录 写在前面 文档与系列文章 N+1 Select查询问题分析 总结 写在前面 在前面的文章(延迟加载,立即加载)中都提到了N+1 Select的问题,总觉得理解的很不到位,也请大家原谅,这也是为什 ...
- OC初步 (一)
OC完全兼容C, 代码后缀名一般习惯用 *.m 或 *.mm,按惯例从 "Hello,World!" 开始,我们编写一个 test.mm 文件如下: #import <Fou ...