Spring的学习____X. spring 和mybatis的整合
Posted xbfchder
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring的学习____X. spring 和mybatis的整合相关的知识,希望对你有一定的参考价值。
(本案例是idea下的maven工程)
案例一:mybatis的回顾:
整体流程的回顾:
mybatis: 1.实体类(User)----》属性名和数据库中字段名相对应 2.接口(UserMapper-----》对数据库的增删改查的业务操作的定义 3.接口的配置文件(UserMapper.xml)----》 mappers的namespace属性要写对应的接口; 再写UserMapper接口中增删改查的SQL语句 4.mybatis 的核心配置文件(mybatis-config.xml)--》 日志信息 别名 数据库源 driver,url,username,pwd mapper的映射器 5.测试类: 加载mybatis的核心配置文件 获得sqlSessionFactory对象通过sqlSessionFactoryBuilder 获得sqlSession通过sqlSessionFactory对象 获得实体类对象 sqlSession.getMapper(UserMapper.class)
1.pojo实体类:User
public class User private String name; private String pwd; private int id; public User() public User(String name, String pwd, int id) this.name = name; this.pwd = pwd; this.id = id; public String getName() return name; public void setName(String name) this.name = name; public String getPwd() return pwd; public void setPwd(String pwd) this.pwd = pwd; public int getId() return id; public void setId(int id) this.id = id; @Override public String toString() return "User" + "name=‘" + name + ‘\‘‘ + ", pwd=‘" + pwd + ‘\‘‘ + ", id=" + id + ‘‘;
2.接口 UserMappe
public interface UserMapper List<User> getUser();
3.接口的配置文件 UserMapper.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"> <mapper namespace="com.xbf.dao.UserMapper"> <select id="getUser" resultType="User"> select * from user </select> </mapper>
4.mybatis的核心配置文件:
<?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> <!--实体类的别名--> <typeAliases> <package name="com.xbf.pojo"/> </typeAliases> <!--数据源的配置--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="1234567890"/> </dataSource> </environment> </environments> <!--mapper映射器--> <mappers> <mapper resource="com/xbf/dao/UserMapper.xml"/> </mappers> </configuration>
5.测试类的编写:
public class Test @org.junit.Test public void test() throws IOException //1. String resource="mybatis-config.xml"; InputStream in = Resources.getResourceAsStream(resource); //2.获得sqlSessionFactory对象 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in); //3.获得sqlSession对象 开启事务管理 SqlSession sqlSession = sqlSessionFactory.openSession(true); //4.实体 mapper UserMapper mapper = sqlSession.getMapper(UserMapper.class); //5.执行方法 List<User> user = mapper.getUser(); System.out.println(user);
案例二:mybatis-spring的整合:
注意:需要在mybatis基础上再导3个包( spring-webmvc ,编织包(横向织入),spring-jdbc (spring数据库连接包),mybatis-spring(Spring-nybatis整合包)):
整体流程的概述:
spring:(过程总结) 1.编写pojo实体类(User) 2.定义接口(UserMapper)---》定义数据库的增删删除操作 3.定义接口的实现类---》 主要是为了sqlSession属性,好在Bean中实现注册 写全接口的增删改查操作 (利用sqlSession.getMapper()实现) 4.编写UserMapper.xml配置文件---》编写增删改查的sql语句 5.编写mybatis核心配置文件---》主要配置UserMapper.xml,数据源信息已不再其中 6.编写applicationContext.xml文件 6.1 数据源的配置; 6.2 通过SqlSessionFactoryBean 创建sqlSessionFactory对象, 并在Spring中配置mybatis核心配置文件 6.3 通过sqlSessionFactory对象创建sqlSession对象 6.4在接口的实现类(UserMapperImpl)加入sqlSession属性 6.5在Spring bean中进行实现类对象的注册 7.测试: 7.1加载 spring配置文件,获得conText对象 7.2获得实现的Bean对象 7.3调用实现类中的增删改查的方法
1.pojo实体类:User
public class User private String name; private String pwd; private int id; public User() public User(String name, String pwd, int id) this.name = name; this.pwd = pwd; this.id = id; public String getName() return name; public void setName(String name) this.name = name; public String getPwd() return pwd; public void setPwd(String pwd) this.pwd = pwd; public int getId() return id; public void setId(int id) this.id = id; @Override public String toString() return "User" + "name=‘" + name + ‘\‘‘ + ", pwd=‘" + pwd + ‘\‘‘ + ", id=" + id + ‘‘;
2.接口的定义:UserMapper
public class UserMapperImpl implements UserMapper //添加sqlSession属性 private SqlSession sqlSession; public void setSqlSession(SqlSession sqlSession) this.sqlSession = sqlSession; public List<User> getUser() UserMapper mapper = sqlSession.getMapper(UserMapper.class); return mapper.getUser();
3.接口实现类的编写:UserMapperImpl
public class UserMapperImpl implements UserMapper //添加sqlSession属性 private SqlSession sqlSession; public void setSqlSession(SqlSession sqlSession) this.sqlSession = sqlSession; public List<User> getUser() UserMapper mapper = sqlSession.getMapper(UserMapper.class); return mapper.getUser();
4.接口的配置文件:UserMapper.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"> <mapper namespace="com.xbf.dao.UserMapper"> <select id="getUser" resultType="User"> select * from user </select> </mapper>
5.mybatisde核心配置文件: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> <!--实体类的别名--> <typeAliases> <package name="com.xbf.pojo"/> </typeAliases> <!--mapper映射器--> <mappers> <mapper resource="com/xbf/dao/UserMapper.xml"/> </mappers> </configuration>
6.spring 的配置文件:applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--1.数据源信息--> <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?serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="1234567890"/> </bean> <!--2.创建sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--3.创建sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!--4.加入sqlSession属性--> <!--5.实体类对象的注入--> <bean id="user" class="com.xbf.dao.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> </bean> </beans>
7.测试类的编写:
public class Test @org.junit.Test public void test() //1.加载配置文件 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapperImpl user = (UserMapperImpl)context.getBean("user"); List<User> users = user.getUser(); for (User user1 : users) System.out.println(user1);
总结:
区别点:
1.数据源的位置不同;
2.spring整合中有了接口的实现类的编写(测试变的更简便);
以上是关于Spring的学习____X. spring 和mybatis的整合的主要内容,如果未能解决你的问题,请参考以下文章