MyBatis - 使用示例(二)整合Spring
在上文中简单的单独使用了MyBatis,但是在正在开发的环境中是不会那样做的,所以接下来为大家介绍如何整合Spring使用MyBatis。
一、MyBatis集成Spring使用
在 Spring 项目中,使用 MyBatis 的模板,请注意 Spring 的版本为5.2.10.RELEASE
,mybatis的版本为3.5.6
,spring-mybatis版本为2.0.6
(一)引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>code-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis-spring-demo</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.version>5.2.10.RELEASE</spring.version>
</properties>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!--spring整合mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
<scope>runtime</scope>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
</project>
(二)添加spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.tzh.mybatis"/>
<!-- 定义一个数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis-demo"/>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- Spring 和 MyBatis 整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!--让spring扫描mapper接口所在的包-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.tzh.mybatis.mapper"/>
</bean>
</beans>
(三)添加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>
<settings>
<!-- 全局映射器启用缓存 -->
<setting name="cacheEnabled" value="true"/>
<!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->
<setting name="multipleResultSetsEnabled" value="true"/>
<!-- 是否开启自动驼峰命名规则映射,数据库的A_COLUMN映射为Java中的aColumn -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- MyBatis利用本地缓存机制(Local Cache)防止循环引用(circular references)和加速重复嵌套查询 -->
<setting name="localCacheScope" value="STATEMENT"/>
</settings>
<!--设置别名-->
<typeAliases>
<package name="cn.tzh.mybatis.entity"/>
</typeAliases>
</configuration>
(四)创建UserMapper和映射mapper.xml
package cn.tzh.mybatis.mapper;
import cn.tzh.mybatis.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* @author tzh
* @date 2020/12/28 14:29
*/
@Mapper
public interface UserMapper {
/**
* 查询用户
*
* @param id id
* @return {@link User}
* @author tzh
* @date 2020/12/30
*/
User selectUser(@Param("id") Long id);
}
<?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="cn.tzh.mybatis.mapper.UserMapper">
<!--标识开起二级缓存-->
<cache/>
<!--查询语句-->
<select id="selectUser" resultType="User">
select *
from user
where id = #{id}
</select>
</mapper>
(五)创建User实体
package cn.tzh.mybatis.entity;
import lombok.Data;
import java.io.Serializable;
/**
* @author tzh
* @date 2020/12/28 14:32
*/
@Data
public class User implements Serializable {
private static final long serialVersionUID = 2296136623557481394L;
private Long id;
private String userName;
private Integer age;
private String phone;
private String desc;
}
(六)创建启动类
package cn.tzh.mybatis;
import cn.tzh.mybatis.entity.User;
import cn.tzh.mybatis.mapper.UserMapper;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author tzh
* @date 2020/12/31 11:59
*/
public class SpringTest {
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/spring-mybatis.xml");
UserMapper bean = applicationContext.getBean(UserMapper.class);
User user = bean.selectUser(1L);
System.out.println(user);
}
}
--------------最后感谢大家的阅读,愿大家技术越来越流弊!--------------
--------------也希望大家给我点支持,谢谢各位大佬了!!!--------------