数据库SQL 语句规范(l ⽂件)及模板
数据库SQL 语句规范(l ⽂件)及模板
⼀.实体entity及请求对象,响应对象
1. 实体
2. 请求对象package com .xxx .xxx .entity ;import com .baomidou .mybatisplus .annotation .IdType ;import com .baomidou .mybatisplus .annotation .TableField ;import com .baomidou .mybatisplus .annotation .TableId ;import com .baomidou .mybatisplus .annotation .TableName ;import lombok .Data ;import lombok .EqualsAndHashCode ;import lombok .experimental .Accessors ;import java .io .Serializable ;import java .time .LocalDateTime ;@Data @EqualsAndHashCode (callSuper = false )@Accessors (chain = true )@TableName ("t_abc_entity")public class User implements Serializable { private static final long serialVersionUID = 1L ; /** * 主键 */ @TableId (value = "id", type = IdType .AUTO ) private Long id ; /** * 姓名 **/ @TableField ("name") private String name ; /** * 年龄 */ @TableField ("age") private Integer age ; /** * 创建时间 */ @TableField ("create_time") private LocalDateTime createdTime ;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
3.响应对象import java .time .LocalDateTime ;import java .util .List ;@Data public class RequestDto i
mplements Serializable { private static final long serialVersionUID = 1L ; /** * 主键 */ private Long id ; /** * 姓名 **/ private String name ; /** * 年龄 */ private Integer age ; /** * 年龄列表 **/ private List <Integer > ageList ; /** * 创建时间 */ private LocalDateTime createdTime ;}
5
6
7
8
9
10
11
索尼xml文件可以删除吗12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
⼆. Mapper层数据库中的执⾏语句写法
解释说明: 1)id后⾯跟的是⽅法名,parameterType后⾯跟的是请求对象类型(带上包名),resultType后⾯跟的是响应对象类型(带上包名); 2)useGeneratedKeys=“true” keyProperty="id"表⽰:如果插⼊的表id以⾃增列为主键,则允许 JDBC ⽀持⾃动⽣成主键,并可将⾃动⽣成的主键id返回。useGeneratedKeys参数只针对 insert 语句⽣效,默认为 false; 3)当对数据库进⾏批量操作的时候使⽤时需要注意,如果在Mapper.java⽂件中对应的⽅法中使⽤了@Param(“list”)这个注解进⾏绑定时,参数列表⼀定要相同都是list,否则在数据进⼊数据库操作时会报错
1. 新增⼀条数据
2. 批量新增import java .time .LocalDateTime ;@Data public class ResponseVo implements Serializable { private static final long serialVersionUID = 1L ; /** * 主键 */ private Long id ; /** * 姓名 **/ private String name ; /** * 年龄 */ private Integer age ; /** * 创建
时间 */ private LocalDateTime createdTime ;}
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31<insert id ="insert" parameterType ="tity.User" useGeneratedKeys ="true" keyProperty ="id"> insert into t_abc_entity ( name , age , create_time , ) values ( #{name }, #{age }, #{createTime } ) </insert >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
3. 通⽤查询
4. 批量删除(根据id)
5. 批量选择更新<insert id ="batchInsert" parameterType ="java.util.List" useGeneratedKeys ="true" keyProperty ="id"> insert into t_abc_entity ( name , age , create_time , ) values <foreach collection ="list" item ="item" separator =","> ( #{item .name }, #{item .age }, #{item .createTime } ) </foreach ></insert >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<select id ="queryBySelect" parameterType ="dtoRequestDto " resultType ="vo.ResponseVo"> select id ,name ,age ,create_time from t_abc_entity <where > <if test ="id != null"> and Id = #{id } </if > <if test ="name != null and name!=''"> and name = #{name ,jdbcType =VARCHAR } </if > <if
test ="age != null"> and age = #{age } </if > <if test ="ageList!= null and ageList.size() > 0"> AND age IN <foreach collection ="ageList" item ="item" index ="index" open ="(" separator ="," close =")"> #{item } </foreach > </if > <if test ="createTime != null and createTime !=''"> and create_time = #{createTime ,jdbcType =VARCHAR } </if > </where ></select >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24<delete id ="deleteByIds" parameterType ="java.util.List"> delete from t_abc_entity where ID in ( <foreach collection ="list" item ="item" separator ="," > #{item } </foreach > ) </delete >
1
2
3
4
5
6
7
8
9
10
<update id ="batchUpdate" parameterType ="java.util.List"> <foreach item ="emp" separator =";" collection ="list"> update t_abc_entity <set > <if test ="emp.name != null"> name = #{emp .name }, </if > <if test ="emp.age!= null"> age = #{emp .age }, </if > <if test ="atedTime!= null"> create_time = #{emp .createdTime }, </if > </set > where ID = #{emp .id } </foreach > </update >123456789101112131415161718
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论