码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot_数据访问-整合MyBatis-配置版MyBatis

时间:2018-06-29 19:29:36      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:while   employee   conf   jdb   pool   整合   min   span   filter   

技术分享图片

1.添加配置文件
    mybatis-config.xml
    
    <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
        <!--驼峰命名-->
        <settings>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        </settings>
</configuration>


    EmployeeMapper.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.baoxing.springboot.mapper.EmployeeMapper">
  <!--
       public Employee getEmpById(Integer id);

       public void insertEmp(Employee employee);
  -->
        <select id="getEmpById" resultType="com.baoxing.springboot.bean.Employee">
            select * from employee where id=#{id}
        </select>
    <insert id="insertEmp">
        INSERT  INTO  employee(lastName,email,gender,d_id) VALUES (#{lastName},#{email},#{gender},#{did})
    </insert>
</mapper>
application.yml配置文件 主要看mybatis对应的配置

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/test
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
#    schema:
#      - classpath:sql/department.sql
#      - classpath:sql/employee.sql

#   数据源其他配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
#   配置监控统计拦截的filters,去掉后监控界面sql无法统计,wall用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
EmployeeMapper类

@Mapper
public interface EmployeeMapper {

    public Employee getEmpById(Integer id);

    public void insertEmp(Employee employee);
}
Controller类
    
@RestController
public class DepartmentController {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @GetMapping(value = "/dept/{id}")
    public Department getDepartment(@PathVariable("id") Integer id){

        return departmentMapper.getDeptById(id);
    }

    @GetMapping(value = "/dept")
    public Department insertDept(Department department){

         departmentMapper.insertDept(department);
        return department;
    }
    @GetMapping(value = "emp/{id}")
    public Employee getEmp(@PathVariable("id")Integer id){
        return employeeMapper.getEmpById( id);
    }

}

 

SpringBoot_数据访问-整合MyBatis-配置版MyBatis

标签:while   employee   conf   jdb   pool   整合   min   span   filter   

原文地址:https://www.cnblogs.com/cbxBlog/p/9244871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!