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书写

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.easytest</groupId>
 7     <artifactId>rebushu</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>rebushu</name>
12     <url>http://maven.apache.org</url>
13     <description>Demo project for Spring Boot</description>
14 
15     <parent>
16         <groupId>org.springframework.boot</groupId>
17         <artifactId>spring-boot-starter-parent</artifactId>
18         <version>1.5.9.RELEASE</version>
19         <relativePath/> <!-- lookup parent from repository -->
20     </parent>
21 
22     <properties>
23         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25         <java.version>1.8</java.version>
26     </properties>
27 
28     <dependencies>
29         <dependency>
30             <groupId>org.springframework.boot</groupId>
31             <artifactId>spring-boot-starter-web</artifactId>
32         </dependency>
33 
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-test</artifactId>
37             <scope>test</scope>
38         </dependency>
39         <dependency>
40             <groupId>com.alibaba</groupId>
41             <artifactId>fastjson</artifactId>
42             <version>1.2.15</version>
43         </dependency>
44 
45         <dependency>
46             <groupId>org.springframework.boot</groupId>
47             <artifactId>spring-boot-starter-jdbc</artifactId>
48         </dependency>
49 
50 
51         <!-- 添加fastjson 依赖包. -->
52         <dependency>
53             <groupId>com.alibaba</groupId>
54             <artifactId>fastjson</artifactId>
55             <version>1.2.15</version>
56         </dependency>
57 
58         <!-- spring boot devtools 依赖包. -->
59         <dependency>
60             <groupId>org.springframework.boot</groupId>
61             <artifactId>spring-boot-devtools</artifactId>
62             <optional>true</optional>
63             <scope>true</scope>
64         </dependency>
65 
66         <!-- 添加MySQL数据库驱动依赖包. -->
67         <dependency>
68             <groupId>mysql</groupId>
69             <artifactId>mysql-connector-java</artifactId>
70         </dependency>
71 
72         <!-- 添加Spring-data-jpa依赖. -->
73         <dependency>
74             <groupId>org.springframework.boot</groupId>
75             <artifactId>spring-boot-starter-data-jpa</artifactId>
76         </dependency>
77 
78     </dependencies>
79 
80     <!--构建节点-->
81     <build>
82         <plugins>
83             <plugin>
84                 <groupId>org.springframework.boot</groupId>
85                 <artifactId>spring-boot-maven-plugin</artifactId>
86                 <configuration>
87                 <!--fork :  如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
88                 <fork>true</fork>
89                 </configuration>
90             </plugin>
91 
92         </plugins>
93     </build>
94 
95 </project>
View Code

    2、实体类

 1 package com.easytest;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.GenerationType;
 6 import javax.persistence.Id;
 7 
 8 /**
 9  * Created by liuya on 2018-01-26.
10  */
11 @Entity
12 public class Cat {
13 
14     /**
15      * 使用@Id指定主键.
16      * <p>
17      * 使用代码@GeneratedValue(strategy=GenerationType.AUTO)
18      * 指定主键的生成策略,mysql默认的是自增长。
19      */
20     @Id
21     @GeneratedValue(strategy = GenerationType.AUTO)
22     private int id;//主键.
23 
24     private String catName;//姓名. cat_name
25 
26     private int catAge;//年龄. cat_age;
27 
28     public int getId() {
29         return id;
30     }
31 
32     public void setId(int id) {
33         this.id = id;
34     }
35 
36     public String getCatName() {
37         return catName;
38     }
39 
40     public void setCatName(String catName) {
41         this.catName = catName;
42     }
43 
44     public int getCatAge() {
45         return catAge;
46     }
47 
48     public void setCatAge(int catAge) {
49         this.catAge = catAge;
50     }
51 
52 }
View Code

   3、修改application配置文档

 1 #######################################################
 2 ##datasource -- u6307u5B9Amysqlu6570u636Eu5E93u8FDEu63A5u4FE1u606F.
 3 #######################################################
 4 spring.datasource.url = jdbc:mysql://localhost:3306/test
 5 spring.datasource.username = root
 6 spring.datasource.password = 123456
 7 spring.datasource.driverClassName = com.mysql.jdbc.Driver
 8 spring.datasource.max-active=20
 9 spring.datasource.max-idle=8
10 spring.datasource.min-idle=8
11 spring.datasource.initial-size=10
12 
13 
14 ########################################################
15 ### Java Persistence Api --  Spring jpau7684u914Du7F6Eu4FE1u606F.
16 ########################################################
17 # Specify the DBMS
18 spring.jpa.database = MYSQL
19 # Show or not log for each sql query
20 spring.jpa.show-sql = true
21 # Hibernate ddl auto (create, create-drop, update)
22 spring.jpa.hibernate.ddl-auto = update
23 # Naming strategy
24 #[org.hibernate.cfg.ImprovedNamingStrategy  #org.hibernate.cfg.DefaultNamingStrategy]
25 spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
26 # stripped before adding them to the entity manager)
27 spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
View Code

   4、测试服务器开启

 1 package com.easytest;
 2 
 3 import com.alibaba.fastjson.serializer.SerializerFeature;
 4 import com.alibaba.fastjson.support.config.FastJsonConfig;
 5 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
 6 import org.springframework.boot.SpringApplication;
 7 import org.springframework.boot.autoconfigure.SpringBootApplication;
 8 import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
 9 import org.springframework.context.annotation.Bean;
10 import org.springframework.http.MediaType;
11 import org.springframework.http.converter.HttpMessageConverter;
12 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
13 
14 import java.util.ArrayList;
15 import java.util.List;
16 
17 @SpringBootApplication
18 public class RebushuApplication {
19 
20     /**
21      * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert
22      * @return
23      */
24      
25     @Bean
26     public HttpMessageConverters fastJsonHttpMessageConverters() {
27         // 1、需要先定义一个 convert 转换消息的对象;
28         FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
29 
30         //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
31         FastJsonConfig fastJsonConfig = new FastJsonConfig();
32         fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
33 
34         //3、在convert中添加配置信息.
35         fastConverter.setFastJsonConfig(fastJsonConfig);
36 
37 
38         HttpMessageConverter<?> converter = fastConverter;
39         return new HttpMessageConverters(converter);
40     }
41 
42 
43 
44     public static void main(String[] args) {
45         SpringApplication.run(RebushuApplication.class, args);
46     }
47 }
View Code

   5、启动服务后,查看数据库

  

原文地址:https://www.cnblogs.com/liuyangfirst/p/8360275.html