注解版 MyBatis|学习笔记

简介: 快速学习注解版 MyBatis

开发者学堂课程【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")

相关文章
|
4月前
|
存储 Java 数据库连接
Mybatisplus中的主要使用注解
3.有些注解需要配合其他配置使用。例如,@Version需要配合乐观锁插件使用,@EnumValue需要配合对应的TypeHandler使用。
227 11
|
5月前
|
SQL XML Java
MyBatis——选择混合模式还是全注解模式?
在MyBatis开发中,Mapper接口的实现方式有两种:全注解模式和混合模式。全注解模式直接将SQL嵌入代码,适合小规模、简单逻辑项目,优点是直观简洁,但复杂查询时代码臃肿、扩展性差。混合模式采用接口+XML配置分离的方式,适合大规模、复杂查询场景,具备更高灵活性与可维护性,但学习成本较高且调试不便。根据项目需求与团队协作情况选择合适模式至关重要。
90 4
|
6月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
472 0
|
10月前
|
SQL Java 数据库连接
【MyBatisPlus·最新教程】包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段
MyBatis-Plus是一个MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。本文讲解了最新版MP的使用教程,包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段等核心功能。
1590 5
【MyBatisPlus·最新教程】包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段
|
10月前
|
SQL 缓存 Java
MyBatis如何关闭一级缓存(分注解和xml两种方式)
MyBatis如何关闭一级缓存(分注解和xml两种方式)
368 5
|
10月前
|
Java 数据库连接 mybatis
Mybatis使用注解方式实现批量更新、批量新增
Mybatis使用注解方式实现批量更新、批量新增
190 3
|
10月前
|
SQL 存储 数据库
深入理解@TableField注解的使用-MybatisPlus教程
`@TableField`注解在MyBatis-Plus中是一个非常灵活和强大的工具,能够帮助开发者精细控制实体类与数据库表字段之间的映射关系。通过合理使用 `@TableField`注解,可以实现字段名称映射、自动填充、条件查询以及自定义类型处理等高级功能。这些功能在实际开发中,可以显著提高代码的可读性和维护性。如果需要进一步优化和管理你的MyBatis-Plus应用程
735 3
|
12月前
|
SQL XML Java
mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数
文章介绍了MyBatis的简单增删改查操作,包括创建数据表、实体类、配置文件、Mapper接口及其XML文件,并解释了`#{}`预编译参数和`@Param`注解的使用。同时,还涵盖了resultType与resultMap的区别,并提供了完整的代码实例和测试用例。
mybatis复习02,简单的增删改查,@Param注解多个参数,resultType与resultMap的区别,#{}预编译参数
|
10月前
|
Java 数据库连接 mybatis
Mybatis使用注解方式实现批量更新、批量新增
Mybatis使用注解方式实现批量更新、批量新增
764 1
|
12月前
|
Java 数据库连接 数据格式
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
IOC/DI配置管理DruidDataSource和properties、核心容器的创建、获取bean的方式、spring注解开发、注解开发管理第三方bean、Spring整合Mybatis和Junit
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit