1.1 安装SpringBoot--sts 网址:https://spring.io/tools/sts/all 下载压缩包不需要解压
菜单栏点击help--install new sofawate--add--archive--找到下载的安装包--选中4个带IDE的
ps:安装会报错:An error occurred while collecting items to be installed 关闭防火墙即可,然后又报错说找不到某些依赖,并且握手期间网站不可访问,这次是家里网络的问题,换到公司网络就安装好了。
1.2安装gradle 网址:https://services.gradle.org/distributions/ 下载带-all的并解压
1)新建环境变量GRADLE_HOME ,配置为你安装GRADLE的目录
2)修改环境变量Path,在后面+上 %GRADLE_HOME%\bin
3)检测是否成功:打开cmd,输入gradle -v
1.3 使用Gradle编译项目 创建:https://start.spring.io/
右边选择依赖的包:Web,Thymeleaf,JPA,MySQL,H2
来到解压的目录 shift+右键-- 选择打开你的powershell gradle build
1.4 将编译好的项目导入eclipse (会自动导入,就是时间比较久)
File--import--Gradle--Existing Gradle Project --找到你的gradle工程
1.5 创建第一个controller
package com.sikiedu.myfirstspringboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello.action")
public String test() {
return "Hello SpringBoot";
}
}
2.1.安装thymeleaf插件 网址:http://www.thymeleaf.org/eclipse-plugin-update-site/
安装步骤同上,html页面插入抬头
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
2.2 配置application.properties
#Thymeleaf 编码
spring.thymeleaf.encoding=UTF-8
#热部署静态文件
spring.thymeleaf.cache=false
#使用html5标准
spring.thymeleaf.mode=HTML5
#使用h2控制台
spring.h2.console.enabled=true
#DataSource
spring.datasource.url=jdbc:mysql:///demo
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#JPA
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
2.3 填写html页面表单(Thymeleaf)
<form action="" th:action="@{~/user/register}" method="POST" >
</form>
<tr th:each="usr:{userList}">
<th th:text="user.username">admin</th>
<th th:text ="user.password">123</th>
<th>
<a th:href="@{~/toEditor">修改</a>
</th>
</tr>
2.4 创建实体
实体:就是与数据库对应的表
1)有Entity注解
2)要有public 或者 protected无参构造函数
3)要有主键(唯一标识)
package com.yanzi.userlogindemo.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private String telephone;
protected User() {
}
public User(Long id, String username, String password, String telephone) {
this.id = id;
this.username = username;
this.password = password;
this.telephone = telephone;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
偶遇bug:2018-11-28 21:50:51.218 INFO 19252 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-11-28 21:50:51.218 INFO 19252 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-11-28 21:50:51.239 INFO 19252 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 21 ms
controller的路径不对
2.5 Data JPA里的 CrudRepository 这里主要替换dao层
主要函数:sava
savaAll
findById
existsById
findAll
findAllById
count
deleteById
delete
deleteAll
package com.yanzi.userlogindemo.Repository;
import org.springframework.data.repository.CrudRepository;
import com.yanzi.userlogindemo.domain.User;
public interface UserRepository extends CrudRepository<User, Long>{
User findByUsernameAndPassword(String username,String password);
}
2.6 service
@service注解