1.创建实体类
public class User {
private Integer id;
private String username;
private String password;
public User() {
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
2.创建dao层接口
//数据访问层
//1、@Mapper注解:
//作用:在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类
@Mapper//在接口上添加了这个注解表示这个接口是基于注解开发的
public interface UserMapper {
//查询全部
@Select("select * from user")
List<User> findAll();
//新增数据
@Insert("insert into user(username,password) values (#{username},#{password})")
public int save(User user);
//删除数据
@Delete("delete from user where id=#{id}")
public int delete(int id);
//根据id查找
@Select("select * from user where id=#{id}")
public User get(int user);
//更新数据
@Update("update user set username=#{username},password=#{password} where id=#{id} ")
public int update(User user);
}
3.创建业务
(1)接口
/*service
业务逻辑层
主要是针对具体的问题的操作,把一些数据层的操作进行组合,间接与数据库打交道(提供操作数据库的方法)。
要做这一层的话,要先设计接口,在实现类。
*/
//业务逻辑层
public interface UserService {
//查询全部
List<User> findAll();
//新增数据
int save(User user);
//删除数据
Integer delete(int id);
//根据id查询
User get(int id);
//更新数据
int update(User user);
}
(2)实现类
//@Service("serviceName")注解相当于applicationContext.xml配置文件中配置的<bean id="serviceName">,
// 表示给当前类命名一个别名,方便注入到其他需要用到的类中。
// @Service注解也可以不指定serviceName,如果不指定相当于<bean id="com.study.service.serviceName">,
// com.study.service.ServiceName就是这个类的全限定名,不加的话,默认别名就是当前类名,但是首字母小写。
@Service
public class UserServiceImpl implements UserService {
//@Resource来自jdk中的注解,可以使用它给引用类型赋值注入(这就是spring的自动注入使用)
@Resource
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public int save(User user) {
return userMapper.save(user);
}
@Override
public Integer delete(int id) {
return userMapper.delete(id);
}
@Override
public User get(int id) {
return userMapper.get(id);
}
@Override
public int update(User user) {
return userMapper.update(user);
}
}
4.创建controller层
/
/controller控制层,导入service层
@Controller
public class UserControl {
//@Autowired引用类型注解,获取mapper中数据
@Autowired
private UserService userService;
/访问跳转到指定页面register.html。相关yml配置文件在下面。如果是jsp页面将后缀改成.jsp*/就行了
//这里的主要用途是重定向,这样就能跳转到指定页面
/* spring:
mvc:
view:
prefix: classpath:/templates/
suffix: .html*/
@RequestMapping(value = "add",method = RequestMethod.GET)
public ModelAndView userAll(){
ModelAndView mv=new ModelAndView("add");
return mv;
}
/*
如果是启动后直接访问页面,就将登录页面放在webapp源目录下,这样就能http://localhost:8080/index.html直接访问。
*/
//查询全部
@RequestMapping("/list")
public String userList(ConcurrentModel model){
List<User> users=userService.findAll();
//model.addattribute(K,V)往前台传数据,
// 可以传对象,可以传List,通过el表达式 ${}可以获取到
model.addAttribute("users",users);
return "index";
}
//新增数据
@RequestMapping("/add")
public String save(User user){
userService.save(user);
return "redirect:/list";
}
@RequestMapping("/useradd")
public String add(){
return "add";
}
//删除数据
@RequestMapping("/delete/{id}")//通过id值,指定删除哪个,"/delete/{id}"restful风格
//@PathVariable他的作用用在参数里用来提取url中的请求参数
//HttpServletResponse servletResponse是响应用户请求
public String delete(@PathVariable Integer id, HttpServletResponse servletResponse) throws IOException {
int count =userService.delete(id);
if(count==1){
servletResponse.sendRedirect("/list");
}
return "error";
}
//根据id查找
@GetMapping("/updatePage/{id}")//此处相当于@RequestMapping(/get/{id},method=RequestMethod.GET)
public String get(Model model, @PathVariable int id){
User users=userService.get(id);
model.addAttribute("users",users);
return "modifi";
}
//更新数据
@GetMapping("/update")
public String update(Model model,User user, HttpServletRequest request){
String id=request.getParameter("id");
User userById=userService.get(Integer.parseInt(id));
userService.update(user);
System.out.println(user);
return "redirect:/list";
}
}
启动类
@SpringBootApplication
public class Boot1Application {
public static void main(String[] args) {
SpringApplication.run(Boot1Application.class, args);
}
}
数据库随便建建就好啦
登录静态页面(add.html)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加用户</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div style="width:600px;height:100%;margin-left:270px;">
<form action="/add" method="post">
用户名:<input class="form-control" type="text" th:value="${username}" name="username"><br>
密 码:<input class="form-control" type="text" th:value="${password}" name="password"><br>
<button class="btn btn-primary btn-lg btn-block">保存</button>
</form>
</div>
</body>
</html>
index页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户列表</title>
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<style>
a{
color:#fff;
}
h1{
text-align: center;
}
</style>
<body>
<h1>spring-boot</h1>
<table class="table table-striped table-bordered table-hover text-center">
<thead>
<tr style="text-align:center">
<th>编号</th>
<th>姓名</th>
<th>密码</th>
<th>操作</th>
</tr>
</thead>
<tr th:each="users:${user}">
<td th:text="${users.id}"></td>
<td th:text="${users.username}"></td>
<td th:text="${users.password}"></td>
<td>
<a class="btn btn-primary" th:href="@{'/updatePage/'+${users.id}}">更改</a>
<a class="btn btn-danger" th:href="@{'/delete/'+${users.id}}">删除</a>
</td>
</tr>
</table>
<button class="btn btn-block" ><a href="/useradd">添加用户</a></button>
</body>
</html>
modify页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>修改用户</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div style="width:600px;height:100%;margin-left:270px;">
<form action="/update" method="post">
ID:<input class="form-control" name="id" type="text" th:value="${users.id}" readonly="readonly"><br>
用户名:<input class="form-control" type="text" th:value="${users.username}" name="username"><br>
密 码:<input class="form-control" type="text" th:value="${users.password}" name="password"><br>
<button class="btn btn-primary btn-lg btn-block" type="submit">提交</button>
</form>
</div>
</body>
</html>
register页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<!--定义表单-->
<form action="#" method="post">
<table border="1" align="center" width="500">
<tr>
<td><label for="username">用户名</label></td>
<td><input type="text" name="username" placeholder="请输入用户名" id="username"></td>
</tr>
<tr>
<td><label for="password">密码</label></td>
<td><input type="password" name="password" placeholder="请输入密码" id="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
application.yml配置
spring:
mvc:
view:
prefix: classpath:/templates/
suffix: .html
thymeleaf:
mode: HTML
encoding: utf-8
cache: false
#数据源有关的
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/springboot?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
username: root
password: root
driver-clakss-name: com.mysql.cj.jdbc.Driver
devtools:
restart:
enabled: true #设置开启热部署
freemarker:
cache: false #页面不加载缓存,修改即时生效
#定义web项目静态资源文件夹位置(静态资源位置是可以定义的,不定义就在templates文件夹下)
# web:
# resources:
# static-locations: classpath:/static/
mybatis:
configuration:
map-underscore-to-camel-case: true # 下划线驼峰配置
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 打印SQL语句
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mini</groupId>
<artifactId>boot1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-jdbc</artifactId>-->
<!-- </dependency>-->
<!-- thymeleaf依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--mysql连接器(驱动)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--阿里巴巴数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!--获取页面session对象request对象response对象HttpServletxxx包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<!--json转换工具包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- 热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-configuration-processor</artifactId>-->
<!-- <optional>true</optional>-->
<!-- </dependency>-->
<!-- junit测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintsge</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 导入hikaricp数据源-->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>hikaricp</artifactId>
<version>3.4.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>