springboot后台服务搭建(三 整合mysql、spring-data-jpa)
总览:https://blog.csdn.net/qq_22037575/article/details/86687765
本文概要:springboot2.x 整合 mysql、spring-data-jpa
码云:https://gitee.com/RichterGit/csdn/tree/master/springboot-radmin/003/
目录
1.新建数据库与用户角色表
新建数据库 csdn_radmin
新建角色表 role
drop table if exists role;
/*==============================================================*/
/* Table: role */
/*==============================================================*/
create table role
(
id bigint not null,
name varchar(50) not null comment '角色名',
alias varchar(50) not null comment '别名',
primary key (id)
);
alter table role comment '角色';
插入三条数据 超级管理员、后台管理员、普通用户
2.pom导入依赖
导入jdbc依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
导入 mysql 依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
导入 spring-data-jpa 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
3.全局配置
配置 jdbc与 mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/csdn_radmin?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
配置 spring-data-jpa
spring.jpa.database=mysql
spring.jpa.show-sql=true
这里为了让全局配置文件的语句更有层次感,把 application.properties重命名为 application.yml
4.业务分层
后台使用分层思想,我这里分别新建 domain包、repository包、service包
domain:用来存放实体
repository:用来存放与 spring-data-jpa相关的接口
service:用来存放实现业务功能的业务类