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. 关于解决用tutorial7教程中的代码打造一款自己的播放器中的声音噪音问题

    ////////////////////////////////////////////////////////////////////////////////////////////对于用FFMPE ...

  2. c++ 静态成员变量

    在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量只存储一份供所有对象共用.所以在所有对象中都可以共享它.使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还可以节 ...

  3. js使用正则表达式从url中获取参数值

    //从url中获取参数值 function getvl(name) { var reg = new RegExp("(^|\\?|&)"+ name +"=([^ ...

  4. c++ define的用法(转)

    #define是C语言中提供的宏定义命令,其主要目的是为程序员在编程时提供一定的方便,并能在一定程度上提高程序的运行效率,但学生在学习时往往不能 理解该命令的本质,总是在此处产生一些困惑,在编程时误用 ...

  5. SharePoint 2013 网站迁移流程

    在新的Farm(场)里,创建一个新的Web Application(网站应用程序),不需要创建Site Collection(网站集) Copy(复制)自定义开发的WSP包到新的Farm Server ...

  6. 不定义JQuery插件 不要说会JQuery

    二.普及JQuery知识 知识1:用JQuery写插件时,最核心的方法有如下两个: $.extend(object) 可以理解为JQuery 添加一个静态方法. $.fn.extend(object) ...

  7. mysql中,root用户密码被遗忘,该如何进行重置?

    需求描述: 在mysql的测试环境中,有时候会遇到一段时间之后root用户的密码被遗忘的情况, 这个时候,就是需要对root密码进行重置,不过,在生产环境中,这种情况还是很少见. 环境描述: 操作系统 ...

  8. 超全面的JavaWeb笔记day04<dom树等>

    1.案例:在末尾添加节点(*****) 创建标签 createElement方法 创建文本 createTextNode方法 把文本添加到标签下面 appendChild方法 2.元素对象(了解) 如 ...

  9. HTML的框架结构

    <html> <head> <title>HTML的框架结构</title> </head> <frameset frameborde ...

  10. swift--设置app图标和启动页面

    1,如下图: