170622、springboot编程之JPA操作数据库
JPA操作数据库
什么事JAP?JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
1、在pom中引用jpa
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2、配置application.properties文件
###datasource 数据源配置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
###Java Persistence Api
#指定的数据库
spring.jpa.database=MYSQL
#显示每个SQL
spring.jpa.show-sql=true
#Hibernate DDL自动(创建,创建-删除,更新)
spring.jpa.hibernate.ddl-auto=update
#命名策略
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
#在将其添加到实体管理剥离
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
3、编写UserRepository.java文件
package com.rick.apps.repository; import com.rick.apps.entity.User;
import org.springframework.data.repository.CrudRepository;
/**
* Desc : CrudRepository是springboot提供的封装方法
* 里面封装了基本的增删改查的方法,方便调用
* 方法如下:
* <S extends T> S save(S var1);
<S extends T> Iterable<S> save(Iterable<S> var1);
T findOne(ID var1);
boolean exists(ID var1);
Iterable<T> findAll();
Iterable<T> findAll(Iterable<ID> var1);
long count();
void delete(ID var1);
void delete(T var1);
void delete(Iterable<? extends T> var1);
void deleteAll();
* User : RICK
* Time : 2017/8/21 15:45
*/ public interface UserRepository extends CrudRepository<User,Integer> { }
4、编写UserService.java
import com.rick.apps.entity.User;
import com.rick.apps.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; @Service
public class UserService { @Autowired
private UserRepository userRepository; @Transactional
public void save(User user){
userRepository.save(user);
}
}
注:我只写了一个保存方法,其他的调用方法雷同,再次不一一展示
5、编写HelloController.java
package com.rick.apps.controller; import com.rick.apps.entity.User;
import com.rick.apps.service.UserService;
import com.rick.common.ResultJson;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController
public class HelloController { @Resource
private UserService userService; @RequestMapping("/hello")
public String hello(){
System.out.println(1/0);
return "Hello World!";
} @GetMapping(value = "/save")
public ResultJson saveDemo(){
User user = new User();
user.setUserName("rick");
user.setPassWord("123456");
userService.save(user);
ResultJson resultJson = ResultJson.buildSuccessInstance();
return resultJson;
}
}
注:ResultJson是我自己封装的一个返回对象类,方便大家测试,我也把这个类的代码给贴出来
package com.rick.common;
import java.io.Serializable;
public class ResultJson implements Serializable{
private static final long serialVersionUID = 1L;
//返回码
private String code;
//返回提示
private String msg;
//返回内容
private Object data;
public ResultJson(){
}
public ResultJson(String msg){
this.code = "1";
this.msg = msg;
}
public ResultJson(Object data){
this.code = "0";
this.msg = "成功";
this.data = data;
}
public ResultJson(String code, String msg){
this.code = code;
this.msg = msg;
}
public ResultJson(String code, String msg, Object data){
this.code = code;
this.msg = msg;
this.data = data;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static ResultJson buildSuccessInstance(){
return new ResultJson("0", "操作成功!", null);
}
public static ResultJson buildSuccessInstance(String msg){
return new ResultJson("0", msg, null);
}
public static ResultJson buildSuccessInstance(Object data){
return new ResultJson("0", "操作成功!", data);
}
public static ResultJson buildSuccessInstance(String msg, Object data){
return new ResultJson("0", msg, data);
}
public static ResultJson buildFailInstance(){
return new ResultJson("1", "操作异常!", null);
}
public static ResultJson buildFailInstance(String msg){
return new ResultJson("1", msg, null);
}
/**
* TODO: 自定义返回异常信息
* @Auhor: RICK
* @Date : 2017年6月26日
* @param code 99 表示没有获取到经纬度信息
* @param msg
* @return
*/
public static ResultJson buildFailInstance(String code,String msg){
return new ResultJson(code, msg, null);
}
}
6、启动项目测试

数据库添加数据成功:

项目截图

170622、springboot编程之JPA操作数据库的更多相关文章
- 170623、springboot编程之JdbcTemplate操作数据库
使用JdbcTemplate操作mysql数据库! 1.在pom中引入jpa包 <dependency> <groupId>org.springframework.boot&l ...
- 第12章—使用NoSQL数据库—使用MongoDB+Jpa操作数据库
使用MongoDB+Jpa操作数据库 SpringData还提供了对多种NoSQL数据库的支持,包括MongoDB;neo4j和redis.他不仅支持自动化的repository,还支持基于模板的数据 ...
- JAVA - SpringBoot项目引用MyBatis操作数据库
JAVA - SpringBoot项目引用MyBatis操作数据库 1. 创建SpringBoot项目,参考:https://www.cnblogs.com/1285026182YUAN/p/1232 ...
- 界面编程之QT的数据库操作20180801
/*******************************************************************************************/ 一.数据库连 ...
- 一篇搞定spring Jpa操作数据库
开始之前你必须在项目配置好数据库,本文使用的spring boot,相比spring,spring boot省去了很多各种对以来组件复杂的配置,直接在pom配置组件,完后会自动帮我们导入组件 < ...
- 170717、springboot编程之mybatis数据库开发和aop拦截
一.springboot整合mybaits (1)新建maven project; 新建一个maven project,取名为:spring-boot-mybatis (2)在pom.xml文件中引入 ...
- 使用spring boot中的JPA操作数据库
前言 Spring boot中的JPA 使用的同学都会感觉到他的强大,简直就是神器一般,通俗的说,根本不需要你写sql,这就帮你节省了很多时间,那么下面我们来一起来体验下这款神器吧. 一.在pom中添 ...
- spring-boot-route(九)整合JPA操作数据库
单调的增删改查让越来越多的程序员感到乏味,这时候就出现了很多优秀的框架,完成了对增删改查操作的封装,只需要简单配置,无需书写任何sql,就可以完成增删改查.这里比较推荐的是Spring Data Jp ...
- 170720、springboot编程之properties文件讲解
但是在实际开发过程中有更复杂的需求,我们在对properties进一步的升华.在本篇博客中您将会学到如下知识(这节中有对之前的知识的温故,对之前的升华): (1) 在application.prope ...
随机推荐
- 【C#】【MySQL】C# 查询数据库语句@Row:=@Row+1以及执行存储过程失败解决方案
如何实现数据库查询产生虚拟的一列序号的功能: ) )AS r; 该语句可以实现产生虚拟的一列数据在MySQL中运行没有问题. 但是在C#里面调用去出现了错误"Parameter '@ROW' ...
- C++ Primer学习笔记(二)
题外话:一工作起来就没有大段的时间学习了,如何充分利用碎片时间是个好问题. 接 C++ Primer学习笔记(一) 27.与 vector 类型相比,数组的显著缺陷在于:数组的长度是固定的,无法 ...
- 调用ffmpeg库编译时出现common.h:175:47: error: 'UINT64_C' was not declared in this scope
解决办法 出现错误:jni/ffmpeg/libavutil/common.h:175:47: error: 'UINT64_C' was not declared in this scope 解决: ...
- CentOS运维常用管理操作命令
自己整理的整理Linux常用运维和linux常用管理操作命令,当然不是非常详细和丰富,但是也基本上够用了吧.欢迎留言补充更多的Linux常用运维和linux常用管理操作命令.不断完善中.... 备份m ...
- CentOS基础命令大全
1.关机 (系统的关机.重启以及登出 ) 的命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours: ...
- 用json在java和C#之间传递base64的问题。。。
记录下..唉.... java代码: 导入这个 commons-codec-1.8.jar (下载链接: http://files.cnblogs.com/files/gaocong/jar%E5%8 ...
- (转)Spring开启Annotation<context:annotation-config> 和 <context:component-scan>诠释及区别
转自:https://www.cnblogs.com/leiOOlei/p/3713989.html <context:annotation-config> 和 <context:c ...
- DLL文件的使用
一. 动态链接库 什么是动态链接库?DLL三个字母对于你来说一定很熟悉吧,它是Dynamic Link Library 的缩写形式,动态链接库 (DLL) 是作为共享函数库的可执行文件.动态链接提供了 ...
- centos7,yum安装工具报错
1.问题描述:yum安装gcc和其他的工具时一直报错: 2.问题解决: 网上看到有类似文章: No more mirrors to try. 得知这可能是错误的缓存源导致,直接两个命令解决: yum ...
- CSS定位背景图片 background-position
网站的样式的时候经常会发现一种情况,就是在很多background属性里都调用同一张图片,来满足网页各个部分的使用.打开这种图片看一下,会发现这张图片上包含了很多小图片; 又如: 这些小图片就是整图分 ...