在idea里创建一个spring-boot工程选依赖的时候把web里的web勾上,sql里的mysql和mybatis也勾上 ,这样就会自动帮我们生成依赖在其中我们需要给mysql的依赖加上版本,不然它会自动去用最新版的(目前mysql最新版已经到了八以上了),我们现在用五点几的就好,有错误也能在网上找到解决方法,采用最新版的如果出现错误,那么解决起来也麻烦
然后在加上druid依赖, 如果是要用到多数据源,就得用 druid-spring-boot-starter
然后在build里设置下配置文件的扫描路径,pom.xml具体依赖如下
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.liy</groupId>
<artifactId>mybatisProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatisProject</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>`
然后在application.properties配置文件中配置数据源信息(我这里打算用两个数据源)
spring.datasource.one.username=root
spring.datasource.one.password=root
spring.datasource.one.url=jdbc:mysql:///again
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.two.username=root
spring.datasource.two.password=root
spring.datasource.two.url=jdbc:mysql:///demo
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.driver-class-name=com.mysql.jdbc.Driver
如果只是一个数据源前缀是spring.datasource.username
这也是默认的扫描前缀
因为我们这里采用两个数据源,所以为了区别两个数据源我们分别给他们加上了spring.datasource.**one.**username=root
spring.datasource.**two.**username=root
但是我们把前缀改了,那么spring-boot也就找不到默认的前缀,那么这两个数据源的配置信息也就不会去扫描
所以我们得自己去写个配置类,来获取和区别这两个数据源DatasourceConfig.java
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.one")
DataSource dsOne(){
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.two")
DataSource dsTwo(){
return DruidDataSourceBuilder.create().build();
}
}
然后得把数据源分别弄给我们的mybatis配置类去写两个mybatis配置类,内容基本一样,但是用于区分两个数据源MybatisConfig1
@Configuration
@MapperScan(basePackages = "com.liy.mapper",sqlSessionFactoryRef = "sqlSessionFactoryBean1",
sqlSessionTemplateRef = "sqlSessionTemplate1")
public class MyBatisConfig1 {
@Resource(name = "dsOne")
DataSource dsOne;
@Bean
SqlSessionFactory sqlSessionFactoryBean1(){
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dsOne);
SqlSessionFactory factory = null;
try {
factory = factoryBean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return factory;
}
@Bean
SqlSessionTemplate sqlSessionTemplate1(){
return new SqlSessionTemplate(sqlSessionFactoryBean1());
}
}
MybatisConfig1
@Configuration
@MapperScan(basePackages = "com.liy.mapper2",sqlSessionFactoryRef = "sqlSessionFactoryBean2",
sqlSessionTemplateRef = "sqlSessionTemplate2")
public class MyBatisConfig2 {
@Resource(name = "dsTwo")
DataSource dsTwo;
@Bean
SqlSessionFactory sqlSessionFactoryBean2(){
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dsTwo);
SqlSessionFactory factory = null;
try {
factory = factoryBean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return factory;
}
@Bean
SqlSessionTemplate sqlSessionTemplate2(){
return new SqlSessionTemplate(sqlSessionFactoryBean2());
}
}
然后分别写两个mapper包里对应的接口和映射文件
我们就只测试一下,那就分别查询下
UserMapper 普通的sql语句也可以直接用注解来,不过不推荐
public interface UserMapper {
//@Select("select * from user")
public List<User> selectAll();
}
UserMapper.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.liy.mapper.UserMapper">
<select id="selectAll" resultType="com.liy.bean.User">
select * from user
</select>
</mapper>
另一个mapper2的代码也就不发了,也是基本和mapper里的一样
只是些路径和数据库查询的表名不太一样
记得写个bean类接收 , 我找了两个相似的表来查询,所有就只用写一个bean类来接收就行
setter和getter方法就省略了
public class User {
private int id;
private String name;
private int age;
private String address;
基本都准备好了,那么就开始测试吧
直接在spring-boot工程自带的测试类里测试即可
@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisApplicationTests {
@Autowired
UserMapper um;
@Autowired
UserMapper2 um2;
@Test
public void contextLoads() {
List<User> user = um.selectAll();
System.out.println(user);
List<User> users = um2.selectAll();
System.out.println(users);
}
}
um 和 um2上应该都有红色波浪线,只是springboot未找到还是啥的,但是还是注入进来了
两个表的数据也都查询出来了