整合Mybatis——方式一
步骤:
- 导入相关jar包
- junit
- mybatis
- mysql数据库
- spring相关的
- aop织入
- mybatis-spring[新包]
- 编写配置文件
- 测试
回顾Mybatis
-
编写实体类
package net.cqwu.pojo; import lombok.Data; @Data public class User { private int sid; private String sname; private String pwd; }
-
编写核心配置文件
<?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核心配置文件--> <configuration> <typeAliases> <package name="net.cqwu.pojo"/> </typeAliases> <!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册--> <!--整合后注释掉--> <!-- <mappers>--> <!-- <mapper class="net.cqwu.mapper.UserMapper"/>--> <!-- </mappers>--> </configuration>
-
编写接口
package net.cqwu.mapper; import net.cqwu.pojo.User; import java.util.List; public interface UserMapper { public List<User> selectUser(); }
-
编写Mapper.xml文件
<?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">
<!--命名空间namespace = 绑定一个对应的Dao/Mapper接口-->
<mapper namespace="net.cqwu.mapper.UserMapper">
<select id="selectUser" resultType="user">
select * from mybatis.user;
</select>
</mapper>
- 测试
Mybatis-spring
-
编写数据源配置
-
sqlSessionFactory
-
sqlSessionTemplate
<?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"> <!--DataSource:使用spring的数据源替换mybatis的配置 使用Spring提供的JDBC:org.springframework.jdbc.datasource--> <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&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:net/cqwu/mapper/*.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!--无Set方法,只能使用构造器注入sqlSessionFactory--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> </beans>
applicationContext.xml
为之后的spring-mvc准备
<?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.xml"/> <bean id="userMapper" class="net.cqwu.mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> </bean> </beans>
-
需要给接口加实现类【UserMapperImpl】
package net.cqwu.mapper; import net.cqwu.pojo.User; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; public class UserMapperImpl implements UserMapper { //所有操作,之前都使用sqlSession执行,现在使用sqlSessionTemplate private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } public List<User> selectUser() { UserMapper mapper = sqlSession.getMapper(UserMapper.class); return mapper.selectUser(); } }
-
将自己写的实现类,注入到Spring中
-
测试
import net.cqwu.mapper.UserMapper; import net.cqwu.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"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); for (User user : userMapper.selectUser()) { System.out.println(user); } } }
测试结果
User(sid=1, sname=feliks, pwd=123456) User(sid=2, sname=MaxPark, pwd=123123) User(sid=3, sname=afei, pwd=111111) User(sid=4, sname=slugger, pwd=654321) User(sid=5, sname=spiderman, pwd=222222) Process finished with exit code 0
方式二:SqlSessionDaoSupport
- 创建实现类
package net.cqwu.mapper;
import net.cqwu.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
public List<User> selectUser() {
// SqlSession sqlSession = getSqlSession();
// UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// return mapper.selectUser();
//三行精简成一行
return getSqlSession().getMapper(UserMapper.class).selectUser();
}
}
- applicationContext.xml中新增
<bean id="userMapper2" class="net.cqwu.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
- 测试
public class MyTest {
@Test
public void test() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = context.getBean("userMapper2", UserMapper.class);
for (User user : userMapper.selectUser()) {
System.out.println(user);
}
}
}
结果相同
😢