博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring boot data jpa 示例
阅读量:4350 次
发布时间:2019-06-07

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

一、maven pom.xml文件

4.0.0
com.ltchu
design
0.0.1-SNAPSHOT
jar
design
ltchu's graduation design
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa

      <dependency>

        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>

mysql
mysql-connector-java
org.springframework.boot
spring-boot-maven-plugin

 

二、properties配置文件

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/design?characterEncoding=UTF-8&useSSL=falsespring.datasource.username = rootspring.datasource.password = rootspring.datasource.driver-class-name = com.mysql.jdbc.Driver#建表策略,这里用update,即根据实体更新表结构spring.jpa.hibernate.ddl-auto = update

#thymeleaf模板

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false

 

三、实体类

package com.ltchu.design.model;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;@Entity@Table(name="person")public class Person {    @Id    //本来jpa是不支持uuid的,但借用hibernate的方法可以实现。    @GeneratedValue(generator = "uuid")    @GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDHexGenerator")    private String id;        private String name;       public Person(String id, String name) {        super();        this.id = id;        this.name = name;    }   ...  setter / getter方法 }

 

四、Repository

package com.ltchu.design.repository;import org.springframework.data.repository.CrudRepository;import com.ltchu.design.model.Person;public interface PersonRepository extends CrudRepository
{}

 

五、TestController

package com.ltchu.design.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.ltchu.design.model.Person;import com.ltchu.design.repository.PersonRepository;@Controllerpublic class TestController {    @Autowired    private PersonRepository personRepository;        @RequestMapping(value = {
"/", "/home"})    @ResponseBody public String home() { personRepository.save(new Person("zhangsan")); return "home"; } }

 

六、DesignApplication

package com.ltchu.design;import java.util.TimeZone;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DesignApplication {    public static void main(String[] args) {        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));        SpringApplication.run(DesignApplication.class, args);    }}

 

七、html文件

 

<!DOCTYPE html>

<html>
  <head>
    <meta charset="UTF-8"/>
    <title>home title</title>
  </head>
  <body>
    啊啦啦啦
  </body>
</html>

 

转载于:https://www.cnblogs.com/zxguan/p/8306461.html

你可能感兴趣的文章
常用正则表达式
查看>>
6.2.7 Math对象的使用
查看>>
PHP 重置数组为连续数字索引的几种方式
查看>>
南阳理工acm 88-汉诺塔(一)
查看>>
160809308周子济第六次作业
查看>>
大型Web应用运行时 PHP负载均衡指南
查看>>
为phpStorm 配置PHP_CodeSniffer自动检查代码
查看>>
测试工具网址大全(转)
查看>>
ServiceStack DotNet Core前期准备
查看>>
webpack中‘vant’全局引入和按需引入【vue-cli】
查看>>
Date、String和Timestamp类型转换
查看>>
计算机的组成
查看>>
CSS命名规范
查看>>
初始化构造函数中定义的实体集合,方便嵌套类型的遍历
查看>>
状压dpHDU - 4856
查看>>
java.nio.ByteBuffer 类 缓冲区
查看>>
PL/SQL系列1-PL/SQL介绍
查看>>
关于render函数的总结
查看>>
JavaScript 小刮号
查看>>
BZOJ USACO 银组 水题集锦
查看>>