【Mybatis 源码分析 3】基于Mybatis使用注解实现CRUD操作
pom.xml中需要添加spring的依赖包
<dependencies>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!-- jdbc的包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!-- 测试用的依赖包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 在mybatis项目上整合spring-->
<!-- spring jar 包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!--logback 日志包-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- mybatis与spring整合的jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--spring管理的 jdbc ,以及事务相关的-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- 数据库连接池 alibaba 的 druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.7</version>
</dependency>
<!-- 日志桥接 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
在resources资源文件夹下添加mybatis-spring-conf.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-4.0.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${name}" />
<property name="password" value="${pass}" />
</bean>
<!-- 2) 获取 SqlSessionFactory 对象,并交由 spring 管理-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 3) 获取 SqlSession 对象,并交由 spring 管理 用SqlSessionTemplate得到的SqlSession可以不用我们自己操心事务的管理,以及关闭操作-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 给 SqlSessionTemplate 的构造函数赋值-->
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- 4) 搜索有哪些 mapper 实现类,把mapper接口自动配置成 spring 中的 <bean>-->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- name="basePackage":(起始)包名, 从这个包开始扫描-->
<property name="basePackage" value="DAO"/>
</bean>
</beans>
从spring容器中拿到sqlSession对象(使用SqlSessionTemplate),通过接口方法操作数据库
package Service;
import DAO.UserDAO;
import Entity.User;
import org.junit.Test;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class UserService {
@Test
public void test() throws IOException {
//加载spring容器
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("mybatis-spring-conf.xml");
//从spring容器中获取sqlSession对象
SqlSessionTemplate sqlSession = (SqlSessionTemplate)ac.getBean("sqlSession");
//获取映射器扫描包下的指定接口对象
UserDAO mapper = sqlSession.getMapper(UserDAO.class);
//CRUD操作
User newUser = new User();
newUser.setId(8);
newUser.setName("p7");
mapper.insertUser(newUser);
System.out.println(mapper.selectAll());
newUser.setName("p8");
mapper.updateUserById(newUser);
System.out.println(mapper.selectAll());
mapper.deleteUserById(8);
System.out.println(mapper.selectAll());
}
}