前言
记录软件开发点滴,积累知识和经验(第二篇)
要做什么?
目标:利用springboot搭建mysql,druid,jpa
怎样去做?
- 引入对应的jar
<!--jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.25</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
- 配置application.properties
## 数据源
spring.datasource.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=gbk&useSSL=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
## jpa
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.show-sql=true
- 使用jpa建立实体类(Admin.java)
@Data
@Entity
@Table(name = "admin")
public class Admin implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String password;
private Integer RoleId;
private Integer status;
private Date CreatedAt;
private Date UpdatedAt;
private String CreatePerson;
private String UpdatePerson;
}
- 在数据库管理器中建立名为blog的数据库
- @Data 的作用就是为了简化get/set
结果怎么样?
如果项目能正常启动并且数据库blog能生成一张admin的表,则配置成功。