Spring Data 自动生成
之前一直用的mybatis逆向自动生成,由于最近学习springdata,所以看了一下springdata的自动生成,基本与mybatis一致,不同的也许就是逆向生成代码(实体类,mapper等)和正向生成数据库
不多逼逼,直接上代码吧
1.添加依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-data-jpa</artifactId> 4 </dependency>
2.在配置文件中配置生成策略
1 server: 2 port: 8081 #设置端口 3 tomcat: 4 uri-encoding: utf-8 5 6 spring: 7 datasource: 8 driver-class-name: com.mysql.jdbc.Driver 9 url: jdbc:mysql://localhost:3306/tourist?characterEncoding=utf8&useSSL=false 10 username: root 11 password: 123456 12 jpa: 13 hibernate: 14 ddl-auto: update #配置自动建表:updata:没有表新建,有表更新操作,控制台显示建表语句 15 show-sql: true #是否显示sql
关于生成策略,有四种,一般都会用update
create----每次运行该程序,没有表格会新建表格,表内有数据会清空
create-drop----每次程序结束的时候会清空表
update----每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新
validate----运行程序会校验数据与数据库的字段类型是否相同,不同会报错
3.创建Bean实体类

1 package com.sunarmy.cn.entity; 2 3 import org.hibernate.annotations.GenericGenerator; 4 5 import javax.persistence.*; 6 7 /** 8 * Created by SunArmyon 2018/8/24. 9 */ 10 @Entity //声明一个实体类 11 @Table(name = "user") //映射的表的名称 12 public class User { 13 /** 14 * id 唯一不重复 15 * 声明主键 16 * 声明主键的生成策略 17 */ 18 @Id 19 @GeneratedValue(strategy = GenerationType.IDENTITY,generator = "system-uuid") 20 @GenericGenerator(name = "system-uuid",strategy = "uuid.hex") 21 String id; 22 23 /** 24 * 用户名 25 */ 26 @Column(name = "username") 27 String username; 28 29 /** 30 * 密码 31 */ 32 @Column(name = "password") 33 String password; 34 35 /** 36 * 盐 37 */ 38 @Column(name = "salt") 39 String salt; 40 41 /** 42 * Token 43 */ 44 @Column(name = "token") 45 String token; 46 /** 47 * 手机号 48 */ 49 @Column(name = "mobile",length = 11) 50 Long mobile; 51 52 /** 53 * 创建时间 54 * @return 55 */ 56 @Column(name = "create_date") 57 Long createdate; 58 59 /** 60 * 最后修改时间 61 */ 62 @Column(name = "last_modified_time") 63 Long lastmodifiedtime; 64 65 public String getId() { 66 return id; 67 } 68 69 public void setId(String id) { 70 this.id = id; 71 } 72 73 public String getUsername() { 74 return username; 75 } 76 77 public void setUsername(String username) { 78 this.username = username; 79 } 80 81 public String getPassword() { 82 return password; 83 } 84 85 public void setPassword(String password) { 86 this.password = password; 87 } 88 89 public String getSalt() { 90 return salt; 91 } 92 93 public void setSalt(String salt) { 94 this.salt = salt; 95 } 96 97 public String getToken() { 98 return token; 99 } 100 101 public void setToken(String token) { 102 this.token = token; 103 } 104 105 public Long getMobile() { 106 return mobile; 107 } 108 109 public void setMobile(Long mobile) { 110 this.mobile = mobile; 111 } 112 113 public Long getCreatedate() { 114 return createdate; 115 } 116 117 public void setCreatedate(Long createdate) { 118 this.createdate = createdate; 119 } 120 121 public Long getLastmodifiedtime() { 122 return lastmodifiedtime; 123 } 124 125 public void setLastmodifiedtime(Long lastmodifiedtime) { 126 this.lastmodifiedtime = lastmodifiedtime; 127 } 128 } 129 130
4.启动项目查看数据库
注意看,因为在配置中设置了show-sql: true 启动的时候会在控制台打印出建表的sql
然后查看数据库发现表已经生成了
至此,自动生成已经完成了,关于表中字段各个属性配置后期再做补充
如果有什么问题,或者哪里有误,请大神们指出,小弟不胜惶恐
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库