1.pom引用
<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.inslife</groupId> <artifactId>baodan</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>baodan</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <!--这里一定要指定版本--> <version>5.1.24</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
2.src/main/resources/application.properties
server.port=9090 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/baodan?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=baodan spring.datasource.password=baodan spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=update #spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect spring.thymeleaf.cache=false #显示sql语句 spring.jpa.show-sql=true
3.创建实体类
import java.util.Date; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Tenant { @Id private String id; private String name; private String phone; private String address; private String remark; private Date gmtCreate; private Date gmtModified; private Integer status; //省略get set }
4.创建数据访问层
import org.springframework.data.jpa.repository.JpaRepository; import com.inslife.baodan.domain.Tenant; public interface TenantDao extends JpaRepository<Tenant, String> { }
5.创建service层
import java.util.Date; import java.util.UUID; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import com.inslife.baodan.dao.TenantDao; import com.inslife.baodan.domain.Tenant; @Service public class TenantService { @Autowired TenantDao tenantDao; @Cacheable(cacheNames = { "Tenant" }) public Tenant getOne(String id) { return tenantDao.findOne(id); } @CachePut(cacheNames = { "Tenant" }, key = "#result.id") public Tenant save(Tenant tenant) { tenant.setId(UUID.randomUUID().toString().replaceAll("-", "")); tenant.setGmtCreate(new Date()); tenantDao.save(tenant); return tenant; } @CachePut(cacheNames = { "Tenant" }, key = "#result.id") public Tenant update(Tenant tenant) { Tenant query = tenantDao.findOne(tenant.getId()); BeanUtils.copyProperties(tenant, query, "id", "gmtCreate"); query.setGmtModified(new Date()); tenantDao.save(query); return query; } }
6.启动项目
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @EnableCaching @SpringBootApplication public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); SpringApplication.run(App.class, args); } }
7. src/main/resources/template/index.html
thymeleaf模板
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>标题</title> <link rel="stylesheet" th:href="@{/main.css}"/> <script th:src="@{/jquery.min.js}"></script> </head> <body> <div> <h2>hello thymeleaf</h2> <a th:href="@{http://127.0.0.1/appcontext?url={url}(url=${item.fileUrl})}" target="_blank">查看图片</a> </div> </body> </html>