首先第一步是要配置我们的pom.xml文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</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-test</artifactId>
<scope>test</scope>
</dependency>
引入上面的依赖后,idea会在右下角自动的提示我们要是否要导包,这时点击确认即可。
第二步,我们需要对application.properties文件进行配置。
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mysqldb?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=17366758919NBV
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
spring.thymeleaf.cache=false
这里要注意以下,文件中mysqldb是本人在MySQL中已经创建好的库,我们的项目会根据在该库下建表,所以这里的mysqldb要根据自己的需求来修改。
其中propertiesspring.thymeleaf.cache=false是关闭thymeleaf的缓存,不然在开发过程中修改页面不会立刻生效需要重启,生产(即为用户端)可配置为true。
第三步是导入我们的的样式资源bookstrap.css,这个是用来对我们制作的网页进行修饰的。
没有上面样式资源的同学可以到https://www.bootcss.com/下载资源。
随便说一下在项目resources目录下会有两个文件夹:static目录用于放置网站的静态内容如css、js、图片;templates目录用于放置项目使用的页面模板。有些同学可以不知道这两个文件的作用,我在这里就补充一下。
第四步编辑ThymeleafTestApplication启动类需要添加Servlet的支持。
package com.example.thymeleaf_test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class ThymeleafTestApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ThymeleafTestApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(ThymeleafTestApplication.class, args);
}
}
第五步要在model目录下创建User.java帮助将实体类映射到数据库mysqldb。
package com.example.thymeleaf_test.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class User {
@Id
private long id;
private String userName;
private String password;
private int age;
public User(){
}
public long getId() {
return id;
}
public User setId(long id){
this.id = id;
return this;
}
public String getUserName(){
return userName;
}
public User setUserName(String userName){
this.userName = userName;
return this;
}
public String getPassword(){
return password;
}
public User setPassword(String password){
this.password = password;
return this;
}
public int getAge(){
return age;
}
public User setAge(int age){
this.age = age;
return this;
}
}
第六步在repository目录下创建接口UserRepository继承JpaRepository类,JpaRepository类有很多自动实现内置的方法,包括增删改查。
package com.example.thymeleaf_test.repository;
import com.example.thymeleaf_test.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User,Long> {
User findById(long id);
void deleteById(Long id);
}
第七步在service目录下创建Userservice接口和UserServiceImpl类,service调用jpa实现相关的增删改查,实际项目中service层处理具体的业务代码。
package com.example.thymeleaf_test.service;
import com.example.thymeleaf_test.model.User;
import java.util.List;
public interface UserService {
public List<User> g