在idea中使用mybatis-generator之二:springboot集成mybatis测试成果

       前一篇博使用mybatis-generator自动生成代码,这篇来测试一下生成的代码的可用性,顺便聊一下springboot集成mybatis。没有什么新鲜,只是为自己做笔记。话不多说,直接上代码。

1. 在pom.xml文件中引入相关的依赖

这里我使用了druid作为数据源。在pom添加依赖,代码如下:

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>

2. 在application.properties配置文件中添加相关配置

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/demo001?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 连接等待超时时间
spring.datasource.maxWait=60000
# 配置隔多久进行一次检测(检测可以关闭的空闲连接)
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置连接在池中的最小生存时间
spring.datasource.minEvictableIdleTimeMillis=300000
# SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前.如果指定,则查询必须是一个SQL SELECT并且必须返回至少一行记录
spring.datasource.validationQuery=SELECT 1 FROM DUAL
# 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串
spring.datasource.testWhileIdle=true
# 指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个.注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串
spring.datasource.testOnBorrow=false
# 指明是否在归还到池中前进行检验注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
#spring.datasource.filters=stat,wall,log4j
#spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

3. 添加数据库配置

使用的springboot我不太喜欢使用配置文和xml文件来配置文件,个人比较喜欢使用注解+包扫描的方式来配置,这样代码看起来比较简洁,实现的功能都是相同的。代码如下:

在idea中使用mybatis-generator之二:springboot集成mybatis测试成果

 

package com.example.demo001.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author 
 * @date 2018/11/5 20:30
 * @description
 */
@Configuration
@MapperScan("com.example.demo001.**.mapper")
public class Demo001Config {
    @Value("${spring.datasource.type}")
    private Class<? extends DataSource> dataSourceType;

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource(){
        return DataSourceBuilder.create().type(dataSourceType).build();
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setDataSource(dataSource());
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/**/*Mapper.xml"));
        return sqlSessionFactoryBean.getObject();
    }
}

4.编写service层代码

Demo001Service.java

在idea中使用mybatis-generator之二:springboot集成mybatis测试成果

package com.example.demo001.service;

import com.example.demo001.dto.OrderDetail;

/**
 * @author 
 * @date 2018/11/5 20:37
 * @description
 */
public interface Demo001Service {
    OrderDetail getOrderDetailById(String detailId);
}

Demo001ServiceImpl.java

package com.example.demo001.service.impl;

import com.example.demo001.dto.OrderDetail;
import com.example.demo001.mapper.OrderDetailMapper;
import com.example.demo001.service.Demo001Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 
 * @date 2018/11/5 20:39
 * @description
 */
@Service
public class Demo001ServiceImpl implements Demo001Service {

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public OrderDetail getOrderDetailById(String detailId) {
        return orderDetailMapper.selectByPrimaryKey(detailId);
    }
}

5.编写controller层代码

Demo001Cntroller.java

package com.example.demo001.controller;

import com.example.demo001.dto.OrderDetail;
import com.example.demo001.service.Demo001Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 
 * @date 2018/11/5 20:41
 * @description
 */
@RestController
@RequestMapping("/demo")
public class Demo001Cntroller {

    @Autowired
    private Demo001Service demo001Service;

    @RequestMapping(value = "/getOrderDetail", method = {RequestMethod.GET, RequestMethod.POST})
    public OrderDetail getOrderDetail(@RequestParam("detailId") String detailId) {
        return demo001Service.getOrderDetailById(detailId);
    }
}

6.测试

启动服务,在浏览器中输入:http://localhost:8080/demo/getOrderDetail?detailId=123456

在idea中使用mybatis-generator之二:springboot集成mybatis测试成果

今天就到此啦,趁着加班的空隙写完。继续坚持。

既然选择了改变就坚持!!!