天天看点

springboot+mybatis多资源配置第一步:创建springboot项目第二步:导入依赖第三步:书写配置文件第四步:书写配置类第五步:创建实体类第六步:书写Controller层第七步: 书写Mapper层第八步:书写启动类总结:

第一步:创建springboot项目

springboot+mybatis多资源配置第一步:创建springboot项目第二步:导入依赖第三步:书写配置文件第四步:书写配置类第五步:创建实体类第六步:书写Controller层第七步: 书写Mapper层第八步:书写启动类总结:

第二步:导入依赖

<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.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.4</version>
        </dependency>
    </dependencies>

    <build>
    <!-- 由于*Mapper.xml在java类包下,需要放行-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
           

第三步:书写配置文件

spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.one.username=root
spring.datasource.one.url=jdbc:mysql:///chapter05-1
spring.datasource.one.password=1234

spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.two.username=root
spring.datasource.two.url=jdbc:mysql:///chapter05-2
spring.datasource.two.password=1234
           

第四步:书写配置类

@Configuration
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties("spring.datasource.one")
    public DataSource dataSourceOne(){
        return new DruidDataSourceBuilder().build();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.two")//对应配置文件的前缀部分
    public DataSource dataSourceTwo(){
        return new DruidDataSourceBuilder().build();
    }
}
           

代码解释:@ConfigurationProperties(“spring.datasource.one”);读取的配置文件的前缀部分

@Configuration
@MapperScan(value = "com.zhy.demo54202.mapper01",sqlSessionFactoryRef = "sqlSessionFactoryBeanOne")
public class MybatisOneConfig {
    @Autowired
    @Qualifier("dataSourceOne")//指定的数据源在spring容器中的名称
    private DataSource dataSourceOne;
    @Bean
    public SqlSessionFactory sqlSessionFactoryBeanOne() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSourceOne);
        return sqlSessionFactoryBean.getObject();
    }
    @Bean
    public SqlSessionTemplate sqlSessionTemplateOne() throws Exception {
        SqlSessionTemplate sqlSessionTemplateOne = new SqlSessionTemplate(sqlSessionFactoryBeanOne());
        return sqlSessionTemplateOne;
    }
}
           
@Configuration
@MapperScan(value = "com.zhy.demo54202.mapper02",sqlSessionFactoryRef = "sqlSessionFactoryBeanTwo")
public class MybatisTwoConfig {
    @Autowired
    @Qualifier("dataSourceTwo")
    private DataSource dataSourceTwo;
    @Bean
    public SqlSessionFactory sqlSessionFactoryBeanTwo() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSourceTwo);
        return sqlSessionFactoryBean.getObject();
    }
    @Bean
    public SqlSessionTemplate sqlSessionTemplateTwo() throws Exception {
        SqlSessionTemplate sqlSessionTemplateTwo = new SqlSessionTemplate(sqlSessionFactoryBeanTwo());
        return sqlSessionTemplateTwo;
    }
}
           

代码解释:value = “com.zhy.demo54202.mapper02”;为mapper接口所对应的路径

sqlSessionFactoryRef = “sqlSessionFactoryBeanTwo”;为引用的对象

第五步:创建实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Book {
    private Integer id;
    private String name;
    private String author;
}

           

第六步:书写Controller层

@RestController
public class MyController {
    @Autowired
    private BookMapperOne bookMapperOne;
    @Autowired
    private BookMapperTwo bookMapperTwo;
    @RequestMapping("/test")
    public String demo(){
        List<Book> allBooks = bookMapperOne.getAllBooks();
        List<Book> allBookss =bookMapperTwo.getAllBooks();
        System.out.println(allBookss.toString()+allBookss.toString());
        return allBooks.toString()+allBookss.toString();
    }
}
           

第七步: 书写Mapper层

@Repository
public interface BookMapperOne {
    List<Book> getAllBooks();
}
           
<?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.zhy.demo54202.mapper01.BookMapperOne">
    <select id="getAllBooks" resultType="com.zhy.demo54202.pojo.Book">
        select *
        from book;
    </select>
</mapper>
           
@Repository
public interface BookMapperTwo {
    List<Book> getAllBooks();
}
           
<?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.zhy.demo54202.mapper02.BookMapperTwo">
    <select id="getAllBooks" resultType="com.zhy.demo54202.pojo.Book">
        select * from book;
    </select>
</mapper>
           

代码解释:注意namespace和*号尽量不要使用,这里使用只是做演示;

第八步:书写启动类

@SpringBootApplication
public class Demo54202Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo54202Application.class, args);
    }
}
           

总结:

第一步创建springboot项目导入相关依赖,导入依赖时,记得加上资源放行;

第二步:书写配置文件和配置类,书写配置文件时,由于我们使用多数据源,则需要制定二个URL地址;书写配置类主要为二大类:第一个为DataSourceConfig数据源配置类,第二个为:MybatisConfig持久层配置,其中持久层需要SqlSessionFactory和SqlSessionTemplate二个对象;

第三步:书写实体类和mapper层;

第四步:书写controller层和启动类,由于在配置类中扫描过接口所在层,所以不需要在启动类上再次扫描