【原】spring+springmvc+mybatis整合
整合框架的代码结构:
最全约束:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
1:配置mybatis:
1.1配置jdbc
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3366/test
jdbc.user=root
jdbc.password=root
1.2:配置log4j在控制台打印sql语句:
1.3:配置mybatis.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>
<!-- 配置映射文件的resultType为类的基本名称 -->
<typeAliases>
<package name="com.mytest.beans"/>
</typeAliases>
<!-- 配置mabatis的映射扫描文件 ,这里也可以不配置,等到整合spring+mybatis的地方再配置,就单独测试mybatis的时候使用-->
<mappers>
<package name="com.mytest.dao"/>
</mappers>
</configuration>
2:mybatis+与spring的整合:
其实说白了,就是把创建mybatis的sqlsession这个bean的权限交给spring容器:
2.1:Spring容器中配置数据源:
<!-- ///////////////////////////////////////spring容器 /////////////////////////////////////// -->
<!-- 1:mybatis配置 -->
<!--1.1 注册C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 注册属性文件 -->
<context:property-placeholder location="classpath:resources/jdbc.properties"/>
1.2 生成SqlSessionFactory:
<!--1.2 生成SqlSessionFactory -->
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置映射文件 -->
<property name="mapperLocations" value="classpath:com/mytest/mapper/*.xml"></property>
<!-- spring + mybatis配置文件整合 -->
<property name="configLocation" value="classpath:resources/mybatis.xml"/>
</bean>
1.3注册映射扫描配置器,生成Dao的代理对象 :
<!-- 1.3注册映射扫描配置器,生成Dao的代理对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory"/>
<property name="basePackage" value="com.mytest.dao"/>
</bean>
1.4开启spring的注解扫描
<!-- 开启spring的注解扫描 -->
<context:component-scan base-package="com.mytest.controller"/>
<!-- ///////////////////////////////////////spring容器 /////////////////////////////////////// -->
<!-- 1:mybatis配置 -->
<!--1.1 注册C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 注册属性文件 -->
<context:property-placeholder location="classpath:resources/jdbc.properties"/>