学完了Spring的JavaConfiguration配置形式,我们就可以进行SpringBoot的配置了。
在
Spring-Java配置形式讲解
文章中介绍了,SpringBoot的配置形式多数为JavaConfiguration的形式
这里我将为大家讲解SpringBoot如何整合MyBatis
严格来说SpringBoot并不是一个框架,精炼的来说SpringBoot是一个入口,Spring全家桶的入口,而这个家族每个人各司其职,每个进来的人都会被事先配置好基本的一些配置,比如扫地的要配个扫把。 - -
好了,废话少说,我们进入正题。 SpringBoot整合Mybatis的思路还是以前的思路,我们依旧采用MyBatis的自动扫描mapper包的形式进行配置。 不懂流程的童鞋,请翻看之前的文章
MyBatis与Spring整合
整合思路:
弃用xml配置文件的形式,改用JavaConfiguration的形式进行配置。再强调一遍,思路采用Spring整合Mybatis的地上那种方案,不懂流程的童鞋,请翻看之前的文章
MyBatis与Spring整合
文档结构:
一下文件,使用的是 Mybats的入门案例(二)的内容
│ pom.xml
│
└─src
├─main
│ ├─java
│ │ └─com
│ │ └─lifeibai
│ │ ├─conf
│ │ │ DbConfig.java ----数据库连接配置
│ │ │ MyBatisConfig.java ---MyBatis的配置操作
│ │ │
│ │ ├─mapper
│ │ │ OrdersMapper.java
│ │ │ UserMapper.java
│ │ │
│ │ ├─po
│ │ │ Orders.java
│ │ │ User.java
│ │ │
│ │ └─springboot
│ │ └─mybatis_demo
│ │ MybatisDemoApplication.java --springboot核心入口
│ │
│ └─resources
│ │ application.properties
│ │ mybatis-conf.xml
│ │
│ └─com
│ └─lifeibai
│ └─mapper
│ OrdersMapper.xml
│ UserMapper.xml
│
└─test
└─java
└─com
└─lifeibai
└─springboot
└─mybatis_demo
MybatisDemoApplicationTests.java
pom.xml
?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"
modelVersion4.0.0/modelVersion
groupIdcom.lifebai.springboot/groupId
artifactIdmybatis_demo/artifactId
version0.0.1-SNAPSHOT/version
packagingjar/packaging
namemybatis_demo/name
descriptionDemo project for Spring Boot/description
parent
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-parent/artifactId
version2.0.2.RELEASE/version
relativePath/ !-- lookup parent from repository --
/parent
properties
project.build.sourceEncodingUTF-8/project.build.sourceEncoding
project.reporting.outputEncodingUTF-8/project.reporting.outputEncoding
java.version1.8/java.version
!-- 集中定义依赖版本号 --
junit.version4.12/junit.version
spring.version5.0.0.RELEASE/spring.version
servlet-api.version2.5/servlet-api.version
mybatis.version3.2.8/mybatis.version
mybatis.spring.version1.2.2/mybatis.spring.version
mysql.version5.1.32/mysql.version
druid.version1.0.9/druid.version
/properties
dependencies
!--对jdbc的支持,配置Mybatis的时候需要用到--
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-jdbc/artifactId
/dependency
!--测试相关--
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-test/artifactId
scopetest/scope
/dependency
!-- Mybatis --
dependency
groupIdorg.mybatis/groupId
artifactIdmybatis/artifactId
version${mybatis.version}/version
/dependency
!--Spring整合 MyBatis整合包--
dependency
groupIdorg.mybatis/groupId
artifactIdmybatis-spring/artifactId
version${mybatis.spring.version}/version
/dependency
!-- MySql --
dependency
groupIdmysql/groupId
artifactIdmysql-connector-java/artifactId
version${mysql.version}/version
/dependency
!-- 德鲁伊连接池 --
dependency
groupIdcom.alibaba/groupId
artifactIddruid/artifactId
version${druid.version}/version
/dependency
/dependencies
/project
1,配置数据库连接池-DbConfig.java
这个注入的形式有很多种,大家任选一种
//第一种
package com.lifeibai.conf;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DbConfig {
@Value("${spring.datasource.url}")
private String jdbcUrl;
@Value("${spring.datasource.driver-class-name}")
private String jdbcDriverClassName;
@Value("${spring.datasource.username}")
private String jdbcUsername;
@Value("${spring.datasource.password}")
private String jdbcPassword;
@Bean(destroyMethod = "close")
public DataSource dataSource() {
DruidDataSource boneCPDataSource = new DruidDataSource();
// 数据库驱动
boneCPDataSource.setDriverClassName(jdbcDriverClassName);
// 相应驱动的jdbcUrl
boneCPDataSource.setUrl(jdbcUrl);
// 数据库的用户名
boneCPDataSource.setUsername(jdbcUsername);
// 数据库的密码
boneCPDataSource.setPassword(jdbcUsername);
return boneCPDataSource;
}
}
//第二种
@Configuration
public class ApplicationConfig {
@Autowired
private Environment env;
@Bean
public DataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}
}
//第三种
@ConfigurationProperties(prefix="spring.datasource")
public class ApplicationConfig {
private String url;
private String username;
private String password;
@Bean
public DataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);// 用户名
dataSource.setPassword(password);// 密码
return dataSource;
}
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;
}
}
2,配置sqlSessionFactory工厂 与 Mapper映射 -MyBatisConfig.java
package com.lifeibai.conf;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import javax.sql.DataSource;
@Configuration
public class MyBatisConfig {
/**
* sqlSessionFactory工厂
*/
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){//这里需要注意,只能用参数的形式注入
System.out.println(dataSource);
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
/*配置连接池*/
sqlSessionFactoryBean.setDataSource(dataSource);
/*加载去哪聚配置文件*/
Resource conf = new ClassPathResource("mybatis-conf.xml");
sqlSessionFactoryBean.setConfigLocation(conf);
return sqlSessionFactoryBean;
}
/**
* 配置mapper映射
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.lifeibai.mapper");
return mapperScannerConfigurer;
}
}
3,将Java配置类被Spring扫描到 - MybatisDemoApplication.java
package com.lifeibai.springboot.mybatis_demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
@ComponentScan("com.lifeibai")
public class MybatisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisDemoApplication.class, args);
}
}
4,测试 MybatisDemoApplicationTests.java
package com.lifeibai.springboot.mybatis_demo;
import com.lifeibai.mapper.UserMapper;
import com.lifeibai.po.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.sql.SQLException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisDemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void test01() throws SQLException{
User user = userMapper.selectByPrimaryKey(1);
System.out.println(user);
}
}
//测试结果
User{id=1, username='王五', birthday=null, sex='2', address='null'}
文章如有错误,请您一定指出,感谢之至!
如果你有不同的见解,欢迎留言,或者加我QQ986320270
图片可能来源于网络,如有侵权请告知。
最后:关注一下呗
长按二维码识别关注