目录
1.Spring集成MyBatis步骤
Step1:修改目录
Step2:生成可视数据库?
Step3:修改pom文件,添加相关依赖
Step4:创建MyBatis核心配置文件
Step5:创建jdbc.properties文件
Step6:创建Spring核心配置文件
Step7
Step8:测试
2.Spring接管MyBatis的众多功能
3.容易报错的位置
将MyBatis与Spring进行整合,主要解决的问题就是将SqlSessionFactory对象交由Spring来管理,所以该整合只需将SqlSessionFactory的对象生成器SqlSessionFactoryBean注册在Spring容器中,再让其注入给mapper的实现类即可完成整合
实现Spring与MyBatis的整合,常用的方式是扫描Mapper动态代理。Spring像插线板一样,mybatis框架是插头,可以容易的组合到一起。插线板spring插上mybatis,两个框架就是整体。
1.Spring集成MyBatis步骤
Step1:修改目录
Step2:生成可视数据库
?
?Step3:修改pom文件,添加相关依赖
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>pro1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.19</version>
</dependency>
<!-- spring-context 核心ioc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.19</version>
</dependency>
<!-- spring-aspects-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.19</version>
</dependency>
<!-- aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.19</version>
</dependency>
<!-- spring事务用到的-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.19</version>
</dependency>
<!-- mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!-- 阿里公司数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
Step4:创建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>
</configuration>
Step5:创建jdbc.properties文件
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=niit
?Step6:创建Spring核心配置文件
也可以将Spring核心配置文件拆分成
applicationContext_mapper和applicationContext_service
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 读取属性文件jdbc.properties 接管properties标签-->
<context:property-placeholder location="jdbc.properties"/>
<!-- 创建数据源 接管environments标签-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置SqlSessionFactoryBean类 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源-->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置MyBatis的核心配置文件-->
<property name="configLocation" value="mybatis-config.xml"></property>
<!-- 注册实体类别名-->
<property name="typeAliasesPackage" value="pojo"></property>
<!-- <property name="mapperLocations" value="mapper/UsersMapper.xml"></property>-->
</bean>
<!-- 注册mapper.xml文件 接管掉mybatis-config中的mapper标签
这个标签已经把mapper层中的对象创建好了,具体创建的什么对象?是在mybatis中的代理对象SqlSession
将代理对象创建的mapper对象用具体的mapper接口来接收
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"></property>
</bean>
</beans>
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--导入applicationContext_mapper.xml-->
<import resource="applicationContext_mapper.xml"></import>
<!--基于注解开发 添加包扫描-->
<context:component-scan base-package="service.impl"/>
<!-- 事务处理-->
</beans>
Step7
添加pojo包中的实体类
?
public class Users {
private Integer userId;
private String uName;
private String uPass;
public Users() {
}
public Users(Integer userId, String uName, String uPass) {
this.userId = userId;
this.uName = uName;
this.uPass = uPass;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
public String getuPass() {
return uPass;
}
public void setuPass(String uPass) {
this.uPass = uPass;
}
@Override
public String toString() {
return "Users{" +
"userId=" + userId +
", uName='" + uName + '\'' +
", uPass='" + uPass + '\'' +
'}';
}
}
添加mapper包中的接口和配置文件
public interface UsersMapper {
int insert(Users users);
}
<?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="mapper.UsersMapper">
<!-- int insert(Users users);-->
<!-- 注册了别名,凡是需要用到pojo.的地方 都可以省略pojo.-->
<insert id="insert" parameterType="users">
insert into users values (#{userId},#{uName},#{uPass})
</insert>
</mapper>
?
添加service包下的接口
?
public interface UsersService {
//增加用户
int insert(Users users);
}
添加impl包下的业务实现类
?
@Service //交给Spring去创建对象
public class UsersServiceImpl implements UsersService {
@Autowired //自动注入代理对象sqlSession获取的mapper接口对象
UsersMapper usersMapper;
@Override
public int insert(Users users) {
return usersMapper.insert(users);
}
}
Step8:测试
public class Test {
@org.junit.Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_service.xml");
UsersService userService = (UsersService) ac.getBean("usersServiceImpl");
int insert = userService.insert(new Users(1, "zz", "123"));
System.out.println(insert);
}
}
2.Spring接管MyBatis的众多功能
?Spring接管mybatis的一些功能,所以mybatis核心配置文件中的一些配置可以删去,但仍然要保留mybatis核心配置文件,因为mybatis中的一些功能是spring接管不了的,如分页插件,缓存处理等。在Spring集成MyBatis后,可以将mybatis中的许多配置信息写到Spring中,下面做一些替换。
(1)properties文件
在mybatis中
<properties resource="jdbc.properties"/>
在spring中
<context:property-placeholder location="jdbc.properties"/>
(2)数据源
在mybatis中
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
在spring中
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
(3)扫描mapper
在mybatis中
<mappers>
<package name="mapper"/>
</mappers>
在spring中
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"></property>
</bean>
3.容易报错的位置
?第一次整合时出现了好多错误,注意这些点。
1.jdbc.properties中的数据库名称
2.pom.xml中的jar包配置
3.spring核心文件的头信息
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
事务
88-101
动力节点2022最新spring框架教程-spring从入门到实战_哔哩哔哩_bilibili
|