mybatis-spring官方文档
:http://mybatis.org/spring/zh/index.html
首先新建一个空的maven项目
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.5version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.21version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.8.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.1.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.4version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.5version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.12version>
dependency>
最后为了防止maven配置文件无法被导出或生效
,加入以下代码
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>truefiltering>
resource>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>truefiltering>
resource>
resources>
build>
更详细过程可以看我之前的博客:第一个MyBatis程序
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSH=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="200024"/>
dataSource>
environment>
environments>
configuration>
package pojo;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String pwd;
}
package mapper;
import pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> getUser();
}
<mapper namespace="mapper.UserMapper">
<select id="getUser" resultType="pojo.User">
select * from mybatis.user
select>
mapper>
public class Test {
@org.junit.Test
public void test() throws IOException {
String resource = "mybatis-config.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.getUser();
for (User user : users) {
System.out.println(user);
}
}
}
在核心配置文件mybatis-config.xml
中加入以下代码
<mappers>
<mapper class="mapper.UserMapper"/>
mappers>
到此,mybatis程序的创建已成功,接下来将进行修改,整合spring
通过SqlSessionTemplate创建SqlSession
接下来将在上述实验环境下进行修改
<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">
beans>
在
spring配置文件
中加入以下代码,配置数据源信息
<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?useSSH=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="200024"/>
bean>
此时,就可以删除mybatis核心配置文件mybatis-config.xml
中的数据源配置
SqlSessionFactoryBuilder
来创建 SqlSessionFactory
SqlSessionFactoryBean
来创建在
spring配置文件
中加入以下代码,创建sqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
bean>
唯一的必要属性:用于 JDBC 的 DataSource
,注入上述创建好的数据源
还可以配置其他属性,完全取代mybatis-config.xml
中的配置
这里加入两个属性配置:
configLocation
:指定mybatis的xml配置文件路径mapperLocations
:注册Mapper.xm映射器
<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:mapper/*.xml"/>
bean>
这里,我们在spring配置文件
中注册了Mapper.xml
,就可以删除mybatis-config.xml
中的Mapper.xml
的注册
在mybatis中,SqlSession
提供了在数据库执行 SQL 命令所需的所有方法
而在spring-mybatis,没有SqlSession
了,而是SqlSessionTemplate
,这两个是同一个东西
在
spring配置文件
中加入以下代码,创建sqlSession
sqlSessionFactory
作为构造方法的参数来创建 SqlSessionTemplate
对象。
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
bean>
上一步我们创建了
sqlSession
对象,此时需要创建一个类来使用sqlSession
在该类中添加一个 SqlSession
属性,并添加 set方法
用于后续sqlSession的注入
public class UserMapperImpl implements UserMapper {
//原来所有操作都通过SqlSession来执行,现在都是SqlSessionTemplate
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<User> getUser() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getUser();
}
}
利用spring创建上述接口实现类对象,取名为
userMapper
,并注入上述创建好的sqlSession
对象
<bean id="userMapper" class="mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
bean>
此时,无需像先前一样创建sqlSessionFactory和sqlSession,我们只需获得创建好的
实体类对象
UserMapper,然后调用该对象的方法即可
public class Test {
@org.junit.Test
public void test() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapperImpl userMapper = (UserMapperImpl) context.getBean("userMapper");
List<User> users = userMapper.getUser();
for (User user : users) {
System.out.println(user);
}
}
}
运行成功!!!
通过SqlSessionDaoSupport获得Sqlsession
接下来将在上述实验环境下进行修改
新建一个接口实现类,继承SqlSessionDaoSupport
类,直接可以获得SqlSession
对象
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
public List<User> getUser() {
SqlSession sqlSession = getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getUser();
}
}
通过Spring来创建
在spring配置文件中
创建上述实体类对象userMapper2
,并设置sqlSessionFactory
属性为上述创建好的sqlSessionFactory
<bean id="userMapper2" class="mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
bean>
同样,无需像先前一样创建sqlSessionFactory和sqlSession,我们只需获得创建好的
实体类对象
UserMapper2,然后调用该对象的方法即可
public class Test {
@org.junit.Test
public void test() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
UserMapperImpl2 userMapper = (UserMapperImpl2) context.getBean("userMapper2");
List<User> users = userMapper.getUser();
for (User user : users) {
System.out.println(user);
}
}
}
成功运行!!
THE END…