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
DROP TABLE IF EXISTS user_address;CREATE TABLE user_address(address_id int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',address_provice varchar(50) NULL DEFAULT NULL COMMENT '省份',address_city varchar(50) NULL DEFAULT NULL COMMENT '城市',address_street varchar(50) NULL DEFAULT NULL COMMENT '街道',address_code int(11) NULL DEFAULT NULL COMMENT '行政编码',PRIMARY KEY (address_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
编写entity实体类
/*** @TableName(value="表名")* 位置:在类定义的上面*/
@TableName(value = "user_address")
public class Address {//指定主键@TableId(value="address_id",type = IdType.AUTO)private Integer id;private String provice;private String city;private String street;private String code;}
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | "" | 表名 |
schema | String | 否 | "" | schema |
keepGlobalPrefix | boolean | 否 | false | 是否保持使用全局的 tablePrefix 的值(当全局 tablePrefix 生效时) |
resultMap | String | 否 | "" | xml 中 resultMap 的 id(用于满足特定类型的实体类对象绑定) |
autoResultMap | boolean | 否 | false | 是否自动构建 resultMap 并使用(如果设置 resultMap 则不会进行 resultMap 的自动构建与注入) |
excludeProperty | String[] | 否 | {} | 需要排除的属性名 @since 3.3.1 |
2、列名
@TableField 注解
/*** @TableField : 指定属性和列名的对应关系。* 属性: value 指定列名*/@TableField(value = "address_provice")private String provice;@TableField(value = "address_city")private String city;@TableField(value = "address_street")private String street;@TableField(value = "address_code")private String code;
属性 | 类型 | 必须指定 | 默认值 | 描述 |
---|---|---|---|---|
value | String | 否 | "" | 数据库字段名 |
exist | boolean | 否 | true | 是否为数据库表字段 |
condition | String | 否 | "" |
字段 where 实体查询比较条件,有值设置则按设置的值为准,没有则为默认全局的 %s=#{%s} ,参考(opens new window)
|
update | String | 否 | "" |
字段 update set 部分注入,例如:当在version字段上注解update="%s+1" 表示更新时会 set version=version+1 (该属性优先级高于 el 属性)
|
insertStrategy | Enum | 否 | FieldStrategy.DEFAULT |
举例:NOT_NULLinsert into table_a(<if test="columnProperty != null">column</if>) values (<if test="columnProperty != null">#{columnProperty}</if>)
|
updateStrategy | Enum | 否 | FieldStrategy.DEFAULT |
举例:IGNOREDupdate table_a set column=#{columnProperty}
|
whereStrategy | Enum | 否 | FieldStrategy.DEFAULT |
举例:NOT_EMPTYwhere <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
|
fill | Enum | 否 | FieldFill.DEFAULT | 字段自动填充策略 |
select | boolean | 否 | true | 是否进行 select 查询 |
keepGlobalFormat | boolean | 否 | false | 是否保持使用全局的 format 进行处理 |
jdbcType | JdbcType | 否 | JdbcType.UNDEFINED | JDBC 类型 (该默认值不代表会按照该值生效) |
typeHandler | Class<? extends TypeHandler> | 否 | UnknownTypeHandler.class | 类型处理器 (该默认值不代表会按照该值生效) |
numericScale | String | 否 | "" | 指定小数点后保留的位数 |
3、主键
@TableId 注解
//指定主键@TableId(value="address_id",type = IdType.AUTO)private Integer id;
IdType 类型
public enum IdType {AUTO(0),NONE(1),INPUT(2),ASSIGN_ID(3),ASSIGN_UUID(4);
}
application.yml配置文件设置
mybatis-plus:global-config:db-config:# id生成策略 auto为数据库自增id-type: auto
值 | 描述 |
---|---|
AUTO | 数据库 ID 自增 |
NONE | 无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT) |
INPUT | insert 前自行 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 )
|
UUID |
32 位 UUID 字符串(please use ASSIGN_UUID )
|
ID_WORKER_STR |
分布式全局唯一 ID 字符串类型(please use ASSIGN_ID )
|
测试
@Testpublic void testInsert(){Address address = new Address();address.setCity("上海");address.setStreet("南京路");address.setCode("020");//INSERT INTO user_address ( address_city, address_street, address_code ) VALUES ( ?, ?, ? )int rows = addressMapper.insert(address);System.out.println("insert address结果:"+rows);}
4、驼峰命名
默认情况下MP会开启字段名列名的驼峰映射, 即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射
application.yml配置文件设置
mybatis-plus:configuration:#是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射map-underscore-to-camel-case: false
1、建表goods
DROP TABLE IF EXISTS goods;CREATE TABLE goods(good_id int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',good_name varchar(50) NULL DEFAULT NULL COMMENT '商品名字',good_cate varchar(50) NULL DEFAULT NULL COMMENT '商品类别',PRIMARY KEY (good_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、创建实体类entity
@TableName(value = "goods")
@Data
public class Goods {//定义属性@TableId(value="good_id",type = IdType.AUTO)private Integer goodId;private String goodName;private String goodCate;@Overridepublic String toString() {return "Goods{" +"goodId=" + goodId +", goodName='" + goodName + '\'' +", goodCate='" + goodCate + '\'' +'}';}
}
3、创建mapper
/*** 自定义Mapper,就是Dao接口* 1、要实现BaseMapper* 2、指定实体类** BaseMapper是MP框架中的对象,定义19个操作方法(CRUD)*/
public interface GoodMapper extends BaseMapper<Goods> {
}
4、测试
@Testpublic void testInsert(){Goods goods = new Goods();goods.setGoodName("iPhone 12");goods.setGoodCate("手机");//INSERT INTO goods ( good_name, good_cate ) VALUES ( ?, ? )int rows = goodMapper.insert(goods);System.out.println("insert good结果:"+rows);}
二、自定义SQL
1、建表
DROP TABLE IF EXISTS student;CREATE TABLE student(id int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',name varchar(50) NULL DEFAULT NULL COMMENT '学生名字',age int(11) NULL DEFAULT NULL COMMENT '年龄',email varchar(50) NULL DEFAULT NULL COMMENT '邮箱',status int(11) NULL DEFAULT NULL COMMENT '状态',PRIMARY KEY (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、创建实体entity
@Data
public class Student {//定义属性@TableId(value="id",type = IdType.AUTO)private Integer id;private String name;private Integer age;private String email;private Integer status;@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", email='" + email + '\'' +", status=" + status +'}';}
}
3、创建mapper
/*** 自定义Sql*/
public interface StudentMapper extends BaseMapper<Student> {//自定义方法public int insertStudent(Student student);public Student selectStudentById(Integer id);public List<Student> selectByName(String name);
}
4、创建SQL映射xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.company.mapper.StudentMapper"><insert id="insertStudent">insert into student(name,age,email,status) values(#{name},#{age},#{email},#{status})</insert><select id="selectStudentById" resultType="com.company.entity.Student">select id,name,age,email,status from student where id=#{studentId}</select><select id="selectByName" resultType="com.company.entity.Student">select id,name,age,email,status from student where name=#{name}</select>
</mapper>
5、配置xml文件
mybatis-plus:#配置xml文件位置mapper-locations: classpath*:mapper/*Mapper.xml
6、测试
@Testpublic void testInsertStudent(){Student student = new Student();student.setName("zhangsan");student.setAge(18);student.setEmail("zhangsan@163.com");student.setStatus(2);int rows = studentMapper.insert(student);System.out.println("insert Student rows:"+rows);}@Testpublic void testSelect(){Student student = studentMapper.selectById(1);System.out.println("testSelect:"+student);}
Java--MybatisPlus表和列;自定义SQL(三)相关推荐
- oracle一个表更新另一个表多列,oracle sql更新表中多列值,值是从其它表中查询(select)得出...
案例描述:sql 将表vehicle中列pay_money_remain的值分为2/3,1/3再更新到表vehicle的pay_money_remain,disinfectionbal_remains ...
- JAVA动态表单,自定义表单,自定义字段
基于雷劈网的表单设计器扩展,java实现后台解析(插件内容和字段和原版有一定改变).致敬雷劈网. http://formdesign.leipi.org/ 动态表单绘制完毕后由后台解析并存储到数据库, ...
- [aaronyang原创] Mssql 一张表3列的sql面试题,看你sql学的怎么样
文章已经迁移到:http://www.ayjs.net/post/99.html 文章已经迁移到:http://www.ayjs.net/post/99.html 文章已经迁移到:http://www ...
- 使用 Wrapper 自定义SQL
MyBatis-Plus官网 前言 MyBatis-Plus支持使用 Wrapper 自定义SQL, 但是官方文档描述简陋, 本文将结合实例做一个扩展补充 自定义SQL一般用于解决多表联合查询问题, ...
- 关于Mybatis-plus多表联查自定义sql分页查询
问题描述: 使用mybatis-plus进行开发过程中,单表得增删改查等都可以利用封装好的方法,而一些场景设计多表联合查询,且需要自定义字段的,就需要进行自定义sql 使用方法: 1.service中 ...
- MybatisPlus自定义 Sql 实现多表查询
目录 概述 编写代码 案例流程说明 控制层 服务层 数据访问层 自定义 SQL 测试 MybatiPlus文档 概述 MyBatis-Plus (opens new window)(简称 MP)是一个 ...
- MyBatis-plus执行自定义SQL
序号 类型 地址 1 MySQL MySQL操作之概念.SQL约束(一) 2 MySQL MySQL操作之数据定义语言(DDL)(二) 3 MySQL MySQL操作之数据操作语言(DML)(三) 4 ...
- MyBatis-Plus 之自定义sql
推荐:MyBatis Plus汇总 MyBatis-Plus 之自定义sql 首先创建一个数据库表,如下图所示: 然后创建一个Spring Boot项目,pom.xml和配置如下: <?xml ...
- MybatisPlus自定义SQL日志打印
前言 mybatisplus在mybatis的基础上为我们提供了诸多方便,大大加快了开发的速率,但是在日常工作中,还是会发现有一些不方便之处,那就是关于日志的打印,框架虽然也提供了日志打印,但是日志的 ...
最新文章
- python可以做什么项目-Python可以做大项目吗?
- 小程序服务器角色,小程序在我们的生活中扮演什么角色?
- idea 设置java栈空间,如何为Intellij编译器提供更多堆空间?
- 两个时间点距离 time_t c_天津二建公路考试时间
- 人力节省 50%,研发效能提升 40%,阿里 Serverless 架构落地实践
- SQL server 使用自定义函数以及游标
- SQL Server无法连接到(local)问题的解决的方法
- ad9修改焊盘阻焊层大小
- 利用DMRMAN备份时出现“管道连接失败”的错误信息的解决方法
- 猕猴桃的红色果肉受到特定的激活-抑制系统的控制
- 云会议开启线上办公新模式
- Golang 获取今天和昨天零点的时间
- matlab画等势线,求助大牛MATLAB画三维等势面
- 微信小程序简单实现上拉触底onReachBottom数据分页请求
- EdgeX 树莓派实践部署
- Java操作ElasticSearch,java程序设计教程第二版pdf
- 【收藏】75个很有用的开源移动工具
- 【深度学习】资源:最全的 Pytorch 资源大全
- 去阿尼网,人人都是俊男美女
- SPI与SSP的区别?