1、在Idea中新建Project
在New Project中选择Spring Initializr,Project SDK选择JDK1.8,点击Next
选择项目类型Type为Gradle Project,打包方式Packaging为War,点击Next


2、配置数据源,运行项目
在项目src\main\resource目录下的application.properties配置Tomcat端口和数据源
server.port=8088
#MySql
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/fb-root?characterEncoding=UTF-8&useUnicode=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
点击Run按钮可运行项目
3、数据库自动建表操作
创建entity包,新建SysUser类
@Entity
@Table(name = "sys_user")
public class SysUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(length = 30)
private String username;
@Column(length = 50)
private String password;
@Column(length = 11)
private String mobile;
@Column(length = 100)
private String header;
@Column(length = 10)
private String salt;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date loginTime;
@Column(length = 1)
private int state;
}
生成getter/setter和Constructor方法,在application.properties配置文件中添加
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
运行项目将自动创建sys_user表
网友评论