dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile 'org.webjars:bootstrap:4.3.1'
compile 'org.springframework.boot:spring-boot-devtools'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
compile 'mysql:mysql-connector-java:5.1.47'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1'
}
welcome.message: advanced web programming
spring.thymeleaf.cache=false
spring.devtools.restart.enabled=false
#datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/lab
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
mybatis.type-aliases-package=com.example.pojo
mybatis.mapper-locations=classpath:/com/example/mapper/UsersMapper.xml
package com.example.pojo;
public class Users {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.example.mapper;
import java.util.List;
import com.example.pojo.Users;
public interface UsersMapper {
void insertUser(Users users);
List selectUsersAll();
Users selectUsersById(Integer id);
void updateUser(Users users);
void deleteUserById(Integer id);
}
insert into Users(name,age) values(#{name},#{age})
update Users set name=#{name},age=#{age} where id=#{id}
delete from Users where id=#{value}
package com.example.service;
import java.util.List;
import com.example.pojo.Users;
public interface UsersService {
void addUsres(Users users);
List findUserAll();
Users findUserById(Integer id);
void updateUser(Users users);
void deleteUserById(Integer id);
}
package com.example.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.mapper.UsersMapper;
import com.example.pojo.Users;
import com.example.service.UsersService;
@Service
@Transactional
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersMapper usersMapper;
@Override
public void addUsres(Users users) {
this.usersMapper.insertUser(users);
}
@Override
public List findUserAll() {
return this.usersMapper.selectUsersAll();
}
@Override
public Users findUserById(Integer id) {
return this.usersMapper.selectUsersById(id);
}
@Override
public void updateUser(Users users) {
this.usersMapper.updateUser(users);
}
@Override
public void deleteUserById(Integer id) {
this.usersMapper.deleteUserById(id);
}
}
package com.example.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.pojo.Users;
import com.example.service.UsersService;
@Controller
@RequestMapping("/users")
public class UsersController {
@Autowired
private UsersService usersService;
/**
* 页面跳转
*
*/
@RequestMapping("/{page}")
public String showPage(@PathVariable String page)
{
return page;
}
/**
* 添加用户
*
*/
@RequestMapping("/addUser")
public String addUser(Users users)
{
this.usersService.addUsres(users);
return "ok";
}
/**
* 查询全部用户
*
*/
@RequestMapping("/findUserAll")
public String findUserAll(Model model)
{
List list=this.usersService.findUserAll();
model.addAttribute("list", list);
return "showUsers";
}
/**
* 根据用户ID查询用户
*
*/
@RequestMapping("/findUserById")
public String findUserById(Integer id,Model model)
{
Users user=this.usersService.findUserById(id);
model.addAttribute("user", user);
return "updateUser";
}
/**
* 更新用户
*
*
*/
@RequestMapping("/editUser")
public String editUser(Users users)
{
this.usersService.updateUser(users);
return "ok";
}
/**
* 删除用户
*
*
*/
@RequestMapping("/delUser")
public String delUser(Integer id)
{
this.usersService.deleteUserById(id);
return "redirect:/users/findUserAll";
}
}
添加用户
showUsers.html,用于显示查询到的数据
展示用户数据
用户ID
用户姓名
用户年龄
操作
更新用户
删除用户
updateUser.html,用于输入修改后的数据
Insert title here