Spring框架(五)—Spring与Mybatis的整合
环境搭建
导入相关依赖
1. junit
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2. mybatis
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
3. mysql-connector-java
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
4. spring
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.6</version>
</dependency>
5. aspectJ AOP
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
6. mybatis-spring整合包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
7.静态资源过滤问题
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
编写实体类(需要导入lombok依赖)
package com.study.pojo;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String pwd;
}
编写UserMapper接口
package com.study.mapper;
import com.study.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> selectUser();
}
编写UserMapper配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.study.mapper.UserMapper">
<select id="selectUser" resultType="user">
select * from mybatis.user
</select>
</mapper>
编写mybtis配置文件
<?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="com.study.pojo"/>
</typeAliases>
</configuration>
整合方式一
要和Spring一起使用Mybatis,需要在Spring应用上下文中定义至少两样东西:一个 【SqlSessionFactory】和至少一个【数据映射类】
注意:SqlSessionFactory需要一个DataSource(数据源)。这可以任意的DataSource,只需要和其他Spring数据库连接一样配置就可以了。
一、在resource目录下创建spring-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--DataSource:以前在mybatis-config中配置,整合之后直接在bean中配置-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--name:数据源名字 ref=应用的数据源,比如dataSource引用上文中配置的数据源-->
<property name="dataSource" ref="dataSource"/>
<!--绑定mybatis配置文件(如果有多个mybatis配置文件)-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--注册映射器:UserMapper.xml-->
<property name="mapperLocations" value="classpath:com/study/mapper/*.xml"/>
<!--这里也可以配置mybatis的所有设置,比如别名,设置是否开启日志等等,可以完全舍弃mybatis配置文件,但建议别名和日志开启在mybatis配置文件中设置-->
</bean>
<!--SqlSessionTemplate:就是我们使用的sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--因为它没有set方法,所以只能使用构造器注入-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
二、增加Dao接口的实现类,私有化sqlSessionTemplate
package com.study.mapper;
import com.study.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class UserMapperImpl implements UserMapper{
//我们的所有操作,都使用SqlSessionTemplate;
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> selectUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.selectUser();
}
}
三、在spring配置文件中注册bean实现(不建议使用)
<bean id="userMapper" class="com.study.mapper.UserMapperImpl">
<!--name:这个bean的sqlSession的名字,ref:引用配置好的sqlSession-->
<property name="sqlSession" ref="sqlSession"/>
</bean>
三、新建一个spring总配置类applicationContext.xml,把实现类注入到spring中(建议使用)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<!--id:bean名字 class:要注册的实现类-->
<bean id="userMapper" class="com.study.mapper.UserMapperImpl">
<!--name:这个bean的sqlSession的名字,ref:引用配置好的sqlSession-->
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
四、编写测试类
import com.study.mapper.UserMapper;
import com.study.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyTest {
@Test
public void test() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//bean的名字
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User user : userMapper.selectUser()) {
System.out.println(user);
}
}
}
五、运行成功:
整合方式二:继承SqlSessionDataSupport
SqlSessionDataSupport是一个抽象的支持类,用来为你提供SqlSession。调用getSqlSession()方法你会得到一个SqlSessionTemplate,之后可以用于执行SQL方法。
一、编写接口实现类
package com.study.mapper;
import com.study.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
@Override
public List<User> selectUser() {
return getSqlSession().getMapper(UserMapper.class).selectUser();
}
}
二、在spring中注册实现类
<bean id="userMapper2" class="com.study.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
三、修改上面的测试类
public class MyTest {
@Test
public void test() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//把userMapper改为userMapper2
UserMapper userMapper = context.getBean("userMapper2", UserMapper.class);
for (User user : userMapper.selectUser()) {
System.out.println(user);
}
}
}
四、测试运行成功