一、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=.htmlspring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/htmlspring.thymeleaf.mode=HTML5spring.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>