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操作数据库的更多相关文章

  1. 170623、springboot编程之JdbcTemplate操作数据库

    使用JdbcTemplate操作mysql数据库! 1.在pom中引入jpa包 <dependency> <groupId>org.springframework.boot&l ...

  2. 第12章—使用NoSQL数据库—使用MongoDB+Jpa操作数据库

    使用MongoDB+Jpa操作数据库 SpringData还提供了对多种NoSQL数据库的支持,包括MongoDB;neo4j和redis.他不仅支持自动化的repository,还支持基于模板的数据 ...

  3. JAVA - SpringBoot项目引用MyBatis操作数据库

    JAVA - SpringBoot项目引用MyBatis操作数据库 1. 创建SpringBoot项目,参考:https://www.cnblogs.com/1285026182YUAN/p/1232 ...

  4. 界面编程之QT的数据库操作20180801

    /*******************************************************************************************/ 一.数据库连 ...

  5. 一篇搞定spring Jpa操作数据库

    开始之前你必须在项目配置好数据库,本文使用的spring boot,相比spring,spring boot省去了很多各种对以来组件复杂的配置,直接在pom配置组件,完后会自动帮我们导入组件 < ...

  6. 170717、springboot编程之mybatis数据库开发和aop拦截

    一.springboot整合mybaits (1)新建maven project; 新建一个maven project,取名为:spring-boot-mybatis (2)在pom.xml文件中引入 ...

  7. 使用spring boot中的JPA操作数据库

    前言 Spring boot中的JPA 使用的同学都会感觉到他的强大,简直就是神器一般,通俗的说,根本不需要你写sql,这就帮你节省了很多时间,那么下面我们来一起来体验下这款神器吧. 一.在pom中添 ...

  8. spring-boot-route(九)整合JPA操作数据库

    单调的增删改查让越来越多的程序员感到乏味,这时候就出现了很多优秀的框架,完成了对增删改查操作的封装,只需要简单配置,无需书写任何sql,就可以完成增删改查.这里比较推荐的是Spring Data Jp ...

  9. 170720、springboot编程之properties文件讲解

    但是在实际开发过程中有更复杂的需求,我们在对properties进一步的升华.在本篇博客中您将会学到如下知识(这节中有对之前的知识的温故,对之前的升华): (1) 在application.prope ...

随机推荐

  1. div 点击展开

    <script type="text/javascript" src="http://libs.baidu.com/jquery/2.1.1/jquery.min. ...

  2. 【转】WCF入门教程一[什么是WCF]

    一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...

  3. 让Android 变身回一台真正的Linux系统吧!!!

    在Android上开发也有两年的时间了,虽然一直都知道Android是构建在Linux Kernel上的手机操作系统,但在此之前一直没有实感. 直到第一次买了Android的手机,并请人帮我Root后 ...

  4. 【AngularJS】AngularJS整合Springmvc、Mybatis环境搭建

    近期想学习AngularJS的使用,网上搜了一圈后,折腾了半天解决bug后,成功使用AngularJS整合Springmvc.Spring.Mybatis搭建了一个开发环境.(这里Spring使用的版 ...

  5. erlang随机数问题

    一.计算机的随机数的老问题,伪随机数. random:seed() random:uniform(N) 如果seed是相同的,则第M次执行 random:uniform(N) .M.N相同,则得到的随 ...

  6. zabbix-agent 自动注册

    1. 概述 上一篇内容<zabbix自动发现配置>,大概内容是zabbix server去扫描一个网段,把在线的主机添加到Host列表中.我们本篇内容与上篇相反,这次是Active age ...

  7. 远程数据库备份到本地出现“Access denied for user 'root'@localhost(using password: YES)”的问题

    由于另外一个人在用远程的server做测试,导致我访问这个远程机器的mysql提示“too many connections”的问题,于是想到干脆把数据库当下来做测试好了,结果用heidiSQLs进行 ...

  8. 如何POST一个JSON格式的数据给Restful服务

    在Android/java平台上实现POST一个json数据: JSONObject jsonObj = new JSONObject(); jsonObj.put("username&qu ...

  9. 更改VS2010的[默认开发语言]

    1.菜单-->"工具"-->"导入导出设置".例如以下图: 2.选择"重置全部设置",例如以下图: 3.重置设置,例如以下图: ...

  10. ios开发之--从相机或相册选取图片,并带删除操作的demo

    多选图片的一个效果,如下图: