IntelliJ IDEA 2017版 spring-boot 实现jpa基本部署,通过实体类自动建立数据库
一、添加Spring Boot JPA-Hibernate步骤
1、在pom.xml添加mysql,spring-data-jpa依赖
2、在application.properties文件中配置mysql连接配置文件
3、在application.properties文件中配置JPA配置信息
4、编写测试例子
二、实战例子
1、部署文件pom.xml书写
<?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.easytest</groupId>
<artifactId>rebushu</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>rebushu</name>
<url>http://maven.apache.org</url>
<description>Demo project for Spring Boot</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-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <!-- 添加fastjson 依赖包. -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency> <!-- spring boot devtools 依赖包. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency> <!-- 添加MySQL数据库驱动依赖包. -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!-- 添加Spring-data-jpa依赖. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependencies> <!--构建节点-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
<fork>true</fork>
</configuration>
</plugin> </plugins>
</build> </project>
2、实体类
package com.easytest; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; /**
* Created by liuya on 2018-01-26.
*/
@Entity
public class Cat { /**
* 使用@Id指定主键.
* <p>
* 使用代码@GeneratedValue(strategy=GenerationType.AUTO)
* 指定主键的生成策略,mysql默认的是自增长。
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;//主键. private String catName;//姓名. cat_name private int catAge;//年龄. cat_age; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getCatName() {
return catName;
} public void setCatName(String catName) {
this.catName = catName;
} public int getCatAge() {
return catAge;
} public void setCatAge(int catAge) {
this.catAge = catAge;
} }
3、修改application配置文档
#######################################################
##datasource -- \u6307\u5B9Amysql\u6570\u636E\u5E93\u8FDE\u63A5\u4FE1\u606F.
#######################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
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\u7684\u914D\u7F6E\u4FE1\u606F.
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
4、测试服务器开启
package com.easytest; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.ArrayList;
import java.util.List; @SpringBootApplication
public class RebushuApplication { /**
* 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert
* @return
*/ @Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
} public static void main(String[] args) {
SpringApplication.run(RebushuApplication.class, args);
}
}
5、启动服务后,查看数据库

IntelliJ IDEA 2017版 spring-boot 实现jpa基本部署,通过实体类自动建立数据库的更多相关文章
- IntelliJ IDEA 2017版 spring-boot使用Spring Data JPA搭建基础版的三层架构
1.配置环境pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- 玩转spring boot——结合JPA入门
参考官方例子:https://spring.io/guides/gs/accessing-data-jpa/ 接着上篇内容 一.小试牛刀 创建maven项目后,修改pom.xml文件 <proj ...
- 玩转spring boot——结合JPA事务
接着上篇 一.准备工作 修改pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- spring boot 系列之四:spring boot 整合JPA
上一篇我们讲了spring boot 整合JdbcTemplate来进行数据的持久化, 这篇我们来说下怎么通过spring boot 整合JPA来实现数据的持久化. 一.代码实现 修改pom,引入依赖 ...
- Spring Boot集成JPA的Column注解命名字段无效的问题
偶然发现,Spring Boot集成jpa编写实体类的时候,默认使用的命名策略是下划线分隔的字段命名. Spring Boot版本:1.5.4.release 数据表: id int, userNam ...
- Spring Boot整合JPA、Redis和Swagger2
好久没有总结了,最近也一直在学习.今天就把spring boot与其它技术的整合做个小总结,主要是jpa.redis和swagger2.公司里有用到这些,整合起来也很简单. 首先,新建一个Spring ...
- Spring boot data JPA数据库映射关系 : @OneToOne,@OneToMany,@ManyToMany
问题描述 在利用Spring boot data JPA进行表设计的时候,表对象之间经常存在各种映射关系,如何正确将理解的映射关系转化为代码中的映射关系是关键之处. 解决办法 概念理解 举例:在公司的 ...
- Spring Boot 整合 JPA 使用多个数据源
介绍 JPA(Java Persistence API)Java 持久化 API,是 Java 持久化的标准规范,Hibernate 是持久化规范的技术实现,而 Spring Data JPA 是在 ...
- Spring Boot2 系列教程(二十四)Spring Boot 整合 Jpa
Spring Boot 中的数据持久化方案前面给大伙介绍了两种了,一个是 JdbcTemplate,还有一个 MyBatis,JdbcTemplate 配置简单,使用也简单,但是功能也非常有限,MyB ...
随机推荐
- 定时器setInterval, innerText获取文本, charAt()获取单个字符串, substring(1, content.length)获取范围内的字符串, 实现字符串的滚动效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 19 python unit4 常用模块练习题&总结
1.logging模块有几个日志级别? logging模块共有5个级别,分别是: DEBUG INFO WARNING ERROR CRITICAL logging的日志可以分为 debug(), ...
- Haskell语言学习笔记(40)Arrow(1)
Arrow class Category a => Arrow a where arr :: (b -> c) -> a b c first :: a b c -> a (b, ...
- URLconf
URLconf 浏览者通过在浏览器的地址栏中输入网址请求网站,对于Django开发的网站,由哪一个视图进行处理请求,是由url匹配找到的 配置 在test3/settings.py中通过ROOT_UR ...
- oracle 网络环境配置
PLSQL Developer连接Oracle11g 64位数据库配置详解 最近换了台64bit的电脑,所以oracle数据库也跟着换成了64bit的,不过 问题也随之产生,由于plsql devel ...
- HTML5 画图--文字
1:html <div style="margin:0 auto;width:794px;height:1123px"> <canvas id="myC ...
- EasyUI多选的获取
function deletePRE() { var rows = $('#dg').datagrid('getSelections'); var ids = []; var other_ids = ...
- 联想笔记本Win10 F1-F12失效的解决方法
最近换了笔记本,用的是win10,发现F1到F12不生效. 比如玩游戏时,按F1没有切换到自己角色上,编程运行代码时的shift+F10也不行. 后来发现,这是因为某些笔记本的Fn功能键默认的不是传统 ...
- hbase概念
1. 概述(扯淡~) HBase是一帮家伙看了Google发布的一片名为“BigTable”的论文以后,犹如醍醐灌顶,进而“山寨”出来的一套系统. 由此可见: 1. 几乎所有的HBase中的理念,都可 ...
- Intellij IDEA使用Maven搭建spark开发环境(scala)
如何一步一步地在Intellij IDEA使用Maven搭建spark开发环境,并基于scala编写简单的spark中wordcount实例. 1.准备工作 首先需要在你电脑上安装jdk和scala以 ...