• Java--MybatisPlus表和列;自定义SQL(三)


    阅读前可先参考

    https://blog.csdn.net/MinggeQingchun/article/details/126521908

    https://blog.csdn.net/MinggeQingchun/article/details/126533536

    一、表和列 

    注解 | MyBatis-Plus

    1、表名

    @TableName 注解

    定义实体类时,默认需要和数据库中的表名保持一致;如果不一致可以使用 @TableName注解来进行说明

    @TableName(value = "数据库表名")

    创建表 user_address

    1. DROP TABLE IF EXISTS user_address;
    2. CREATE TABLE user_address(
    3. address_id int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    4. address_provice varchar(50) NULL DEFAULT NULL COMMENT '省份',
    5. address_city varchar(50) NULL DEFAULT NULL COMMENT '城市',
    6. address_street varchar(50) NULL DEFAULT NULL COMMENT '街道',
    7. address_code int(11) NULL DEFAULT NULL COMMENT '行政编码',
    8. PRIMARY KEY (address_id)
    9. )ENGINE=InnoDB DEFAULT CHARSET=utf8;

    编写entity实体类

    1. /**
    2. * @TableName(value="表名")
    3. * 位置:在类定义的上面
    4. */
    5. @TableName(value = "user_address")
    6. public class Address {
    7. //指定主键
    8. @TableId(value="address_id",type = IdType.AUTO)
    9. private Integer id;
    10. private String provice;
    11. private String city;
    12. private String street;
    13. private String code;
    14. }
    属性类型必须指定默认值描述
    valueString""表名
    schemaString""schema
    keepGlobalPrefixbooleanfalse是否保持使用全局的 tablePrefix 的值(当全局 tablePrefix 生效时)
    resultMapString""xml 中 resultMap 的 id(用于满足特定类型的实体类对象绑定)
    autoResultMapbooleanfalse是否自动构建 resultMap 并使用(如果设置 resultMap 则不会进行 resultMap 的自动构建与注入)
    excludePropertyString[]{}需要排除的属性名 @since 3.3.1

    2、列名

    @TableField 注解

    1. /**
    2. * @TableField : 指定属性和列名的对应关系。
    3. * 属性: value 指定列名
    4. */
    5. @TableField(value = "address_provice")
    6. private String provice;
    7. @TableField(value = "address_city")
    8. private String city;
    9. @TableField(value = "address_street")
    10. private String street;
    11. @TableField(value = "address_code")
    12. private String code;
    属性类型必须指定默认值描述
    valueString""数据库字段名
    existbooleantrue是否为数据库表字段
    conditionString""字段 where 实体查询比较条件,有值设置则按设置的值为准,没有则为默认全局的 %s=#{%s}参考(opens new window)
    updateString""字段 update set 部分注入,例如:当在version字段上注解update="%s+1" 表示更新时会 set version=version+1 (该属性优先级高于 el 属性)
    insertStrategyEnumFieldStrategy.DEFAULT举例:NOT_NULL
    insert into table_a(column) values (#{columnProperty})
    updateStrategyEnumFieldStrategy.DEFAULT举例:IGNORED
    update table_a set column=#{columnProperty}
    whereStrategyEnumFieldStrategy.DEFAULT举例:NOT_EMPTY
    where column=#{columnProperty}
    fillEnumFieldFill.DEFAULT字段自动填充策略
    selectbooleantrue是否进行 select 查询
    keepGlobalFormatbooleanfalse是否保持使用全局的 format 进行处理
    jdbcTypeJdbcTypeJdbcType.UNDEFINEDJDBC 类型 (该默认值不代表会按照该值生效)
    typeHandlerClassUnknownTypeHandler.class类型处理器 (该默认值不代表会按照该值生效)
    numericScaleString""指定小数点后保留的位数

    3、主键

    @TableId 注解

    1. //指定主键
    2. @TableId(value="address_id",type = IdType.AUTO)
    3. private Integer id;

    IdType 类型 

    1. public enum IdType {
    2. AUTO(0),
    3. NONE(1),
    4. INPUT(2),
    5. ASSIGN_ID(3),
    6. ASSIGN_UUID(4);
    7. }

    application.yml配置文件设置

    1. mybatis-plus:
    2. global-config:
    3. db-config:
    4. # id生成策略 auto为数据库自增
    5. id-type: auto
    描述
    AUTO数据库 ID 自增
    NONE无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
    INPUTinsert 前自行 set 主键值
    ASSIGN_ID分配 ID(主键类型为 Number(Long 和 Integer)或 String)(since 3.3.0),使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法)
    ASSIGN_UUID分配 UUID,主键类型为 String(since 3.3.0),使用接口IdentifierGenerator的方法nextUUID(默认 default 方法)
    ID_WORKER分布式全局唯一 ID 长整型类型(please use ASSIGN_ID)
    UUID32 位 UUID 字符串(please use ASSIGN_UUID)
    ID_WORKER_STR分布式全局唯一 ID 字符串类型(please use ASSIGN_ID)

    测试

    1. @Test
    2. public void testInsert(){
    3. Address address = new Address();
    4. address.setCity("上海");
    5. address.setStreet("南京路");
    6. address.setCode("020");
    7. //INSERT INTO user_address ( address_city, address_street, address_code ) VALUES ( ?, ?, ? )
    8. int rows = addressMapper.insert(address);
    9. System.out.println("insert address结果:"+rows);
    10. }

    4、驼峰命名

    默认情况下MP会开启字段名列名的驼峰映射, 即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射

    application.yml配置文件设置

    1. mybatis-plus:
    2. configuration:
    3. #是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射
    4. map-underscore-to-camel-case: false

    1、建表goods

    1. DROP TABLE IF EXISTS goods;
    2. CREATE TABLE goods(
    3. good_id int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    4. good_name varchar(50) NULL DEFAULT NULL COMMENT '商品名字',
    5. good_cate varchar(50) NULL DEFAULT NULL COMMENT '商品类别',
    6. PRIMARY KEY (good_id)
    7. )ENGINE=InnoDB DEFAULT CHARSET=utf8;

    2、创建实体类entity

    1. @TableName(value = "goods")
    2. @Data
    3. public class Goods {
    4. //定义属性
    5. @TableId(value="good_id",type = IdType.AUTO)
    6. private Integer goodId;
    7. private String goodName;
    8. private String goodCate;
    9. @Override
    10. public String toString() {
    11. return "Goods{" +
    12. "goodId=" + goodId +
    13. ", goodName='" + goodName + '\'' +
    14. ", goodCate='" + goodCate + '\'' +
    15. '}';
    16. }
    17. }

    3、创建mapper

    1. /**
    2. * 自定义Mapper,就是Dao接口
    3. * 1、要实现BaseMapper
    4. * 2、指定实体类
    5. *
    6. * BaseMapper是MP框架中的对象,定义19个操作方法(CRUD)
    7. */
    8. public interface GoodMapper extends BaseMapper {
    9. }

    4、测试

    1. @Test
    2. public void testInsert(){
    3. Goods goods = new Goods();
    4. goods.setGoodName("iPhone 12");
    5. goods.setGoodCate("手机");
    6. //INSERT INTO goods ( good_name, good_cate ) VALUES ( ?, ? )
    7. int rows = goodMapper.insert(goods);
    8. System.out.println("insert good结果:"+rows);
    9. }

    二、自定义SQL

    1、建表

    1. DROP TABLE IF EXISTS student;
    2. CREATE TABLE student(
    3. id int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    4. name varchar(50) NULL DEFAULT NULL COMMENT '学生名字',
    5. age int(11) NULL DEFAULT NULL COMMENT '年龄',
    6. email varchar(50) NULL DEFAULT NULL COMMENT '邮箱',
    7. status int(11) NULL DEFAULT NULL COMMENT '状态',
    8. PRIMARY KEY (id)
    9. )ENGINE=InnoDB DEFAULT CHARSET=utf8;

    2、创建实体entity

    1. @Data
    2. public class Student {
    3. //定义属性
    4. @TableId(value="id",type = IdType.AUTO)
    5. private Integer id;
    6. private String name;
    7. private Integer age;
    8. private String email;
    9. private Integer status;
    10. @Override
    11. public String toString() {
    12. return "Student{" +
    13. "id=" + id +
    14. ", name='" + name + '\'' +
    15. ", age=" + age +
    16. ", email='" + email + '\'' +
    17. ", status=" + status +
    18. '}';
    19. }
    20. }

    3、创建mapper

    1. /**
    2. * 自定义Sql
    3. */
    4. public interface StudentMapper extends BaseMapper {
    5. //自定义方法
    6. public int insertStudent(Student student);
    7. public Student selectStudentById(Integer id);
    8. public List selectByName(String name);
    9. }

    4、创建SQL映射xml文件

    1. "1.0" encoding="UTF-8"?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.company.mapper.StudentMapper">
    6. <insert id="insertStudent">
    7. insert into student(name,age,email,status) values(#{name},#{age},#{email},#{status})
    8. insert>
    9. <select id="selectStudentById" resultType="com.company.entity.Student">
    10. select id,name,age,email,status from student where id=#{studentId}
    11. select>
    12. <select id="selectByName" resultType="com.company.entity.Student">
    13. select id,name,age,email,status from student where name=#{name}
    14. select>
    15. mapper>

    5、配置xml文件

    1. mybatis-plus:
    2. #配置xml文件位置
    3. mapper-locations: classpath*:mapper/*Mapper.xml

    6、测试

    1. @Test
    2. public void testInsertStudent(){
    3. Student student = new Student();
    4. student.setName("zhangsan");
    5. student.setAge(18);
    6. student.setEmail("zhangsan@163.com");
    7. student.setStatus(2);
    8. int rows = studentMapper.insert(student);
    9. System.out.println("insert Student rows:"+rows);
    10. }
    11. @Test
    12. public void testSelect(){
    13. Student student = studentMapper.selectById(1);
    14. System.out.println("testSelect:"+student);
    15. }
  • 相关阅读:
    Python中的集合
    基于 Keras 的图像分类器
    latex:使用中文字体
    [附源码]SSM计算机毕业设计远程在线教育平台JAVA
    共享盘的文件删除后能找回吗
    一文了解模型量化中的QAT和PTQ
    自制OS3-1到4-10==保护模式(GDT、选择子、寄存器)、多任务由来(LDT)、内核态和用户态ring0和ring3、特权级切换(TSS-CPL-DPL-RPL-门)、时钟中断、保护模式中断编程
    Matlab三维数据区域显示问题
    【half done】剑指offer53:在排序数组中查找数字
    OpenCore-Legacy-Patcher 0.5.1正式版
  • 原文地址:https://blog.csdn.net/MinggeQingchun/article/details/126539292