Spring整合MyBatis
官网:http://mybatis.org/spring/zh/index.html
方式一:
1、导入相关的jar包
- junt
- mybatis
- 连接数据库
- 操作数据库
- spring相关的
- aop织入包
- lombok
- mybatis-spring整合包
注意版本:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--spring操作数据库的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<!--配置资源资源过滤-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
2、编写实体类、接口、接口配置文件
1、实体类
package com.star.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
2、接口
package com.star.mapper;
import com.star.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> getUserList();
public int addUser(User user);
public int deleteUser(int id);
}
3、接口配置文件
<?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.star.mapper.UserMapper">
<select id="getUserList" resultType="user">
select * from user;
</select>
<insert id="addUser" parameterType="user">
insert into user (id,name,pwd) values (#{id},#{name},#{pwd});
</insert>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id};
</delete>
</mapper>
3、编写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.star.pojo"/>
</typeAliases>
</configuration>
4、编写spring配置文件
1、配置文件的约束文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
2、数据整合
<!--选择jdbc包下的DriverManagerDataSource-->
<bean id="dateSources" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
3、注入sqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--导入数据源-->
<property name="dataSource" ref="dateSources"/>
<!--绑定mybatis-config.xml文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--扫描接口对应的配置文件-->
<property name="mapperLocations" value="classpath:com/star/mapper/*.xml"/>
</bean>
4、注入sqlsession
<!--使用构造器注入-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--index="0":给第一个参数赋值 将sqlSessionFactory赋值给参数 可以点进源码查看-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
5、编写接口的实现类来使用sqlSeeion
package com.star.mapper.impl;
import com.star.mapper.UserMapper;
import com.star.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class UserMapperImpl implements UserMapper {
//我们所有的操作,原来都使用sqlSession来执行,现在使用SqlSessionTemplate,作为 SqlSession 的一个实现,SqlSessionTemplate 是线程安全的,可以被多个 DAO 或映射器所共享使用。
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<User> getUserList() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
return mapper.getUserList();
}
public int addUser(User user) {
return 0;
}
public int deleteUser(int id) {
return 0;
}
}
6、将实现类中的sqlSession装配
<bean id="userMapper" class="com.star.mapper.impl.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
5、编写测试类进行测试
import com.star.mapper.UserMapper;
import com.star.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class MyTest {
@Test
public void getUserListTest2(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapperImpl = (UserMapper) context.getBean("userMapperImpl");
List<User> userList = userMapperImpl.getUserList();
for (User user : userList) {
System.out.println(user);
}
}
}
测试结果:
如果我们要进行增删改查,只需要改变sqlSession操作类中的mapperMethods方法中的代码,然后编写测试类就可以了!
方式二:
1、编写sqlSession类
package com.star.mapper.impl;
import com.star.mapper.UserMapper;
import com.star.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
public List<User> getUserList() {
SqlSession sqlSession = getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
return userList;
}
public int addUser(User user) {
return 0;
}
public int deleteUser(int id) {
return 0;
}
}
2、修改之前的spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!--数据整合-->
<!--选择jdbc包下的DriverManagerDataSource-->
<bean id="dateSources" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--注入sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dateSources"/>
<!--绑定mybatis-config.xml文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--<property name="typeAliasesPackage" value="com.star.pojo"/>-->
<property name="mapperLocations" value="classpath:com/star/mapper/*.xml"/>
</bean>
<bean id="userMapperImpl2" class="com.star.mapper.impl.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
3、编写测试类
@Test
public void getUserListTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapperImpl2 = (UserMapper) context.getBean("userMapperImpl2");
List<User> userList = userMapperImpl2.getUserList();
for (User user : userList) {
System.out.println(user);
}
}
测试结果:
相比之下,方式二比较简便,但是在刚开始学的时候,先使用方式一,熟练后再使用方式二!
还有:增删改操作需要开启事务,所以需要再Spring配置文件中配置事务,我们下篇再说!