wei_shuo的个人主页
wei_shuo的学习社区
Hello World !
步骤:
导入相关jar包
- junit
- mybatis
- mysql
- spring
- aop织入
- mybatis-spring
环境搭建:
<dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>8.0.28version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.5.2version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>5.2.0.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>5.1.9.RELEASEversion> dependency> <dependency> <groupId>org.aspectjgroupId> <artifactId>aspectjweaverartifactId> <version>1.9.4version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>2.0.2version> dependency> dependencies>
- 编写实体类
package com.wei.pojo; import lombok.Data; @Data public class User { private int id; private String name; private String pwd; }
- 编写核心配置文件
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="logImpl" value="LOG4J"/> settings> <typeAliases> <package name="com.wei.pojo.User"/> typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> dataSource> environment> environments> <mappers> <mapper class="com.wei.Mapper.UserMapper"/> mappers> configuration>
- 编写接口
package com.wei.Mapper; import com.wei.pojo.User; import java.util.List; public interface UserMapper { public List<User> selectUser(); }
- 编写Mapper映射文件
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wei.Mapper.UserMapper"> <select id="selectUser" resultType="com.wei.pojo.User"> select * from mybatis.user; select> mapper>
- 测试
import com.wei.Mapper.UserMapper; import com.wei.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 java.io.IOException; import java.io.InputStream; import java.util.List; public class MyTest { @Test public void test() throws IOException { String resources = "mybatis-config.xml"; //读取mybatis-config.xml主配置文件 InputStream in = Resources.getResourceAsStream(resources); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in); SqlSession sqlSession = sessionFactory.openSession(true); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.selectUser(); for (User user : userList) { System.out.println(user); } sqlSession.close(); } }
- 要和 Spring 一起使用 MyBatis,需要在 Spring 应用上下文中定义至少两样东西:一个
SqlSessionFactory
和至少一个数据映射器类。- 在 MyBatis-Spring 中,可使用
SqlSessionFactoryBean
来创建SqlSessionFactory
。 要配置这个工厂 bean,只需要把下面代码放在 Spring 的 XML 配置文件中- 在基础的 MyBatis 用法中,是通过
SqlSessionFactoryBuilder
来创建SqlSessionFactory
的。而在 MyBatis-Spring 中,则使用SqlSessionFactoryBean
来创建
- 编写UserMapper接口类
package com.wei.Mapper; import com.wei.pojo.User; import java.util.List; public interface UserMapper { public List<User> selectUser(); }
- 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"> <!--namespace=绑定一个对应的Dao/Mapper接口--> <mapper namespace="com.wei.Mapper.UserMapper"> <!--select查询语句查询全部用户--> <select id="selectUser" resultType="com.wei.pojo.User"> select * from mybatis.user; </select> </mapper>
- UserMapperImpl实现类,接口增加实现类
package com.wei.Mapper; import com.wei.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(); } }
- User接口
package com.wei.pojo; import lombok.Data; @Data public class User { private int id; private String name; private String pwd; }
- Mybatis-config.xml核心配置文件
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="logImpl" value="LOG4J"/> settings> <typeAliases> <package name="com.wei.pojo"/> typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> dataSource> environment> environments> configuration>
- log4j.properties资源包
log4j.properties #将等级为DEBUG的日志信息输出到console和file两个目的地 log4j.rootLogger=DEBUG,console,file #控制台输出的相关设置 log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.Target=System.out log4j.appender.console.Threshold=DEBUG log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=【%c】-%m%n #文件输出的相关配置 log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/wei.log log4j.appender.file.MaxFileSize=10mb log4j.appender.file.Threshold=DEBUG log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=【%p】[%d{yy-MM-dd}【%c】%m%n #日志输出级别 log4j.logger.org.mybatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
- spring-dao.xml(将sqlSessionFactory等bean注入到spring框架中,来管理持久层中的操作)
<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"> <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?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/wei/Mapper/*.xml"/> bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> bean> <bean id="userMpaaer" class="com.wei.Mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> bean> beans>
- ApplicationContext.xml(配置Spring框架所需的信息)
<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 id="userMpaaer" class="com.wei.Mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> bean> beans>
- 测试
import com.wei.Mapper.UserMapper; import com.wei.pojo.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class MyTest { @Test public void test() throws IOException { //解析beans.xml文件,生成管理相应的Bean对象 ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); // UserMapper userMpaaer = context.getBean("userMpaaer", UserMapper.class); UserMapper userMpaaer = (UserMapper) context.getBean("userMpaaer"); for (User user: userMpaaer.selectUser()){ System.out.println(user); } } }
SqlSessionTemplate
是 MyBatis-Spring 的核心。作为SqlSession
的一个实现,这意味着可以使用它无缝代替你代码中已经在使用的SqlSession
。SqlSessionTemplate
是线程安全的,可以被多个 DAO 或映射器所共享使用
- 使用
SqlSessionFactory
作为构造方法的参数来创建SqlSessionTemplate
对象<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> bean>
@Configuration public class MyBatisConfig { @Bean public SqlSessionTemplate sqlSession() throws Exception { return new SqlSessionTemplate(sqlSessionFactory()); } }
- 现在,这个 bean 就可以直接注入到你的 DAO bean 中了。你需要在你的 bean 中添加一个 SqlSession 属性,就像下面这样:
public class UserDaoImpl implements UserDao { private SqlSession sqlSession; public void setSqlSession(SqlSession sqlSession) { this.sqlSession = sqlSession; } public User getUser(String userId) { return sqlSession.selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId); } }
- 注入Spring:按下面这样,注入
SqlSessionTemplate
<bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl"> <property name="sqlSession" ref="sqlSession" /> bean>
SqlSessionTemplate
还有一个接收ExecutorType
参数的构造方法。这允许你使用如下 Spring 配置来批量创建对象,例如批量创建一些 SqlSession:<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> <constructor-arg index="1" value="BATCH" /> bean>
@Configuration public class MyBatisConfig { @Bean public SqlSessionTemplate sqlSession() throws Exception { return new SqlSessionTemplate(sqlSessionFactory(), ExecutorType.BATCH); } }
- 现在所有的映射语句可以进行批量操作了,可以在 DAO 中编写如下的代码
public class UserService { private final SqlSession sqlSession; public UserService(SqlSession sqlSession) { this.sqlSession = sqlSession; } public void insertUsers(List<User> users) { for (User user : users) { sqlSession.insert("org.mybatis.spring.sample.mapper.UserMapper.insertUser", user); } } }
注意,只需要在希望语句执行的方法与
SqlSessionTemplate
中的默认设置不同时使用这种配置。这种配置的弊端在于,当调用这个方法时,不能存在使用不同
ExecutorType
的进行中的事务。要么确保对不同ExecutorType
的SqlSessionTemplate
的调用处在不同的事务中,要么完全不使用事务
- Spring-dao.xml中
<property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/wei/Mapper/*.xml"/>
- configLocation :即mybatis-config.xml核心配置文件
当你需要使用mybatis-config.xml 配置文件的时候你就需要配置config-location,config-location的作用是确定mybatis-config.xml文件位置
- mapperLocations:dao接口类的映射文件
mapper-locations是用来注册所写的xxxmapper.xml映射文件
- 创建User类
package com.wei.pojo; import lombok.Data; @Data public class User { private int id; private String name; private String pwd; }
- UserMapper接口
package com.wei.Mapper; import com.wei.pojo.User; import java.util.List; public interface UserMapper { public List<User> selectUser(); }
- UserMapper.xml映射文件
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wei.Mapper.UserMapper"> <select id="selectUser" resultType="com.wei.pojo.User"> select * from mybatis.user; select> mapper>
- Spring-dao.xml(配置、整合Mybatis)
<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"> <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?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/wei/Mapper/*.xml"/> bean> beans>
- UserMapperImpl实现类,注入到spring中(applicationContext.xml)
package com.wei.Mapper; import com.wei.pojo.User; import org.mybatis.spring.support.SqlSessionDaoSupport; import java.util.List; public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{ @Override public List<User> selectUser() { // SqlSession sqlSession = getSqlSession(); // UserMapper mapper = sqlSession.getMapper(UserMapper.class); return getSqlSession().getMapper(UserMapper.class).selectUser(); } }
- applicationContext.xml(配置Spring框架所需的信息)
<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 id="userMpaaer" class="com.wei.Mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> bean> <bean id="userMapper2" class="com.wei.Mapper.UserMapperImpl2"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> bean> beans>
- MyTest测试类
import com.wei.Mapper.UserMapper; import com.wei.pojo.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class MyTest { @Test public void test() throws IOException { //解析beans.xml文件,生成管理相应的Bean对象 ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); // UserMapper userMpaaer = context.getBean("userMpaaer", UserMapper.class); UserMapper userMpaaer = (UserMapper) context.getBean("userMpaaer"); for (User user: userMpaaer.selectUser()){ System.out.println(user); } } }
SqlSessionDaoSupport
是一个抽象的支持类,用来为你提供SqlSession
。调用getSqlSession()
方法你会得到一个SqlSessionTemplate
,之后可以用于执行 SQL 方法,就像下面这样:public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { public User getUser(String userId) { return getSqlSession().selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId); } }
在这个类里面,通常更倾向于使用
MapperFactoryBean
,因为它不需要额外的代码。但是,如果你需要在 DAO 中做其它非 MyBatis 的工作或需要一个非抽象的实现类,那么这个类就很有用了。
SqlSessionDaoSupport
需要通过属性设置一个sqlSessionFactory
或SqlSessionTemplate
。如果两个属性都被设置了,那么SqlSessionFactory
将被忽略。假设类
UserMapperImpl
是SqlSessionDaoSupport
的子类,可以编写如下的 Spring 配置来执行设置:<bean id="userDao" class="org.mybatis.spring.sample.dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> bean>
结语:创作不易,如果觉得博主的文章赏心悦目,还请——
点赞
收藏
⭐️评论
冲冲冲