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整合的更多相关文章

  1. Mybatis学习(六)————— Spring整合mybatis

    一.Spring整合mybatis思路 非常简单,这里先回顾一下mybatis最基础的根基, mybatis,有两个配置文件 全局配置文件SqlMapConfig.xml(配置数据源,全局变量,加载映 ...

  2. MyBatis学习(二):与Spring整合(非注解方式配置MyBatis)

    搭建SpringMVC的-->传送门<-- 一.环境搭建: 目录结构: 引用的JAR包: 如果是Maven搭建的话,pom.xml的配置如下: <?xml version=" ...

  3. mybatis学习四 mybatis的三种查询方式

    <select id="selAll" resultType="com.caopeng.pojo.Flower"> select * from fl ...

  4. MyBatis学习系列三——结合Spring

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring MyBatis在项目中应用一般都要结合Spring,这一章主要把MyBat ...

  5. MyBatis学习总结-MyBatis快速入门的系列教程

    MyBatis学习总结-MyBatis快速入门的系列教程 [MyBatis]MyBatis 使用教程 [MyBatis]MyBatis XML配置 [MyBatis]MyBatis XML映射文件 [ ...

  6. MyBatis学习(三)---MyBatis和Spring整合

    想要了解MyBatis基础的朋友可以通过传送门: MyBatis学习(一)---配置文件,Mapper接口和动态SQL http://www.cnblogs.com/ghq120/p/8322302. ...

  7. Mybatis学习(7)spring和mybatis整合

    整合思路: 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spr ...

  8. Mybatis 学习笔记1 不整合Spring的方式使用mybatis

    两种方式都包含了: package com.test.mybatis; import java.util.List; import org.apache.ibatis.io.Resources; im ...

  9. Mybatis插件扩展以及与Spring整合原理

    @ 目录 前言 正文 插件扩展 1. Interceptor核心实现原理 2. Mybatis的拦截增强 Mybatis与Spring整合原理 1. SqlSessionFactory的创建 2. 扫 ...

  10. (原创)mybatis学习四,利用mybatis自动创建代码

    在使用mybatis的过程中,我们可以直接利用MyBatis生成器自动生成实体类.DAO接口和Mapping映射文件,然后copy到工程中即可 需要的jar包如下 下载路径如下:下载jar包 其中的g ...

随机推荐

  1. awk 用法(使用入门)

    转自:http://www.cnblogs.com/emanlee/p/3327576.html awk 用法:awk ' pattern {action} '  变量名    含义 ARGC   命 ...

  2. PHP常用框架

    1.Thinkphp  thinkphp是国内开发的框架,它有比较易读的手册,和大量的网上资源,所以我推荐大家首选thinkphp. 2.CodeIgniter  前一段时间我用过codeignite ...

  3. wpf *和auto的区别

    Auto 表示自动适应显示内容的宽度, 如自动适应文本的宽度,文本有多长,控件就显示多长. * 则表示按比例来分配宽度. <ColumnDefinition Width="3*&quo ...

  4. svn清理失败且路径显示乱码

    1.下载 sqlite数据库工具,sqlite3.exe下载地址:sqlite官网http://www.sqlite.org/download.html,我这里是windows操作系统,因此下载 Pr ...

  5. JS事件-事件处理程序-笔记总结ing...

    html事件处理程序:缺点-行为与结构耦合<input type="button" onclick = "diaoyong();"> dom0级事件 ...

  6. HTML5本地存储——IndexedDB(一:基本使用)

    在HTML5本地存储——Web SQL Database提到过Web SQL Database实际上已经被废弃,而HTML5的支持的本地存储实际上变成了 Web Storage(Local Stora ...

  7. php多进程总结

    本文部分来自网络参考,部分自己总结,由于一直保存在笔记中,并没有记录参考文章地址,如有侵权请通知删除.最近快被业务整疯了,这个等抽时间还需要好好的整理一番.   多进程--fork 场景:日常任务中, ...

  8. UITabBarController 、TabBar背景颜色设置,UITabBarItem的文字样式(颜色和大小)UITabBarItem的位置调整

    改变UITabBarController的颜色 UIView*mView=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,48)];//这是部分tabb ...

  9. 多线程之GCD

    什么是GCD Grand Central Dispatch 是Apple开发的一种多核编程技术.主要用于优化应用程序以支持多核处理器以及其他多对称处理系统TA会自动管理线程的生命周期(创建线程.调度任 ...

  10. [NHibernate]条件查询Criteria Query

    目录 写在前面 文档与系列文章 条件查询 一个例子 总结 写在前面 上篇文章介绍了HQL查询,我个人觉得使用ORM框架就是为了让少些sql,提高开发效率,而再使用HQL就好像还是使用sql,就觉得有点 ...