目录
一、项目介绍
二、具体实现步骤如下:
1. 新建工程,引入依赖 pom.xml
2.编写User 实体用户类
3.编写UserMapper.java 直接和数据库进行交互
4.编写UserService.java 调用mapper层,并将结果返回给controller
5. 编写userFront.html前台页面
6. 编写Controller层
7. application.yml 配置文件
本篇文章实现了用户的增删改查功能,使用的技术:ajax,mybatis,mysql,springboot。界面实现效果如下:
需要源码,请加微信号,进技术交流群,发送springboot106,免费获取此文章源码。
包含jdbc,mybatis,mysql,web,thymeleaf,json依赖
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
fastjson
1.2.28
public class User {
private Integer id;
private String username;
private String sex;
private String employeeNum;
private String department;
//省略get,set
}
@Mapper
public interface UserMapper {
@Select("select * from t_user where id = #{id}")
public User getUserById(Integer id);
@Options(useGeneratedKeys = true,keyProperty = "id") //加入自增主键
@Insert("insert into t_user(username,sex,employeeNum,department) values(#{username},#{sex},#{employeeNum},#{department})")
public int insertUser(User user);
@Delete("delete from t_user where id=#{id}")
public int deleteById(Integer id);
@Update("update t_user set username=#{username},sex=#{sex},employeeNum=#{employeeNum},department=#{department} where id=#{id}")
public int updateUser(User user);
@Select("select * from t_user")
public List getALLUsers();
@Select("select * from t_user where username like CONCAT('%',#{name},'%') and employeeNum like CONCAT('%',#{num},'%')")
public List searchUsers(@Param("name") String name,@Param("num") String num);
}
@Service
public class UserService {
@Autowired
UserMapper userMapper;
public List searchUsers(String name, String num){
List userList=userMapper.searchUsers(name,num);
return userList;
}
public User getUserById(Integer id)
{
return userMapper.getUserById(id);
}
public int insertUser(User user)
{
return userMapper.insertUser(user);
}
public int deleteById(Integer id)
{
return userMapper.deleteById(id);
}
public int updateUser(User user)
{
return userMapper.updateUser(user);
}
public List getALLUsers()
{
return userMapper.getALLUsers();
}
}
前台界面使用bootstrap-talbe展示表格,通过ajax发送请求到后台controller。单击新增用户,弹出新增用户模态对话框。单击修改用户,弹出修改用户模态对话框。单击删除用户则发送删除请求
Title
ID
姓名
性别
工号
部门
UserFrontController.java 用户接收前台ajax发送的请求,并进行处理,将数据返回给前台
package com.whale.springboot106.controller;
import com.alibaba.fastjson.JSONObject;
import com.whale.springboot106.bean.User;
import com.whale.springboot106.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* author:zll
* date:2020/1/17
* description:
**/
@Controller
public class UserController {
@Autowired
UserService userService;
//从前端访问
@RequestMapping("users")
public String getUserFront()
{
return "user/userFront";
}
@ResponseBody
@RequestMapping(value="pageModel",produces="html/text;charset=UTF-8")
public String pageModel(HttpServletResponse response) {
response.setContentType("text/json");
response.setCharacterEncoding("utf-8");
List userList=userService.getALLUsers();
int total = (int) userList.size();
//转换为json数据
JSONObject result = new JSONObject();
result.put("rows",userList);
result.put("total",total);
System.out.println("pageModel");
System.out.println(result.toJSONString());
return result.toJSONString();
}
//前端分页,增加新用户
@ResponseBody
@RequestMapping("/addUser")
public String addUserModal(User user)
{
System.out.println("addUserModal");
System.out.println("====="+user.getId());
System.out.println("====="+user.getUsername());
System.out.println("====="+user.getSex());
System.out.println("====="+user.getDepartment());
//user.setUsername("aa");
userService.insertUser(user);
JSONObject result = new JSONObject();
result.put("state", "success");
System.out.println(result.toJSONString());
return result.toJSONString();
}
//前端分页,增加新用户
@ResponseBody
@RequestMapping("/editUser")
public String editUserModal(User user)
{
System.out.println("editUserModal");
userService.updateUser(user);
JSONObject result = new JSONObject();
result.put("state", "success");
System.out.println(result.toJSONString());
return result.toJSONString();
}
//删除用户
@ResponseBody
@RequestMapping("/deleteUser")
public String deleteUser(HttpServletRequest request) {
String[] list=request.getParameterValues("ids");
System.out.println(list);
for (int i = 0; i < list.length; i++) {
String id = list[i];
System.out.println(id);
userService.deleteById(Integer.parseInt(id));
}
JSONObject result = new JSONObject();
result.put("state", "success");
return result.toJSONString();
}
//查找用户
@ResponseBody
@RequestMapping("/searchUser")
public String searchUser(HttpServletRequest request) {
String name=request.getParameter("username");
String num=request.getParameter("employeeNum");
List userList=userService.searchUsers(name,num);
int total = (int) userList.size();
JSONObject result = new JSONObject();
result.put("rows",userList);
result.put("total",total);
return result.toJSONString();
}
}
spring:
datasource:
username: root
password: 888
url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
server:
port: 8091
mybatis:
configuration:
#开启驼峰命名转换
map-underscore-to-camel-case: true