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


    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);
        }
    
    }
  • 相关阅读:
    redis中的发布订阅(Pub/Sub)
    emmc基础技术8:操作模式3-interrupt mode
    Linux命令-tar
    git获取内核源码的方法
    Linux内核基础设施
    Linux内核简介
    emmc基础技术8:操作模式2-device identification mode
    内核子系统文档撰写方法
    eMMC基础技术6:eMMC data读写
    eMMC基础技术10:寄存器介绍
  • 原文地址:https://www.cnblogs.com/cbxBlog/p/9244871.html
Copyright © 2020-2023  润新知