开发者学堂课程【SpringBoot快速掌握 - 核心技术:注解版 MyBatis】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/612/detail/9276
注解版 MyBatis
目录:
一、实操演示
二、课堂笔记
一、实操演示
1.创建一个 Mapper
DepartmentMapper 接口部分代码展示:
package com.atguigu.springboot.mapper;
import com.atguigu.springboot.bean.Department;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
//指定这是一个操作数据库的 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);
//增加
@Insert("insert into department(departmentName) values(#{departmentName})")publicJint insertDept(Department department);
//修改
@Update("update department set departmentName=#{ departmentName} where id=#(id)")public int updateDept(Department department);
}
DeptController 层部分代码展示:
package com.atguigu.springboot.controller;
import com.atguigu.springboot.bean . Department;
import com.atguigu.springboot.mapper.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DeptController i
@Autowired
DepartmentMapper;
@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;
}
}
MybatisConfig 部分代码展示:
package com.atguigu.springboot.config;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;import org.springframework.context.annotation.Bean;
8
@org-springframework.context.annotation.Configurationlpublic class MyBatisconfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer(){
@Override
public void customize(configuration configuration) {
configuration.setMapUnderscoreToCamelcase(true);
}
};
}
}
二、课堂笔记
//指定这是一个操作数据库的 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);
@Insert("insert into department(departmentName) values(#{ departmentName})")public int insertDept(Department department);
@Update("update department set departmentName=#{departmentName} where id=#{id} ")public int updateDept(Department department);
}
问题:
自定义 MyBatis 的配置规则;给容器中添加一个ConfigurationCustomizer:
@org.springframework.context.annotation.configurationpublic class MyBatisconfig {
@Bean
public configurationcustomizer configurationcustomizer(){
return new configurationcustomizer(){
@override
public void customize(Configuration configuration){
configuration.setMapUnderscoreToCamelCase(true) ;
}
};
}
使用MapperScan 批量扫描所有的 Mapper接口;
@MapperScan(value ="com.atguigu.springboot.mapper")