目录
1、提出问题
2、解决问题
3、相关文件
1、提出问题
SSM(Spring+SpringMVC+MyBatis)的开发,MyBatis在没有与Spring和SpringMVC整合的时候,是单独使用,单独配置。
Spring和SpringMVC的整合是无缝衔接的,因为是同生态,非常顺滑甚至“无所察觉”。
MyBatis整合前存在的问题是,部分代码过于"冗余":
两个问题:
①每次都要编写session工厂的代码
②事务控制
解决方式:
①将Session工厂交给Spring容器管理,从容器中获得执行操作的Mapper实例。
②将事务的控制交给Spring容器进行声明式事务控制
2、解决问题
配置applicationContext.xml:
这里的Mapper扫描,扫的是mapper的接口,由Spring自动创建接口的实现类。
编写测试代码:
只需要传入接口,spring容器会自动注入实现类。
这一步注入,就等价于之前的:
new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml")).openSession().getMapper(UserProxyDao.class);
3、相关文件
applicationContext.xml:
<?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"
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
">
<context:component-scan base-package="com.xzl.controller"/>
<!--2、加载jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--3、配置数据源对象-->
<bean id="dataSource_C3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置MyBatis的SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource_C3p0"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--配置Mapper扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xzl.dao"/>
</bean>
</beans>
mybatis-config.xml:
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--注册类型自定义转换器-->
<typeHandlers>
<typeHandler handler="com.xzl.handle.DateTypeHandle"/>
</typeHandlers>
<!-- 注意:分页助手的插件 配置在通用mapper之前 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 指定方言 -->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
<mappers>
<!-- <mapper resource="com/xzl/domain/mapper/UserMapper.xml"/>
<mapper resource="com/xzl/domain/mapper/UserDaoMapper.xml"/>-->
<mapper resource="com/xzl/dao/UserProxyDaoMapper.xml"/>
<!-- <mapper class="com.xzl.dao.UserProxyDao"/>-->
<!-- <package name="com.xzl.dao"/>-->
</mappers>
</configuration>
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!--spring相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--mybatis相关-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.5</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>
</project>