1添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
2书写pojo类
实体类adminId在表中会变成admin_id(不用column的name属性指定名称的情况下)
@Data
//@Table(name = "admin")
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class Admin {
//主键
@Id
//自增
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int adminId;
//unique是否唯一,唯一就是true;nullable可不可以为null,不可以是false
@Column(length =20,unique =true,nullable =false)
private StringadminName;
@Column(nullable =false,length =20)
private StringadminPassword;
}
3dao层
可以继承四种接口,接口内有定义好的方法
JPARepository继承PagingAndSortingRepository继承CURDRepository继承Repository
4service实现类和service接口
5配置application.yml文件
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3307/shop?useSSL=true&serverTimezone=UTC&characterEncoding=UTF-8
username: root
password: www19981220
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
# 数据库的方言
database:mysql
# 是否显示sql语句
show-sql:true
# 是否将sql语句格式化(分几行写)
properties:
hibernate:
format_sql: true
# 自动创建|更新|验证数据库表结构
hibernate:
ddl-auto: update
错误:mysql驱动版本问题
解决:com.mysql.jdbc.Driver,是mysql5之前的
com.mysql.cj.jdbc.Driver是MySQL6之后的