• 【SpringBoot__Mybatis】整合MyBatis注解版


    Application扫描包

    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @MapperScan(value = "k.mapper")
    @SpringBootApplication
    public class Boot08Application {
    
    	public static void main(String[] args) {
    		SpringApplication.run(Boot08Application.class, args);
    	}
    
    }
    

    MyBatisConfig驼峰命名适配

    import org.apache.ibatis.session.Configuration;
    import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
    import org.springframework.context.annotation.Bean;
    
    @org.springframework.context.annotation.Configuration
    public class MyBatisConfig {
    
        @Bean
        public ConfigurationCustomizer configurationCustomizer(){
            return new ConfigurationCustomizer(){
    
                @Override
                public void customize(Configuration configuration) {
                    configuration.setMapUnderscoreToCamelCase(true);
                }
            };
        }
    }
    

    DepartmentMapper注解

    package k.mapper;
    
    import k.bean.Department;
    import org.apache.ibatis.annotations.*;
    
    //指定这是一个操作数据库的mapper
    // @Mapper
    public interface DepartmentMapper {
    
        @Select("select * from department where id=#{id}")
        public Department getDeptById(Integer id);
    
        @Delete("delete from department where id=#{id}")
        public int deleteDeptById(Integer id);
    
        @Options(useGeneratedKeys = true, keyProperty = "id")
        @Insert("insert into department(department_name) values(#{departmentName})")
        public int insertDept(Department department);
    
        @Update("update department set department_name=#{departmentName} where id=#{id}")
        public int updateDept(Department department);
    }
    
    

    DeptController

    @RestController
    public class DeptController {
    
        @Autowired
        DepartmentMapper departmentMapper;
    
        @Autowired
        EmployeeMapper employeeMapper;
    
        @GetMapping("/dept/{id}")
        public Department getDepartment(@PathVariable("id") Integer id) {
            return departmentMapper.getDeptById(id);
        }
    
        @GetMapping("/dept")
        public Department insertDept(Department department) {
            departmentMapper.insertDept(department);
            return department;
        }
    
        @GetMapping("/emp/{id}")
        public Employee getEmp(@PathVariable("id") Integer id) {
            return employeeMapper.getEmpById(id);
        }
    }
    

    DruidConfig

  • 相关阅读:
    ATCoder code festival 2016 qual C
    2019.10.26模拟赛
    2019.10.24模拟赛
    狄利克雷卷积和莫比乌斯反演学习笔记
    ljq的互测の题解
    noi.ac #39
    noi.ac #741 code
    noi.ac #65 triangle
    让别人也可以访问你电脑上的ASP.NET MVC创建的网站
    ASP.NET MVC 开发中遇到的两个小问题
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/13471727.html
Copyright © 2020-2023  润新知