整合所需要的jar包这里就不多说了,创建类图如下:
db.properties文件内容如下:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/goods?serverTimezone=GMT%2B8&rewriteBatchedStatements=true
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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">
<!-- 读取db.properties -->
<context:property-placeholder
location="classpath:db.properties" />
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<!-- 数据库驱动 -->
<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="maxTotal" value="${jdbc.maxTotal}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
<property name="initialSize" value="${jdbc.initialSize}" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven
transaction-manager="transactionManager" />
<!-- 配置Mybatis工厂 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定核心配置文件 -->
<property name="configLocation"
value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 实例化Dao -->
<bean id="customerDao" class="ltd.ourfamily.dao.impl.CustomerDaoImpl">
<!-- 注入SqlSessionFactory对象实例 -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
mybatis-config.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>
<package name="ltd.ourfamily.po"/>
</typeAliases>
<!-- 配置mapper -->
<mappers>
<mapper resource="ltd/ourfamily/po/CustomerMapper.xml"/>
</mappers>
</configuration>
这里可以很明显看出,每次调用findCustomerById方法时,都需要获取到CustomerMapper.xml中的id,这样无疑会加大代码重复且繁琐,有没有什么方法来解决这个问题??
肯定是有的,我们可以利用mybatis的Mapper接口开发。
1.基于MapperFactoryBean的整合
实现原理:创建Mapper及其配置文件,在application中创建Bean,引入MapperFactoryBean,配置接口及SqlSessionFactory即可
修改后的包图:
增添CustomerMapper.xml
<?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="ltd.ourfamily.mapper.CustomerMapper">
<select id="findCustomerById" parameterType="Integer" resultType="customer">
SELECT * from t_customer where id=#{id}
</select>
</mapper>
增添CustomerMapper.class
package ltd.ourfamily.mapper;
import ltd.ourfamily.po.Customer;
public interface CustomerMapper {
//根据id获取到Customer
public Customer findCustomerById(Integer id);
}
修改对应的mybatis-config.xml(将Mapper改为现在的位置)
修改applicationContext.xml文件如下
2.基于MapperScannerConfigurer
实际开发项目中,如果每个接口都需要配置,那无疑会让代码显得冗余,所以为了解决这个问题,可以采用MapperScannerConfigurer类来解决这个问题,它的使用也非常简单:
<!-- mapper代理开发-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="ltd.ourfamily.mapper"></property>
</bean>