1,编写数据源
2,sqlSessionFactory
3,sqlSessionTemplate
4,添加接口实现类
5,单元测试
User.java
package com.miao.pojo; import lombok.Data; @Data public class User { private int id; private String name; private String pwd; }
UserMapper
package com.miao.mapper; import com.miao.pojo.User; import java.util.List; public interface UserMapper { public List<User> selectUser(); }
UserMapperImpl.java
package com.miao.mapper; import com.miao.pojo.User; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; public class UserMapperImpl implements UserMapper{ //之前操作mybatis都使用sqlSession,现在都使用sqlSessiontemplate private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate=sqlSessionTemplate; } public List<User> selectUser() { List<User> userList = sqlSessionTemplate.getMapper(UserMapper.class).selectUser(); return userList; } }
UserMapper.xml
<?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.miao.mapper.UserMapper"> <select id="selectUser" resultType="User"> select * from mybatis.user; </select> </mapper>
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> <!--这些标签的引入还需要注意顺序--> <!--引入外部配置文件--> <properties resource="db.properties"/> <!--引入log4j文件--> <settings> <!--<setting name="logImpl" value="LOG4J"/>--><!--开启日志--> <setting name="mapUnderscoreToCamelCase" value="true"/><!--开启驼峰转换设置--> <setting name="cacheEnabled" value="true"/><!--开启缓存--> </settings> <!--别名 简化代码--> <typeAliases> <package name="com.miao.pojo"/> </typeAliases> <!--此处的绑定接口会与spring中的 <property name="mapperLocations" value="classpath:com/miao/mapper/*.xml"/> 冲突--> <!--绑定接口--> <!--<mappers> <package name="com/miao/mapper"/> </mappers>--> </configuration>
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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd "> <!--DataSources:使用Spring的数据源替换mybatis的配置--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!--SqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--绑定mybatis--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/miao/mapper/*.xml"/> </bean> <!--SqlSessionTemplate是我们使用的sqlSession--> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <!--只能用构造器注入sqlSession,因为他没有sqlSession--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> </beans>
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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd "> <import resource="spring-dao.xml"/> <bean id="userMapper" class="com.miao.mapper.UserMapperImpl"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/> </bean> </beans>
test.java
import com.miao.mapper.UserMapper; import com.miao.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.support.ClassPathXmlApplicationContext; import java.io.IOException; import java.io.InputStream; import java.util.List; public class test { @Test public void test1() throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); for (User user : userMapper.selectUser()) { System.out.println(user); } } }