纯注解版spring与mybatis的整合过程
作者:dengfengling999 时间:2022-12-14 00:25:06
今天给大家分享纯注解版spring与mybatis的整合
mybatis包下:有这几个,上面图片没有展开
配置Bean:MyBatisAutoConfiguration
User
UserDAO
MybatisProperties
MyBatisAutoConfiguration:
package com.baizhiedu.mybatis;
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import javax.sql.DataSource;
import java.io.IOException;
@Configuration
@ComponentScan(basePackages = "com.baizhiedu.mybatis")
@MapperScan(basePackages = "com.baizhiedu.mybatis") //扫描dao接口对象
public class MyBatisAutoConfiguration {
//连接池对象
@Bean
public DataSource dataSource(){
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&charaterEcoding=utf-8");
dataSource.setUsername("root");
dataSource.setPassword("123456");
return dataSource;
}
//工厂对象
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage("com.baizhiedu.mybatis");
//设置Mapper文件的路径,这个只会 扫描一个Mapper文件,如果需要扫描多个文件,则需要让他扫描包
sqlSessionFactoryBean.setMapperLocations(new ClassPathResource("UserDAOMapper.xml"));
//通配写法让他扫描包下的所有Mapper文件资源
try {
ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();
Resource[] resources=resolver.getResources("com.baizhiedu.mapper/*Mapper.xml");
sqlSessionFactoryBean.setMapperLocations(resources);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSessionFactoryBean;
}
}
User:
package com.baizhiedu.mybatis;
public class User {
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserDAO:
package com.baizhiedu.mybatis;
public interface UserDAO {
public void save(User user);
}
UserDAOMapper.xml:
<?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.baizhiedu.mybatis.UserDAO">
<!--使用insert,update,delete,select标签来写sql语句-->
<insert id="save" parameterType="User">
insert into tb_users (name,password) values (#{name},#{password})
</insert>
</mapper>
TestMyBatis:
package com.baizhiedu;
import com.baizhiedu.mybatis.MyBatisAutoConfiguration;
import com.baizhiedu.mybatis.User;
import com.baizhiedu.mybatis.UserDAO;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMyBatis {
//用于测试Spring+Mybatis注解的整合
@Test
public void test1(){
ApplicationContext ctx=new AnnotationConfigApplicationContext(MyBatisAutoConfiguration.class);
UserDAO userDAO=(UserDAO)ctx.getBean("userDAO");
User user=new User();
user.setName("annotation1");
user.setPassword("123456");
userDAO.save(user);
}
}
上面配置Bean:MyBatisAutoConfiguration:存在数据耦合,可以通过配置文件来解耦合:
mybatis.properties:
mybatis.driverClassName=com.mysql.jdbc.Driver
mybatis.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&charaterEcoding=utf-8
mybatis.username=root
mybatis.password=123456
mybatis.typeAliasesPackages=com.baizhiedu.mybatis
mybatis.mapperLocations=com.baizhiedu.mapper/*Mapper.xml
定义类来封装配置文件中的配置信息:MybatisProperties:
package com.baizhiedu.mybatis;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:mybatis.properties") //读取配置文件
public class MybatisProperties {
@Value("${mybatis.driverClassName}")
private String driverClassName;
@Value("${mybatis.url}")
private String url;
@Value("${mybatis.username}")
private String username;
@Value("${mybatis.password}")
private String password;
@Value("${mybatis.typeAliasesPackages}")
private String typeAliasesPackages;
@Value("${mybatis.mapperLocations}")
private String mapperLocations;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTypeAliasesPackages() {
return typeAliasesPackages;
}
public void setTypeAliasesPackages(String typeAliasesPackages) {
this.typeAliasesPackages = typeAliasesPackages;
}
public String getMapperLocations() {
return mapperLocations;
}
public void setMapperLocations(String mapperLocations) {
this.mapperLocations = mapperLocations;
}
}
更改配置MyBatisAutoConfiguration:
package com.baizhiedu.mybatis;
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import javax.sql.DataSource;
import java.io.IOException;
@Configuration
@ComponentScan(basePackages = "com.baizhiedu.mybatis")
@MapperScan(basePackages = "com.baizhiedu.mybatis") //扫描dao接口对象
public class MyBatisAutoConfiguration {
@Autowired //注解 自定义类型注入,用来解耦合
private MybatisProperties mybatisProperties;
//连接池对象
@Bean
public DataSource dataSource(){
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName(mybatisProperties.getDriverClassName());
dataSource.setUrl(mybatisProperties.getUrl());
dataSource.setUsername(mybatisProperties.getUsername());
dataSource.setPassword(mybatisProperties.getPassword());
return dataSource;
}
//工厂对象
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackages());
//设置Mapper文件的路径,这个只会 扫描一个Mapper文件,如果需要扫描多个文件,则需要让他扫描包
sqlSessionFactoryBean.setMapperLocations(new ClassPathResource("UserDAOMapper.xml"));
//通配写法让他扫描包下的所有Mapper文件资源
try {
ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();
Resource[] resources=resolver.getResources(mybatisProperties.getMapperLocations());
sqlSessionFactoryBean.setMapperLocations(resources);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSessionFactoryBean;
}
}
TestMyBatis:test:
来源:https://blog.csdn.net/dengfengling999/article/details/125053938
标签:spring,mybatis,整合,注解
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java NIO实现聊天系统
2023-08-08 08:35:36
![](https://img.aspxhome.com/file/2023/5/88165_0s.jpg)
详解C#多线程编程之进程与线程
2021-07-18 14:15:36
![](https://img.aspxhome.com/file/2023/9/85669_0s.png)
GC调优实战之过早提升Premature Promotion
2023-10-22 10:07:55
![](https://img.aspxhome.com/file/2023/3/111453_0s.png)
游戏开发之随机概率的选择算法
2022-08-26 13:21:09
android studio 3.6.1导入项目报错提示无法下载classpath里的内容
2021-09-28 11:02:57
SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)推荐
2023-01-21 13:58:45
Android对话框使用方法详解
2023-11-09 03:37:51
![](https://img.aspxhome.com/file/2023/1/130421_0s.png)
Springboot 项目读取Resources目录下的文件(推荐)
2023-07-27 09:40:47
![](https://img.aspxhome.com/file/2023/9/57699_0s.png)
Android编程开发之Spinner组件用法
2022-09-02 17:55:26
深入解析Java的设计模式编程中建造者模式的运用
2022-08-10 23:31:22
![](https://img.aspxhome.com/file/2023/5/76315_0s.jpg)
详解JAVA流程控制语句
2023-11-05 02:27:09
![](https://img.aspxhome.com/file/2023/1/58711_0s.png)
PowerShell 定时执行.Net(C#)程序的方法
2023-07-09 14:10:39
C#控制台带参数程序源码编写实例讲解
2022-11-25 21:30:55
Python调用Java可执行jar包问题
2021-06-02 10:26:26
![](https://img.aspxhome.com/file/2023/9/102899_0s.png)
Android图片翻转动画简易实现代码
2023-03-12 02:33:35
eclipse 中的javac命令与java命令
2023-08-19 14:16:57
![](https://img.aspxhome.com/file/2023/2/67622_0s.png)
Android通过实现GridView的横向滚动实现仿京东秒杀效果
2023-06-08 22:53:46
![](https://img.aspxhome.com/file/2023/7/138057_0s.png)
java使用poi生成excel的步骤
2021-08-25 07:03:45
Java并发编程之Fork/Join框架的理解
2023-09-30 14:14:01
![](https://img.aspxhome.com/file/2023/9/75509_0s.png)
输出的文本实现对齐的方法(超简单)
2023-06-19 03:57:09
![](https://img.aspxhome.com/file/2023/1/66581_0s.jpg)