(1).编写数据源配置
可以顶替mybatis的核心配置文件
(1).引用类 id: 起一个类对象的变量名 class: 引用驱动管理数据资源这个类 (2).给类进行属性的赋值 <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&charEncoding=UTF8"/> <property name="username" value="root"/> <property name="password" value="121788"/> (3).这个类是JAR包自带的,不需要我们进行手动的创建
<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&charEncoding=UTF8"/> <property name="username" value="root"/> <property name="password" value="121788"/> </bean>
(2).建立sqlSessionFactory
可以顶替工具类
(1).注入sqlSessionFactory 引用SqlSessionTemplate类,并且利用构造器注入 (2).有且只能使用构造器进行注入 因为SqlSessionTemplate这个类中只有有参构造方法,没有无参构造方法;所以我们只能使用构造器进行注入 (3).这个类是JAR包自带的,不需要我们进行手动的创建
<!-- SqlSessionTemplate: 就是我们需要用的sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!-- 只能利用构造器注入 sqlSessionFactory,因为他没有set方法--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean>
(4).接口添加实现类
(1).把工具传递给实现接口类中属性 给接口实现类进行注入 (2). name: 接口实现类中的属性名
<!--创建bean--> <bean id="usermapperimpl" class="Com.Jsxs.Mapper.UserMapperImpl"> <property name="sqlSessionTemplate" ref="sqlSession"/> </bean>
(5).将自己写的实现类,注入到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 "> <!-- DateSource: 使用Spring的数据源替换MyBatis的配置 我们这里使用Spring-Jdbc依赖提供的类 : org.springframework.jdbc.datasource.DriverManagerDataSource --> <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&charEncoding=UTF8"/> <property name="username" value="root"/> <property name="password" value="121788"/> </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/Jsxs/Mapper/*.xml"/> </bean> <!-- SqlSessionTemplate: 就是我们需要用的sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!-- 只能利用构造器注入 sqlSessionFactory,因为他没有set方法--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!--创建bean--> <bean id="usermapperimpl" class="Com.Jsxs.Mapper.UserMapperImpl"> <property name="sqlSessionTemplate" ref="sqlSession"/> </bean> </beans>
(6).接口实现类
(1).设置qlSessionTemplate属性 private SqlSessionTemplate sqlSessionTemplate; (2).利用set注入 (3).进行工具的实现 UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class); List<User> userList = mapper.selectUser(); return userList;
package Com.Jsxs.Mapper; import Com.Jsxs.pojo.User; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; public class UserMapperImpl implements UserMapper{ // 原来,我们所有的操作,都是用sqlSession来执行,现在我们使用SqlSessionTemplate来执行 private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } @Override public List<User> selectUser() { UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class); List<User> userList = mapper.selectUser(); return userList; } }
(7).mybatis-config核心文件
mybatis核心配置文件几乎没用
(1).可以写别名 (2).可以写设置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <!--configuration核心配置文件--> <configuration> </configuration>
(8).测试类
import Com.Jsxs.Mapper.UserMapper; import Com.Jsxs.Mapper.UserMapperImpl; import Com.Jsxs.Utils.MybatisUtil; import Com.Jsxs.pojo.User; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.io.Resources; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class MyTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml"); UserMapper usermapper =(UserMapper) context.getBean("usermapperimpl"); List<User> userList = usermapper.selectUser(); for (User user : userList) { System.out.println(user); } } }
(10).提炼配置文件
spring-config
<?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 "> <!-- DateSource: 使用Spring的数据源替换MyBatis的配置 我们这里使用Spring-Jdbc依赖提供的类 : org.springframework.jdbc.datasource.DriverManagerDataSource --> <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&charEncoding=UTF8"/> <property name="username" value="root"/> <property name="password" value="121788"/> </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/Jsxs/Mapper/*.xml"/> </bean> <!-- SqlSessionTemplate: 就是我们需要用的sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!-- 只能利用构造器注入 sqlSessionFactory,因为他没有set方法--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> </beans>
applicationContext文件
<?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"/> <!--创建bean--> <bean id="usermapperimpl" class="Com.Jsxs.Mapper.UserMapperImpl"> <property name="sqlSessionTemplate" ref="sqlSession"/> </bean> </beans>
测试
import Com.Jsxs.Mapper.UserMapper; import Com.Jsxs.Mapper.UserMapperImpl; import Com.Jsxs.Utils.MybatisUtil; import Com.Jsxs.pojo.User; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.io.Resources; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class MyTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); UserMapper usermapper =(UserMapper) context.getBean("usermapperimpl"); List<User> userList = usermapper.selectUser(); for (User user : userList) { System.out.println(user); } } }
(11).小结
- 比mybatis多一个接口实现类
- 实现图
3.整合Mybatis方式二(没写工具类)
(1).接口实现类要继承SqlSessionDaoSupport
(1).继承 (2).实现父类的方法 getSqlSession()----》继承类的方法 (3).这个类SqlSessionDaoSupport,依赖自带的
package Com.Jsxs.Mapper; import Com.Jsxs.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(); } }
(2).mybatis-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 "> <!-- DateSource: 使用Spring的数据源替换MyBatis的配置 我们这里使用Spring-Jdbc依赖提供的类 : org.springframework.jdbc.datasource.DriverManagerDataSource --> <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&charEncoding=UTF8"/> <property name="username" value="root"/> <property name="password" value="121788"/> </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/Jsxs/Mapper/*.xml"/> </bean> <!-- <!– SqlSessionTemplate: 就是我们需要用的sqlSession–>--> <!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">--> <!-- <!– 只能利用构造器注入 sqlSessionFactory,因为他没有set方法–>--> <!-- <constructor-arg index="0" ref="sqlSessionFactory"/>--> <!-- </bean>--> </beans>
(3).configContext
以下文件写死
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<?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"/> <!-- 创建bean2--> <bean id="user2" class="Com.Jsxs.Mapper.UserMapperImpl2"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
(4).测试
import Com.Jsxs.Mapper.UserMapper; import Com.Jsxs.Mapper.UserMapperImpl; import Com.Jsxs.Utils.MybatisUtil; import Com.Jsxs.pojo.User; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.io.Resources; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class MyTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); UserMapper usermapper =(UserMapper) context.getBean("user2"); List<User> userList = usermapper.selectUser(); for (User user : userList) { System.out.println(user); } } }
(5).优点
- 可以在mybatis-spring中省略这个字段的创建
<!-- SqlSessionTemplate: 就是我们需要用的sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!-- 只能利用构造器注入 sqlSessionFactory,因为他没有set方法--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean>
- 文件可写死
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>