博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot+Jpa+MySql学习
阅读量:7192 次
发布时间:2019-06-29

本文共 6165 字,大约阅读时间需要 20 分钟。

上一篇介绍了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##控制台打印sqlspring.jpa.show-sql=true

启动类application

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

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

4.0.0
com.dalaoyang
springboot_jpa
0.0.1-SNAPSHOT
jar
springboot_jpa
springboot_jpa
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin

实体类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
{}

最后是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 */@RestControllerpublic 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;    }}

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

源码下载 :

个人网站:

转载地址:http://vjxkm.baihongyu.com/

你可能感兴趣的文章
使用Gitolite搭建Gitserver
查看>>
【Maven】Snapshot和Release版本的区别
查看>>
FZU1920 Left Mouse Button(dfs)
查看>>
迭代器模式
查看>>
poj 1979 dfs
查看>>
(转)论python工厂函数与内建函数
查看>>
HDU1212 Big Number 【同余定理】
查看>>
HDU1069(还是dp基础)
查看>>
用RotateDrawable实现网易云音乐唱片机效果
查看>>
Mac OS X各版本号的历史费用和升级关系
查看>>
Android -- AsyncTask源码解析
查看>>
[转]Grunt 新手一日入门
查看>>
牛客网Java刷题知识点之四种不同的方式创建线程
查看>>
搜索分析(DFS、BFS、递归、记忆化搜索)
查看>>
服务容错保护断路器Hystrix之一:入门示例介绍(springcloud引入Hystrix的两种方式)...
查看>>
vue2.0 之 douban (七)APP 打包
查看>>
Android开源项目——设置图文居中的按钮 IconButton
查看>>
raw格式转换成qcow2格式
查看>>
volley4--RequestQueue
查看>>
python3 实现mysql数据库连接池
查看>>