@Data
@TableName("user")
public class UserDO {
@TableId(type=IdType.AUTO) //这个要注意加上(id在表内为主键自增)
private Integer id;
@TableField("name")
private String name;
@TableField("card_type")
private int cardType;
@TableField("card_number")
private String cardNum;
@TableField("information")
private String information;
@TableField("role_id")
private int role;
@TableField("phone")
private String phone;
@TableField("sex")
private int sex;
@TableField("password")
private String passWord;
private String type;
private List ct;
实体类2
@Data
@ToString
public class CardType {
@TableId
private Integer id;
@TableField
private String type;
@TableField
private String regular;
接下来Mapper层
public interface UserMapper extends BaseMapper {
/*
*查找
*/
List findAll();
/*
*删除用户
*/
Integer deleteInfoById(Integer id);
/*
*新增用户信息
*/
Integer insertuser(UserDO userDO);
/*
*修改用户信息
*/
Integer updateInfoById(UserDO userDO);
}
mapper映射xml文件
delete from user
where id = #{id}
INSERT INTO `account`.`user`( `name`, `card_type`, `card_number`, `information`,
`role_id`, `phone`, `sex`, `password`)
VALUES
(#{name},#{cardType},#{cardNum},#{information},#{role},#{phone},#{sex},#{passWord})
update user set name=#{name},card_type=#{cardType},card_number=#{cardNum},role_id=#{role},phone=#{phone},
sex=#{sex} where id=#{id};
Service层
package com.huaxia.service;
import java.util.List;
import com.huaxia.DO.UserDO;
public interface UserService {
/**
* 查用户信息
*/
List findAll();
/**
* 删除用户
*/
Integer deleteUser(Integer id);
/**
* 新增用户
* @throws Exception
*/
Integer addUser(UserDO userDO) throws Exception;
/**
* 修改数据
* @return
* @throws Throwable
*/
UserDO updateInfo(UserDO userDO) throws Throwable;
;
}
serviceImpl
package com.huaxia.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huaxia.DO.UserDO;
import com.huaxia.mapper.UserMapper;
import com.huaxia.service.UserService;
/**
* @Description:
* @Author:Jingdaxia
* @Data:2019/7/28
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
public UserDO findUserByName(String name) {
return userMapper.findUserByUsername(name);
}
@Override
public List findAll() {
ArrayList fundVOS = new ArrayList<>();
List findAll = userMapper.findAll();
//ListPageUtil listPageUtil = new ListPageUtil(findAll, 1, 5);
//List pagedList = listPageUtil.getPagedList();
for (UserDO fundDO : findAll) {
fundVOS.add(findType(fundDO));
}
return fundVOS;
}
private UserDO findType(UserDO findAll) {
String a=findAll.getCt().get(0).getType();
findAll.setType(a);
return findAll;
}
@Override
public Integer deleteUser(Integer id) {
Integer di=userMapper.deleteInfoById(id);
return di;
}
@Override
public Integer addUser(UserDO userDO) throws Exception {
Integer ini=userMapper.insertuser(userDO);
if(ini==1) {
return ini;
}else {
throw new Exception("发生一个错误");
}
}
@Override
public UserDO updateInfo(UserDO userDo) throws Throwable {
Integer up=userMapper.updateInfoById(userDo);
if(up==1) {
return userDo;
}else {
throw new Exception("发生一个错误");
}
}
}
controller层
package com.huaxia.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.huaxia.DO.UserDO;
import com.huaxia.service.UserService;
@RequestMapping
@Controller
public class UserController {
@Autowired
UserService userService;
@ResponseBody
@RequestMapping(value = "userManagement1")
public String listUser(
@RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
@RequestParam(required = false,defaultValue="5",value="pageSize")Integer pageSize) {
List userList = userService.findAll();
System.out.println("分页数据:" + userList);
return JSON.toJSONString(userList);
}
@RequestMapping(value = "deleteById" )
@ResponseBody
public String deleteProfessorByProid(Integer id){
this.userService.deleteUser(id);
return "0";
}
@RequestMapping(value="addUserInfo")
@ResponseBody
public String addInfo(ModelMap map, String name,String cardType,String cardNum
,String phone,String sex,String passWord) throws Exception {
UserDO userDO = new UserDO();
userDO.setName(name);
userDO.setCardType(Integer.parseInt(cardType));
userDO.setCardNum(cardNum);
userDO.setPhone(phone);
userDO.setSex(Integer.parseInt(sex));
userDO.setPassWord(passWord);
Integer inte=userService.addUser(userDO);
return JSON.toJSONString(inte);
}
@RequestMapping(value="updateInfo")
@ResponseBody
public String updataInfo(UserDO userDO) throws Throwable {
UserDO us=userService.updateInfo(userDO);
return JSON.toJSONString(us);
}
}
前端页面内容多 有些乱
在新增功能上 我用的是模态框 因为有些数据添加后不在上页面显示
修改功能同样也是模态框
用户管理