1.创建项目
项目中选择lombok,jpa,mysql,web
引入pom.xml
<dependencies>
<!--spring data-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--springboot-->
<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>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--JPA依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--Druid连接池依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!--thymeleaf模板-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
2.thymeleaf有点类似于jstl+el
th:href="@{url}表示这是一个链接
<!--thymeleaf表达式,th:href="@{/css/bootstrap.css}"@表示后面的是一个链接-->
<link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
th:each="user : ${users}"相当于foreach,对user进行循环遍历
<!--each来进行for循环求值-->
<tr th:each="user : ${users}">
<th scope="row" th:text="${user.id}">1</th>
<td th:text="${user.username}">neo</td>
<td th:text="${user.password}">Otto</td>
<td th:text="${user.age}">6</td>
<td><a th:href="@{/toEdit(id=${user.id})}">edit</a></td>
<td><a th:href="@{/delete(id=${user.id})}">delete</a></td>
</tr>
th:if进行if条件判断
<a th:if="${myself=='yes'}" > </i> </a>
*{变量} 与 ${变量}的区别:
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
等价于:
<div th:object="${session.user}">
<p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>
</div>
3.加入三个静态页面
4.配置好application.yml配置文件
5.DAO层
6.实体类
@GeneratedValue(strategy = GenerationType.IDENTITY) 中的strategy = GenerationType.IDENTITY使用场景补充说明:
前端页面中只有三个username,password,age三个数据,我们设置strategy = GenerationType.IDENTITY以后只需要传入这三个属性我们就把他当作是一个User对象来看。
那么User对象有了,使用下面的代码才不会会出错:
7.UserService和它的实现类
实现类UserServiceImpl
package com.qianliu.springboot_test.service.impl;
import com.qianliu.springboot_test.dao.UserRepository;
import com.qianliu.springboot_test.entity.User2;
import com.qianliu.springboot_test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
/*
* @author qianliu on 2019/5/14 16:38
* @return java.util.List<com.qianliu.springboot_test.entity.User> 查询返回User的列表
* @Discription:查询所有的数据
*/
@Override
public List<User2> getUserList() {
return userRepository.findAll();
}
/*
* @author qianliu on 2019/5/14 16:39
* @param [id] 输入id
* @return com.qianliu.springboot_test.entity.User 返回用户的User实体类
* @Discription:通过id查询某一个数据
*/
@Override
public User2 findUserById(long id) {
return userRepository.findById(id);
}
/*
* @author qianliu on 2019/5/14 16:39
* @param User 输入一个User的实体类
* @Discription:
*/
@Override
public void save(User2 user) {
userRepository.save(user);
}
/*
* @author qianliu on 2019/5/14 16:39
* @param User 输入一个User的实体类
* @Discription:更新User实体类
*/
@Override
public void edit(User2 user) {
userRepository.save(user);
}
/*
* @param 输入一个id
* @Discription:通过id删除用户
*/
@Override
public void delete(long id) {
userRepository.deleteById(id);
}
}
8.controller层
UserController的编写
package com.qianliu.springboot_test.web;
import com.qianliu.springboot_test.entity.User2;
import com.qianliu.springboot_test.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class UserController {
@Resource
UserService userService;
/*
* @author qianliu on 2019/5/14 16:42
* @param / 输入一个"/"字符串
* @return 跳转到"/list"页面
* @Discription: 将"/"自动重定向到"/list"
*/
@RequestMapping("/")
public String index() {
return "redirect:/list";
}
/*
* @author qianliu on 2019/5/14 16:44
* @param 输入字符串"/list"
* @return
* @Discription:
*/
@RequestMapping("/list")
public String list(Model model) {
List<User2> users=userService.getUserList();
model.addAttribute("users", users);
return "user/list";
}
/*
* @author qianliu on 2019/5/14 17:39
* @Discription:跳转到增加用户页面
*/
@RequestMapping("/toAdd")
public String toAdd() {
return "user/userAdd";
}
/*
* @author qianliu on 2019/5/14 17:41
* @param "/add"链接
* @return 重定向到/list页面
* @Discription: 保存User实体到数据库,然后返回到list界面
*/
@RequestMapping("/add")
public String add(User2 user) {
userService.save(user);
return "redirect:/list";
}
/*
* @author qianliu on 2019/5/14 18:39
* @param /toEdit链接进入该程序
* @return 查询出User以后,跳转到"user/userEdit"编辑界面
* @Discription:
*/
@RequestMapping("/toEdit")
public String toEdit(Model model,Long id) {
User2 user=userService.findUserById(id);
model.addAttribute("user", user);
return "user/userEdit";
}
/*
* @author qianliu on 2019/5/14 18:40
* @param "/edit"链接进入该程序
* @return 重定向到"/list"链接
* @Discription:更新数据库中的User以后跳转到list
*/
@RequestMapping("/edit")
public String edit(User2 user) {
userService.edit(user);
return "redirect:/list";
}
/*
* @author qianliu on 2019/5/14 18:43
* @param "/delete"链接进入该方法
* @return 重定向到"/list"
* @Discription: 删除某一个数据后定向到"/list"
*/
@RequestMapping("/delete")
public String delete(Long id) {
userService.delete(id);
return "redirect:/list";
}
}
9.测试程序
进入程序:
添加一次数据,成功即可!
然后删除一条数据,刷新一下: