Spring Boot学习日志(二)JPA操作Mysql
2018-03-11 10:53
721 查看
Spring Boot学习日志(二)JPA操作Mysql
标签:Spring BootSpring Boot学习日志(二)JPA基本操作
Spring Boot学习日志(一)Hello WorldSpring Boot学习日志(二)JPA操作Mysql
Spring Boot学习日志(三)再谈Jpa
Spring Boot学习日志(四)使用Mybatis
Spring Boot学习日志(五)结合Swagger管理接口
什么是JPA
Jpa简单说就是一个ORM持久层框架,可以让开发人员一行sql不写,实现基本的增删改查操作。导入依赖库
在新建项目时,勾选Web,JPA和Mysql(因为我使用的Mysql测试,所以也要用到Mysql的依赖)<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> 4000 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>1
2
3
4
5
6
7
8
9
10
11
12
13
14
数据库配置
#数据库类型 spring.datasource.driverClassName = com.mysql.jdbc.Driver #数据库链接地址 spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&allowMultiQueries=true&&useSSL=false #用户名 spring.datasource.username = root #密码 spring.datasource.password = root1
2
3
4
5
6
7
8
9
创建测试用到的表
CREATE TABLE `tb_user` ( `id` int(11) NOT NULL, `username` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;1
2
3
4
5
6
7
创建实体类
准备工作已经做完了,下面开始jpa相关的东西。既然jpa是一个ORM框架,那么一定会有一个实体类和表对应。
新建UserEntity类
@Entity @Table(name = "tb_user") public class UserEntity { @Id @Column(name = "id") private Integer id; @Column(name = "username") private String username; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Entity 注解表示该类是一个实体类
@Table 用于设置该实体对应的表名称
@Id 用于标注该字段是主键
@Column 表示该字段为数据库一列
创建Jpa
创建一个名为UserJpa的接口,让它继承JpaRepository(SpringDataJPA简单数据操作接口)public interface UserJpa extends JpaRepository<UserEntity,Long>,Serializable{ }1
2
做完了这个操作,就可以通过UserJpa来操作数据库了。
操作数据库
先创建一个Controller作为入口来使用Jpa@RequestMapping(value = "/user") @RestController public class UserController { @Autowired private UserJpa jpa; @RequestMapping(value = "/all") public List<UserEntity> getAllUser(){ return jpa.findAll(); } @RequestMapping(value = "/save") public UserEntity saveUser(UserEntity userEntity){ return jpa.save(userEntity); } @RequestMapping(value = "/delete") public void deleteAll(UserEntity userEntity){ jpa.delete(userEntity); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
我们在使用Jpa的时候,不需要我们自己初始化,在声明jpa的地方加上@Autowired注解,这时候我们就可以使用他自带的方法了
如下
T save(T entity);//保存单个实体 Iterable<T> save(Iterable<? extends T> entities);//保存集合 T findOne(ID id);//根据id查找实体 boolean exists(ID id);//根据id判断实体是否存在 Iterable<T> findAll();//查询所有实体 long count();//查询实体数量 void delete(ID id);//根据Id删除实体 void delete(T entity);//删除一个实体 void delete(Iterable<? extends T> entities);//删除一个实体的集合 void deleteAll();//删除所有实体1
2
3
4
5
6
7
8
9
10
这里先使用简单的增|、删、改、查。
增加数据
@RequestMapping(value = "/save") public UserEntity saveUser(UserEntity userEntity){ return jpa.save(userEntity); }1
2
3
4
jpa.save会将我们传进来的实体对象保存在数据库中,并将操作的结果返回给我们。
查询数据
@RequestMapping(value = "/all") public List<UserEntity> getAllUser(){ return jpa.findAll(); }1
2
3
4
使用findAll会得到一个list对象,通过Controller的RequestMapping注解访问这个方法会直接得到一个json对象。
更新数据
更新数据和添加数据一样,都是使用jpa.save如果该主键的数据不存在,会自动向数据库添加该数据,如果该主键已经存在,会根据该主键更新该数据其他字段。
删除数据
@RequestMapping(value = "/delete") public void deleteAll(UserEntity userEntity){ jpa.delete(userEntity); }1
2
3
4
5
6
通过jap.delete会自动根据userEntity的主键删掉对应数据。
相关文章推荐
- Spring Boot学习日志(二)JPA操作Mysql
- springboot学习-springboot使用spring-data-jpa操作MySQL数据库
- SpringBoot之连接MySQL,JPA操作
- Spring Boot之使用JPA操作mysql
- SpringBoot学习之路:03.Spring Boot使用Jpa操作数据库
- spring boot 学习--04---mysql,jpa
- Spring Boot学习日志(三)再谈Jpa
- 02_Spring Boot 数据库操作 [基于Spring Boot、Mysql、Jpa]
- spring boot 学习--05---mysql,jpa,jdbcTemplate-应用
- Spring Boot+JPA+Mysql完成数据库整合操作
- Spring Boot (五)Spring Data JPA 操作 MySQL 8
- SpringBoot学习(五)操作数据库Spring-Data-JPA
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Springboot 之 使用JPA进行分页操作
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Springboot 之 JPA数据库操作进阶篇
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Spring Boot+JPA+Mysql+ThymeLeaf快速构建CURD系统(四)构建前端