说明:
第1步:配置连接池与事务管理(配置和Spring事务管理一致),在applicationContext.xml中。
第2步:配置SqlSessionFactory为单例模式,在applicationContext.xml中;
第3步:配置Dao动态代理,在applicationContext.xml中;
第4步:代码加载applicationContext.xml,获取ApplicationContext,然后获取Dao接口实现;
第5步:拿到Dao实现类执行增删改查操作。
Mysql驱动jar包:mysql-connector-java-x.x.x.jar
C3P0依赖jar包:c3p0-x.x.x.x.jar、mchange-commons-java-x.x.x.jar
MyBatis依赖jar包:mybatis-x.x.x.jar、mybatis-spring-x.x.x.jar、ant-x.x.x.jar、ant-launcher-x.x.x.jar、asm-x.x.jar、cglib-x.x.xx.jar、、javassist-x.x.x-GA.jar、log4j-x.x.x.jar、log4j-api-x.x.x.jar、log4j-core-x.x.x.jar、ognl-x.x.x.jar、slf4j-api-x.x.x.jar、slf4j-log4j12-x.x.x.jar
Spring依赖jar包:spring-core-x.x.x.RELEASE.jar、spring-context-x.x.x.RELEASE.jar、spring-beans-x.x.x.RELEASE.jar、spring-expression-x.x.x.RELEASE.jar、spring-aop-x.x.x.RELEASE.jar、spring-aspects-x.x.x.RELEASE.jar、aspectjweaver-x.x.x.jar、aopalliance-x.x.jar、spring-tx-x.x.x.RELEASE.jar、spring-jdbc-x.x.x.RELEASE.jar
一、配置数据连接池:
1.创建jdbc.properties文件:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC
username=root
password=root
2.在applicationContext.xml中<beans>标签内加载jdbc.properties文件:
<!-- 导入jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
3.配置C3P0连接池:
<!-- 配置C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}" /><!-- 获取jdbc.properties中的值 -->
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${user}" />
<property name="password" value="${password}" />
</bean>
二、配置SqlSessionFactory:
在applicationContext.xml中<beans>标签内:
<!-- 配置SqlSessionFactory -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 给SqlSessionFactory设置连接池,ref值为上面配的连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
三、创建数据实体类,配置Dao映射:
1.定义实体类:
//用户信息实体类
public class User {
public Integer id;
public String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.定义Dao接口:
//User的dao接口
public interface UserMapper {
public void insert(User user);
}
3.创建UserMapper.xml,配置Dao映射:
<?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">
<!-- UserMapper为User的dao接口 -->
<mapper namespace="com.yyh.hkw.dao.UserMapper">
<!-- 插入User数据 -->
<insert id="insert" parameterType="com.yyh.hkw.domain.User">
insert into user(id,name) values(#{id}, #{name})
</insert>
</mapper>
四、Dao动态代理配置(自动创建实现子类):
在applicationContext.xml中<beans>标签内:
1.方式1,配置接口:
<!-- 方式1,配置Dao接口 -->
<!-- Dao动态代理配置 -->
<bean id="baseDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 设置SqlSessionFactory属性值,ref值为上面配的sessionFactory -->
<property name="sqlSessionFactory" ref="sessionFactory" />
<!-- 设置mapperInterface属性值为自已的Dao接口 -->
<property name="mapperInterface" value="com.yyh.hkw.dao.UserMapper" />
</bean>
2.方式2,包扫描:
<!-- 方式2,包扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yyh.hkw.dao"/>
</bean>
四、使用:
1.加载applicationContext.xml配置文件:
private static ApplicationContext sCtx;
static {
// 自动加载applicationContext.xml配置文件
sCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
2.测试插入数据:
// 插入数据
public void insertUser() throws IOException {
// 1.获取Dao代理类,applicationContext.xml中配置了UserMapper动态代理
UserMapper dao = (UserMapper) sCtx.getBean(UserMapper.class);
User user = new User();
user.id = 1;
user.name = "杨先生";
// 3.执行插入语句
dao.insert(user);
// 4.关闭工厂
((ClassPathXmlApplicationContext) sCtx).close();
}