純注解版spring與mybatis的整合過程
今天給大家分享純注解版spring與mybatis的整合






mybatis包下:有這幾個(gè),上面圖片沒有展開
配置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");
//設(shè)置Mapper文件的路徑,這個(gè)只會(huì) 掃描一個(gè)Mapper文件,如果需要掃描多個(gè)文件,則需要讓他掃描包
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標(biāo)簽來寫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:存在數(shù)據(jù)耦合,可以通過配置文件來解耦合:
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());
//設(shè)置Mapper文件的路徑,這個(gè)只會(huì) 掃描一個(gè)Mapper文件,如果需要掃描多個(gè)文件,則需要讓他掃描包
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:


到此這篇關(guān)于基于純注解實(shí)現(xiàn)spring與mybatis的整合的文章就介紹到這了,更多相關(guān)spring與mybatis整合內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring整合Mybatis 掃描注解創(chuàng)建Bean報(bào)錯(cuò)的解決方案
- springboot整合mybatis-plus基于注解實(shí)現(xiàn)一對一(一對多)查詢功能
- SpringBoot整合Mybatis注解開發(fā)的實(shí)現(xiàn)代碼
- Spring Boot整合mybatis使用注解實(shí)現(xiàn)動(dòng)態(tài)Sql、參數(shù)傳遞等常用操作(實(shí)現(xiàn)方法)
- 詳解SpringBoot 快速整合Mybatis(去XML化+注解進(jìn)階)
- Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
- Spring與Mybatis基于注解整合Redis的方法
相關(guān)文章
關(guān)于@JSONField和@JsonFormat的使用區(qū)別說明
這篇文章主要介紹了關(guān)于@JSONField 和 @JsonFormat的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java多線程編程實(shí)戰(zhàn)之模擬大量數(shù)據(jù)同步
這篇文章主要介紹了Java多線程編程實(shí)戰(zhàn)之模擬大量數(shù)據(jù)同步,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
java虛擬機(jī)運(yùn)行時(shí)數(shù)據(jù)區(qū)分析
這篇文章主要介紹了java虛擬機(jī)運(yùn)行時(shí)數(shù)據(jù)區(qū)分析,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
springboot整合mybatis分頁攔截器的問題小結(jié)
springboot整合mybatis分頁攔截器,分頁攔截實(shí)際上就是獲取sql后將sql拼接limit,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-07-07
關(guān)于報(bào)錯(cuò)IDEA Terminated with exit code
如果在IDEA構(gòu)建項(xiàng)目時(shí)遇到下面這樣的報(bào)錯(cuò)IDEA Terminated with exit code 1,那必然是Maven的設(shè)置參數(shù)重置了,導(dǎo)致下載錯(cuò)誤引起的,本文給大家分享兩種解決方法,需要的朋友可以參考下2022-08-08
Java多線程實(shí)現(xiàn)簡易微信發(fā)紅包的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Java多線程實(shí)現(xiàn)簡易微信發(fā)紅包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
SpringBoot實(shí)現(xiàn)自定義線程池的方法
這篇文章主要介紹了SpringBoot中的自定義線程池解析,實(shí)現(xiàn)自定義線程池重寫spring默認(rèn)線程池的方式使用的時(shí)候,只需要加@Async注解就可以,不用去聲明線程池類,需要的朋友可以參考下2023-11-11

