zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
  • 相关阅读:
    I/O流
    宇宙第一帅的HTML笔记
    宇宙无敌第一帅的Java笔记
    Activity常用的方法
    Spinne
    安卓布局方式
    for循环
    TextView
    开发Activity步骤
    重写
  • 原文地址:https://www.cnblogs.com/cbxBlog/p/9244871.html
Copyright © 2011-2022 走看看