zoukankan      html  css  js  c++  java
  • 【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

  • 相关阅读:
    js去除字符串空格(空白符)
    jq以固定开头的class属性的名称
    day--38 mysql表的完整性约束总结
    day--39-MySQL的多表查询
    day--40 mysql-视图,触发器,存储过程,函数总结
    day--41 mysql索引原理与慢查询优化
    day--42 前端基础小结
    day--43 HTML标签和CSS基本小结
    day46----JavaScript的函数及对象小结
    JavaScript正则表达式
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/13471727.html
Copyright © 2011-2022 走看看