上一篇介绍了springboot简单整合mybatis的教程。这一篇是介绍springboot简单整合jpa的教程。

由于jpa的功能强大,后续会继续写关于jpa的介绍已经使用,本文只是简单介绍一下它与springboot的整合。

jpa不需要像mybatis一样创建表,首先给大家看一下application.properties文件代码,其中包含了jpa的配置和数据库配置,尤其注意一下spring.jpa.hibernate.ddl-auto属性,代码如下:

##端口号
server.port=8888 ##数据库配置
##数据库地址
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
##数据库用户名
spring.datasource.username=root
##数据库密码
spring.datasource.password=root
##数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver ##validate 加载hibernate时,验证创建数据库表结构
##create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
##create-drop 加载hibernate时创建,退出是删除表结构
##update 加载hibernate自动更新数据库结构
##validate 启动时验证表的结构,不会创建表
##none 启动时不做任何操作
spring.jpa.hibernate.ddl-auto=create ##控制台打印sql
spring.jpa.show-sql=true

启动类application

package com.dalaoyang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class SpringbootJpaApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootJpaApplication.class, args);
}
}

pom文件大致和整合mybatis一样,只是把其中的mybatis改成了jpa,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.dalaoyang</groupId>
<artifactId>springboot_jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboot_jpa</name>
<description>springboot_jpa</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

实体类city,其中@Table中的name对应数据库中表的名称

package com.dalaoyang.entity;

import javax.persistence.*;

/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.Entity
* @email 397600342@qq.com
* @date 2018/4/7
*/
@Entity
@Table(name="city")
public class City { @Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int cityId;
private String cityName;
private String cityIntroduce; public City(int cityId, String cityName, String cityIntroduce) {
this.cityId = cityId;
this.cityName = cityName;
this.cityIntroduce = cityIntroduce;
} public City(String cityName, String cityIntroduce) {
this.cityName = cityName;
this.cityIntroduce = cityIntroduce;
} public City() {
} public int getCityId() {
return cityId;
} public void setCityId(int cityId) {
this.cityId = cityId;
} public String getCityName() {
return cityName;
} public void setCityName(String cityName) {
this.cityName = cityName;
} public String getCityIntroduce() {
return cityIntroduce;
} public void setCityIntroduce(String cityIntroduce) {
this.cityIntroduce = cityIntroduce;
}
}

然后就是jpa的重要地方,CityRepository,继承了JpaRepository,

由于本文只是简单介绍了jpa的简单功能,所以JpaRepository中内置的方法已经足够使用。

代码如下:

package com.dalaoyang.repository;

import com.dalaoyang.entity.City;
import org.springframework.data.jpa.repository.JpaRepository; /**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.Repository
* @email 397600342@qq.com
* @date 2018/4/7
*/
public interface CityRepository extends JpaRepository<City,Integer> {
}

最后是controller,里面和mybatis整合一样,方法上面写的就是对应的测试方法。

package com.dalaoyang.controller;

import com.dalaoyang.entity.City;
import com.dalaoyang.repository.CityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.controller
* @email 397600342@qq.com
* @date 2018/4/7
*/
@RestController
public class CityController { @Autowired
private CityRepository cityRepository; //http://localhost:8888/saveCity?cityName=北京&cityIntroduce=中国首都
@GetMapping(value = "saveCity")
public String saveCity(String cityName,String cityIntroduce){
City city = new City(cityName,cityIntroduce);
cityRepository.save(city);
return "success";
} //http://localhost:8888/deleteCity?cityId=2
@GetMapping(value = "deleteCity")
public String deleteCity(int cityId){
cityRepository.delete(cityId);
return "success";
} //http://localhost:8888/updateCity?cityId=3&cityName=沈阳&cityIntroduce=辽宁省省会
@GetMapping(value = "updateCity")
public String updateCity(int cityId,String cityName,String cityIntroduce){
City city = new City(cityId,cityName,cityIntroduce);
cityRepository.save(city);
return "success";
} //http://localhost:8888/getCityById?cityId=3
@GetMapping(value = "getCityById")
public City getCityById(int cityId){
City city = cityRepository.findOne(cityId);
return city;
}
}

到这里启动项目就可以简单测试一下整合的效果了。

SpringBoot+Jpa+MySql学习的更多相关文章

  1. springboot+jpa+mysql+redis+swagger整合步骤

    springboot+jpa+MySQL+swagger框架搭建好之上再整合redis: 在电脑上先安装redis: 一.在pom.xml中引入redis 二.在application.yml里配置r ...

  2. springboot+jpa+mysql+swagger整合

    Springboot+jpa+MySQL+swagger整合 创建一个springboot web项目 <dependencies> <dependency>      < ...

  3. spring-boot jpa mysql emoji utfmb4 异常处理

    spring-boot jpa mysql utf8mb4 emoji 写入失败 mysql database,table,column 默认为utf8mb4 Caused by: java.sql. ...

  4. IDEA SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统

    先放上github地址:spike-system,可以直接下载完整项目运行测试 SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统 技术栈:SpringBoot, MyS ...

  5. Springboot Jpa: [mysql] java.sql.SQLException: Duplicate entry 'XXX' for key 'PRIMARY'

    前言 1.问题背景 偶尔会出现登录请求出错的情况,一旦失败就会短时间内再也登录不上,更换浏览器或者刷新可能会暂时解决这个问题. 项目运行日志如下: 2022-07-21 09:43:40.946 DE ...

  6. SpringBoot+Mybatis+MySql学习

    介绍一下SpringBoot整合mybatis,数据库选用的是mysql. 首先创建数据库 CREATE DATABASE test; 建表以及插入初始数据(sql是从navicat中导出的) SET ...

  7. springboot JPA mysql

    官方文档 https://docs.spring.io/spring-data/jpa/docs/1.11.10.RELEASE/reference/html/ 常用关键字 通常,JPA的查询创建机制 ...

  8. SpringBoot中JPA的学习

    SpringBoot中JPA的学习 准备环境和项目配置 写一下学习JPA的过程,主要是结合之前SpringBoot + Vue的项目和网上的博客学习一下. 首先,需要配置一下maven文件,有这么两个 ...

  9. Springboot+Atomikos+Jpa+Mysql实现JTA分布式事务

    1 前言 之前整理了一个spring+jotm实现的分布式事务实现,但是听说spring3.X后不再支持jotm了,jotm也有好几年没更新了,所以今天整理springboot+Atomikos+jp ...

随机推荐

  1. 华为手机浏览器 onclick失灵的问题

    开发h5 遇到的问题是华为浏览器onclick 点击失灵. 下面这个网站是检查 浏览器是否支持es6语法的网站 http://ruanyf.github.io/es-checker/index.cn. ...

  2. CentOS 7 部署 Spring Boot

    Spring Boot 内嵌了tomcat .我们可以将Boot打成 jar 包丢到服务器上运行才行. Spring Boot已经帮我们打理好了这一切,如果项目是继承自 spring-boot-sta ...

  3. swoole 使用异步redis的前置条件

    redis安装 官网下载redis 下载完成之后解压: 进入redis目录执行make: 进入src目录启动redis 启动成功如下: 启动后连接redis 编译安装hiredis 下载:https: ...

  4. 雅礼 noip2018 模拟赛day3 T2

    典型的状压思想 设0表示黑球,1表示白球,用一串01序列代表剩下的球的状态,记f[i]表示在i状态下取球的最大期望 那么可以利用记忆化搜索更新,每一层枚举可能拿走的球然后向下搜索,同时记忆化即可 在状 ...

  5. pycharm导入本地py文件时,模块下方出现红色波浪线时如何解决

    有时候导入本地模块或者py文件时,下方会出现红色的波浪线,但不影响程序的正常运行,但是在查看源函数文件时,会出现问题 问题如下:  解决方案: 1. 进入设置,找到Console下的Python Co ...

  6. ServerSocket实现超简单HTTP服务器

    1.相关知识简介 HTTP协议 HTTP是常用的应用层协议之一,是面向文本的协议.HTTP报文传输基于TCP协议,TCP协议包含头部与数据部分,而HTTP则是包含在TCP协议的数据部分,如下图 HTT ...

  7. models批量生成数据

    models批量生成数据 1.将数据生成为 列表序列,通过 bulk_create 将数据一次插入数据库中 def host(request): # 插入数据速度快消耗资源少 Hostlist=[] ...

  8. 20165323 实验一 Java开发环境的熟悉

    一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:杨金川 学号:20165323 指导教师:娄嘉鹏 实验日期:2018年4月2日 实验时间:13:45 - 15:25 实验序号:一 实 ...

  9. 金蝶核算项目余额表卡号余额与天财商龙CRM卡号余额对比

    金蝶核算项目余额表卡号余额与天财尚龙CRM卡号余额对比 由于历史遗留问题,财务一直不调账,修改核算科目卡号与天财商龙CRM系统一直,只能用VBA把卡号前缀修改成两边一致. 再通过,Power BI D ...

  10. MySQL主从备份配置实例

    转载自:https://www.cnblogs.com/ahaii/p/6307648.html MySQL主从备份配置实例 场景: 1.主服务器192.168.0.225.从服务器192.168.0 ...