整体项目结构如下
1、导入依赖
- 导入Spring及MyBatis的相关依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.24</version>
</dependency>
2、创建Dao层接口和对应的mapper文件
- 只用dao层去测试,没有创建Service层
public interface UserDao {
List<User> selectAllUser();
}
<?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="com.robot.dao.UserDao">
<select id="selectAllUser" resultType="com.robot.entity.User">
SELECT id, name, pwd FROM user;
</select>
</mapper>
其中User实体类中有三个属性,且对应数据库中user表中的同样三个属性
private Integer id;
private String name;
private String pwd;
3、配置MyBatis的配置文件: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>
<!-- 日志工厂-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!-- 开启全局缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!-- 给实体类起别名-->
<typeAliases>
<package name="com.robot.pojo"/>
</typeAliases>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
4、配置Spring的配置文件:applicationContext.xml
- 配置数据源,配置连接数据库的相关信息(连接数据库的配置文件放在db.properties,所以再此之前先加载配置文件)
- 构建SqlSessionFactory:在这里加载了MyBatis的配置文件
- 构建mapper:管理DAO实现类的创建,并创建DAO对象,存入工厂管理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 构建SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- 构建mapper-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.robot.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
也可以选择不去创建MyBatis的配置文件,直接在sqlSessionFactory中去绑定mapper文件即可
5、测试
- @ExtendWith注解为Junit5的注解,在Junit4中使用@RunWith
- @ContextConfiguration为加载配置文件
- @Resource:根据属性名字注入值,这里可以直接为UserDao创建对象
这些注解为Spring整合Junit的形式进行测试
@ExtendWith(SpringExtension.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class UserDaoTest {
@Resource
private UserDao userDao;
@Test
public void selectAllUser() {
List<User> users = userDao.selectAllUser();
for (User user : users) {
System.out.println(user);
}
}
}
最终结果查询成功,说明Spring与MyBatis整合成功