MyBatis + Spring整合开发

a)使用Spring容器用单例模式管理Mybatis的sqlSessionFactory;
b)使用Spring管理连接池、数据源等;
c)将Dao/Mapper动态代理对象注入到Spring容器中,使用时直接获取;

  

一、MyBatis整合Spring框架

  a)导入所需的包;

  

  b)创建Mybatis主配置文件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>
<!-- 别名直接按他的类名去取的User(不区别大小写) -->
<package name="com.Gary.bean"/>
</typeAliases> </configuration>

sqlMapConfig.xml

  c)创建Spring主配置文件applicationContext.xml (以下是spring框架xml的头,需要导入约束);

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
</beans>

applicationContent.xml

  d)配置C3P0连接池;

    <!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd "> <!-- 读取配置文件 -->
<context:property-placeholder location="db.properties" /> <!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean> </beans>

applicationContent.xml

  e)读取db.properties;

    <!-- 读取配置文件 -->
<context:property-placeholder location="db.properties" />

  jdbc.driverClass=com.mysql.jdbc.Driver
  jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_mybatis
  jdbc.user=root
  jdbc.password=123456

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd "> <!-- 读取配置文件 -->
<context:property-placeholder location="db.properties" /> <!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean> </beans>

applicationContent.xml

    jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_mybatis
jdbc.user=root
jdbc.password=123456

db.properties

  f)配置sqlSessionFactory;

    <!-- 配置mybatis sqlSessionFactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 告诉spring mybatis的核心配置文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd "> <!-- 读取配置文件 -->
<context:property-placeholder location="db.properties" /> <!-- 配置c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!-- 配置mybatis sqlSessionFactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 告诉spring mybatis的核心配置文件 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
</bean> </beans>

applicationContent.xml

# 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

log4j.properties

package com.Gary.test;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContent.xml"); SqlSessionFactoryBean bean = ac.getBean(SqlSessionFactoryBean.class); System.out.println(bean); } }

Test.java

最新文章

  1. 如何开发一个简单的HTML5 Canvas 小游戏
  2. TOP 和 OFFSET 筛选(转)
  3. Ubuntu12.04安装lnmp环境笔记
  4. sql 操作常用操作语句 新增、修改字段等
  5. centos6.3环境下升级python及MySQLdb的安装
  6. [Ng]Angular应用点概览
  7. AIX网络性能优化简介
  8. Xamarin.Forms——WebView技术研究
  9. linux 命令查看CPU和内存信息
  10. AngularJs编写指令
  11. 都是类型惹的祸——小心unsigned
  12. Django设置
  13. FormsAuthentication 登录兼容 IE11 保存cookie
  14. 关于AS3里的Matrix3D中的appendXXX和prependXXX
  15. C程序的构成及动态内存分配
  16. .net中Web.config文件的基本原理及相关设置
  17. js图片放大镜 可动态更换图片
  18. for循环、穷举法和迭代
  19. Windows下与Linux下编写socket程序的区别 《转载》
  20. 基于LINUX的多功能聊天室

热门文章

  1. (八)二进制文件在webservice中的处理(以byte[]字节数组方式)
  2. (四)XML基础(客户端和服务端发送与接收xml数据)
  3. 11. Java方法的定义与使用
  4. 0.a开始数据结构征程
  5. nginx分割日志
  6. 03 Django之视图函数
  7. React/Refs and this DOM
  8. 整屏纵向切换超出整屏内容纵向滚动 解决办法-fullpage
  9. 负载均衡之haproxy负载均衡算法及haproxy的工作模式
  10. kbmMW 5.10.10 SmartBinding问题修正