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 ...
随机推荐
- 开始学emacs-1
读readme http://ftp.gnu.org/gnu/emacs/windows/README
- transition
transition: all 所有属性都将获得过渡效果. property 定义应用过渡效果的 CSS 属性名称列表,列表以逗号分隔 transition-duration 规定完成过渡效果需要多少 ...
- IIS如何配置可以下载APK、IPA文件
解决步骤: 1).打开IIS服务管理器,找到服务器,右键-属性,打开IIS服务属性: 2.单击MIME类型下的“MIME类型”按钮,打开MIME类型设置窗口: 3).单击“新建”,建立新的MIME类型 ...
- IDC机房线路质量测试方案
1.测试节点: 北京:123.206.*.* 上海:139.196.*.* 广州:119.29.*.* 汕头:125.91.*.* 香港:103.20.*.* 美国:198.52.*.* 测试服务器: ...
- elk系列6之tcp模块的使用
preface tcp模块的使用场景如下: 有一台服务器A只需要收集一个日志,那么我们就可以不需要在这服务器上安装logstash,我们通过在其他logstash上启用tcp模块,监听某个端口,然后我 ...
- centos6 install mcrypt
Download the latest epel-release rpm from http://dl.fedoraproject.org/pub/epel/6/x86_64/ Install epe ...
- elasticsearch curl operation
Elastic Search API Index.简单的介绍了使用Elastic Search 如何建立索引. ElasticSearch-API-Index 索引创建API允许初始化一个索引.Ela ...
- 外景VR的应用
留坑,续写. 最近在做外景的项目,被相关的帧率优化和灯光布置困扰的不要不要的.下面写下我是怎么优化帧率和对帧率的一些理解. 帧率,游戏的重要影响因素,会对玩家的手感以及视觉产生重大的影响,一般的游戏帧 ...
- 前端进阶试题css(来自js高级前端开发---豪情)既然被发现了HOHO,那我就置顶了嘿嘿!觉得自己技术OK的可以把这套题目做完哦,然后加入高级前端的社区咯
http://www.cnblogs.com/jikey/p/4426105.html js高级前端开发加群方法(此群很难进,里面纯技术,严禁广告,水群) 完整题目做完发邮箱(jikeytang@16 ...
- hadoop源码编译——2.5.0版本
强迫症必治: WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using b ...