SpringBoot实现CURD
项目列表
Pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.1.RELEASE
com.lin
springboot0711
0.0.1-SNAPSHOT
springboot0711
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-data-jdbc
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
8.0.20
log4j
log4j
1.2.17
junit
junit
5
org.springframework
spring-webmvc
5.2.7.RELEASE
org.springframework.boot
spring-boot-starter-web
2.3.1.RELEASE
org.springframework.boot
spring-boot-starter-jdbc
2.3.1.RELEASE
org.springframework.boot
spring-boot-starter-thymeleaf
2.3.1.RELEASE
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.3
junit
junit
4.11
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
junit
junit
test
org.springframework.boot
spring-boot-maven-plugin
Bean
User
package com.lin.springboot0711.Bean;
public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
Controller
ModifyUserController
package com.lin.springboot0711.Controller;
import com.lin.springboot0711.Bean.User;
import com.lin.springboot0711.Service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class ModifyUserController {
@Resource
UserService userService;
//查询所有用户
@RequestMapping("/users")
public String showUsers(ModelMap modelMap){
List userList=userService.queryAllUser();
modelMap.addAttribute("userList",userList);
return "users";
}
// 新增用户
@RequestMapping("addUser")
@ResponseBody
public Map addUser(User user){
int flag=userService.addUser(user);
Mapmap=new HashMap();
if (flag==1) {
map.put("msg","新增用户成功");
return map;
}
else {
map.put("msg","新增用户失败");
return map;
}
}
// 根据用户ID删除用户信息
@RequestMapping(value = "/deleteUser+{id}")
public String dropUser(@PathVariable("id")String id ,ModelMap modelMap){
int flag=userService.dropUser(id);
ListuserList=userService.queryAllUser();
modelMap.addAttribute("userList",userList);
if (flag==1){
return "users";
}else
{
return "error";
}
}
// 根据用户ID查询用户信息
@RequestMapping("/user+{id}")
public String queryUser(@PathVariable("id") String id,ModelMap modelMap){
User user=userService.queryUserById(id);
modelMap.addAttribute("user",user);
return "userInfo";
}
// 根据用户ID来修改用户信息
@RequestMapping("/modifyUser")
@ResponseBody
public Map modifyUser(User user){
int flag=userService.modifyUser(user);
Mapmap =new HashMap<>();
if (flag==1){
map.put("msg","修改用户信息成功");
return map;
}
else {
map.put("msg", "修改用户信息失败");
return map;
}
}
}
DAO
package com.lin.springboot0711.Dao;
import com.lin.springboot0711.Bean.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
public User getInfo(@Param("name") String name,@Param("password") String password);
public User selectUserById(String id);
public int insertUser(User user);
int updateUser(User user);
int deleteUser(String id);
List getAllUser();
}
Service
package com.lin.springboot0711.Service;
import com.lin.springboot0711.Bean.User;
import java.util.List;
public interface UserService {
User loginIn(String name,String password);
User queryUserById(String id);
int addUser(User user);
int dropUser(String id);
int modifyUser(User user);
ListqueryAllUser();
}
ServiceImpl
package com.lin.springboot0711.ServiceImpl;
import com.lin.springboot0711.Bean.User;
import com.lin.springboot0711.Dao.UserMapper;
import com.lin.springboot0711.Service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
// 将DAO注入Service层
@Resource
private UserMapper userMapper;
@Override
public User loginIn(String name,String password){
return userMapper.getInfo(name,password);
}
@Override
public User queryUserById(String id){
return userMapper.selectUserById(id);
}
@Override
public int addUser(User user){
int aFlag=userMapper.insertUser(user);
return aFlag;
}
@Override
public int dropUser(String id){
int dFlag=userMapper.deleteUser(id);
return dFlag;
}
@Override
public int modifyUser(User user){
int mFlag=userMapper.updateUser(user);
return mFlag;
}
@Override
public ListqueryAllUser(){
return userMapper.getAllUser();
}
}
Springboot0711Application
package com.lin.springboot0711;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.lin.springboot0711.Dao")
public class Springboot0711Application {
public static void main(String[] args) {
SpringApplication.run(Springboot0711Application.class, args);
}
}
mapper UserMapper.xml
insert into user(name, password) VALUES (#{name},#{password});
delete from user where id=#{id};
update user set name=#{name} ,password=#{password} where id=#{id};
layui框架包(空间查看)
login.html
login
error.html
error
登录失败!
success.html
success
登录成功!
userInfo.html
UserInfo
users.html
Users
application.yml
spring:
datasource:
name: springboot #数据库名
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
username: #用户名
password: #密码
driver-class-name: com.mysql.cj.jdbc.Driver #数据库链接驱动
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.lin.springboot0711.Bean
操作过程
说明
该项目是一个简单的CURD 参考GitHub一个小的项目修改
附上GitHub地址:https://github.com/redesperado/test1.git