mybaties执行流程:
step1.Resources获取加载全局配置文件 —>
step2.解析配置文件流:InputStream inputStream = Resources.getResourceAsStream(resource);–>
step3.通过流生成SqlSessionFactory 对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);–>
step4.通过sqlSessionFactory.openSession方法得到sqlSession
step5.在test测试类中通过反射sqlSession.getMapper(***Mapper.class)得到Mapper接口对象,
step6.通过Mapper接口对象调用接口定义的增删改查方法
spring:
1.在test.java中通过:ApplicationContext applicationContext=new ClassPathXmlApplicationContext(“beans.xml”);
拿到配置文件为beans.xml的容器。
2.再通过UserServiceImpl userServiceImpl = (UserServiceImpl) applicationContext.getBean(“userServiceImpl”);拿到userServiceImpl类型的bean对象
3.通过userServiceImpl 对象调用这个接口定义的方法
spring 整合mybaties:
步骤1:创建容器:即创建xml文件例如: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:context="http://www.springframework.org/schema/context"
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/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="java"/> <!--注解必须要这个配置:扫描java包下面的注解,即可以立刻把java包下面的类注册到spring的容器中-->
<context:annotation-config/>
</beans>
步骤2:在容器中创建一个dataSource的bean对象(用来代替:MybatisUtils工具类读取的源文件流)
<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/mytest?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone = GMT"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
步骤3:再在容器中创建一个sqlSessionFactory的bean对象(用来代替mybatis执行流程的step3创建的对象)
<!--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:Mapper/*.xml"/>
</bean>
步骤4:再次创建sqlSession的bean对象(用来代替mybatis执行流程的step3创建的对象sqlSession)
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" value="sqlSessionFactory"/>
</bean>
步骤5:创建UserMapperImpl类(这是spring的步骤,创建接口对于的Impl类,然后在容器中创建bean)
public class UserMapperImpl implements UserMapper{
//用SqlSessionTemplate来操作增删改查 原来是使用SqlSession
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> getAllUser() {
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
return userMapper.getAllUser();
}
}
步骤6:在容器中创建UserMapperImpl对象
<bean id="UserMapperImpl" class="Mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
步骤7:创建测试类
@Test
public void springAndmybatisGetUserAll(){
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapper userMapper=applicationContext.getBean("UserMapperImpl", UserMapperImpl.class);
List<User> userList =userMapper.getAllUser();
for (User user:userList) {
System.out.println(user);
}
}