一、maven 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.ltchu</groupId> <artifactId>design</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>design</name> <description>ltchu's graduation design</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
二、properties配置文件
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/design?characterEncoding=UTF-8&useSSL=false spring.datasource.username = root spring.datasource.password = root spring.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<Person, String> { }
五、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; @Controller public 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;
@SpringBootApplication
public 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>