Spring Boot使用Mybatis实现增删改查
java.com.wms.model.Admin.java
1 package com.wms.model; 2 3 import java.sql.Timestamp; 4 5 public class Admin { 6 public static enum GenderOption { 7 MAN, //男士 8 WOMAN, //女士 9 SECRET //保密 10 } 11 12 private Integer id; 13 private String loginName; 14 private String loginPassword; 15 private String name; 16 private String mobile; 17 private String email; 18 private GenderOption gender = GenderOption.SECRET; 19 private String ip; 20 private Timestamp loginTime; 21 private Timestamp createTime; 22 23 24 public Integer getId() { 25 return id; 26 } 27 28 public void setId(Integer id) { 29 this.id = id; 30 } 31 32 public String getLoginName() { 33 return loginName; 34 } 35 36 public void setLoginName(String loginName) { 37 this.loginName = loginName; 38 } 39 40 public String getLoginPassword() { 41 return loginPassword; 42 } 43 44 public void setLoginPassword(String loginPassword) { 45 this.loginPassword = loginPassword; 46 } 47 48 public String getName() { 49 return name; 50 } 51 52 public void setName(String name) { 53 this.name = name; 54 } 55 56 public String getMobile() { 57 return mobile; 58 } 59 60 public void setMobile(String mobile) { 61 this.mobile = mobile; 62 } 63 64 public String getEmail() { 65 return email; 66 } 67 68 public void setEail(String email) { 69 this.email = email; 70 } 71 72 public GenderOption getGender() { 73 return gender; 74 } 75 76 public void setGender(GenderOption gender) { 77 this.gender = gender; 78 } 79 80 public String getIp() { 81 return ip; 82 } 83 84 public void setIp(String ip) { 85 this.ip = ip; 86 } 87 88 public Timestamp getLoginTime() { 89 return loginTime; 90 } 91 92 public void setLoginTime(Timestamp loginTime) { 93 this.loginTime = loginTime; 94 } 95 96 public Timestamp getCreateTime() { 97 return createTime; 98 } 99 100 public void setCreateTime(Timestamp createTime) { 101 this.createTime = createTime; 102 } 103 104 105 @Override 106 public String toString() { 107 return "Admin{" + 108 "id=" + id + 109 ", loginName='" + loginName + '\'' + 110 ", loginPassword='" + loginPassword + '\'' + 111 ", name='" + name + '\'' + 112 ", mobile='" + mobile + '\'' + 113 ", email='" + email + '\'' + 114 ", gender=" + gender + 115 ", ip='" + ip + '\'' + 116 ", loginTime=" + loginTime + 117 ", createTime=" + createTime + 118 '}'; 119 } 120 }
resources/application.properties
1 #mybatis 2 mybatis.mapper-locations=classpath:mapper/*.xml 3 mybatis.type-aliases-package=com.wms.model 4 ###开启驼峰命名转换 5 mybatis.configuration.map-underscore-to-camel-case=true
java.com.wms.mapper.AdminMapper.java
1 package com.wms.mapper; 2 3 import com.wms.model.Admin; 4 import org.apache.ibatis.annotations.Mapper; 5 6 import java.util.List; 7 8 @Mapper 9 public interface AdminMapper { 10 /** 11 * 查询全部数据 12 */ 13 List<Admin> selectAll(); 14 15 Admin selectById(int id); 16 17 int updateById(Admin admin); 18 19 int insert(Admin admin); 20 21 int deleteById(int id); 22 23 List<Admin> selectAdminByLoginName(String loginName); 24 }
resources/mapper/AdminMapper.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wms.mapper.AdminMapper"> <resultMap id="AdminResultMap" type="Admin"> <result column="login_name" jdbcType="VARCHAR" property="loginName" /> </resultMap> <select id="selectAll" resultType="Admin"> select id,email,gender,ip,login_name,login_password,login_time,mobile,name,create_time from admin </select> <select id="selectById" resultType="Admin"> select id,email,gender,ip,login_name,login_password,login_time,mobile,name,create_time from admin where id=#{id} </select> <select id="selectAdminByLoginName" resultType="Admin"> select email,gender,ip,login_name,login_time,mobile,name,create_time from admin where id=#{loginName} </select> <update id="updateById" parameterType="Admin"> update admin <trim prefix="set" suffixOverrides=","> <if test="email != null">email=#{email},</if> <if test="gender != null">gender=#{gender},</if> <if test="loginName != null">login_name=#{loginName},</if> <if test="loginPassword != null">login_password=#{loginPassword},</if> <if test="mobile != null">mobile=#{mobile},</if> <if test="name != null">`name`=#{name},</if> <if test="ip != null">ip=#{admin.ip},</if> <if test="loginTime != null">login_time=#{admin.loginTime},</if> </trim> where id=#{id} </update> <insert id="insert" parameterType="Admin"> insert into admin (`email`,`gender`,`login_name`,`login_password`,`mobile`,`name`,`create_time`) values (#{email},#{gender},#{loginName},#{loginPassword},#{mobile},#{name},#{createTime}) </insert> <delete id="deleteById"> delete from admin where id=#{id} </delete> </mapper>
java.com.wms.controller.AdminController.java
1 package com.wms.controller; 2 3 import java.sql.Timestamp; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import javax.annotation.Resource; 9 10 import com.fasterxml.jackson.annotation.JsonFormat; 11 import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; 12 import com.wms.model.Admin; 13 import com.wms.mapper.AdminMapper; 14 import com.wms.repository.AdminRepository; 15 import com.wms.service.mybatis.AdminMybatisService; 16 17 18 import org.springframework.beans.factory.annotation.Autowired; 19 import org.springframework.stereotype.Controller; 20 import org.springframework.ui.ModelMap; 21 import org.springframework.util.DigestUtils; 22 import org.springframework.util.StringUtils; 23 import org.springframework.web.bind.annotation.*; 24 import org.springframework.web.servlet.ModelAndView; 25 26 27 @Controller 28 @RequestMapping("/admin") 29 public class AdminController { 30 @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") 31 private Timestamp createTime; 32 33 @Resource 34 private AdminRepository adminRepository; 35 36 @Resource 37 private AdminMybatisService adminMybitsService; 38 39 @Autowired 40 private AdminMapper adminMapper; 41 42 @GetMapping("/list") 43 public String list() { 44 return "admin/admin-list.html"; 45 } 46 47 @GetMapping(value="/add") 48 public String add() { 49 return "admin/admin-add.html"; 50 } 51 52 @GetMapping(value="/edit") 53 public String edit(@RequestParam("id") Integer id, ModelMap map) { 54 //Admin admin = adminRepository.getAdminById(id); 55 Admin admin = adminMapper.selectById(id); 56 57 map.addAttribute("admin", admin); 58 59 return "admin/admin-edit.html"; 60 } 61 62 63 @GetMapping("/list/ajax") 64 @ResponseBody 65 public Map<String, Object> getListAjax(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) throws Exception{ 66 System.out.println(limit); 67 68 69 //List<Admin> admin_list = adminRepository.findAll(); 70 List<Admin> admin_list = adminMapper.selectAll(); 71 72 Integer total_count = adminMybitsService.getAdminTotal(); 73 74 Map<String, Object> result = new HashMap<>(4); 75 result.put("code", 0); 76 result.put("count", total_count); 77 result.put("msg", ""); 78 result.put("data", admin_list); 79 80 return result; 81 } 82 83 @GetMapping("/detail") 84 @ResponseBody 85 public Admin getAdminById(@RequestParam("id") Integer id) throws Exception { 86 //Admin admin = adminRepository.getAdminById(id); 87 Admin admin = adminMapper.selectById(id); 88 return admin; 89 } 90 91 @GetMapping("/info") 92 @ResponseBody 93 public List<Admin> getAdminByLoginName(@RequestParam("login_name") String loginName) throws Exception { 94 //List<Admin> admin_list = adminRepository.getAdminByLoginName(loginName); 95 List<Admin> admin_list = adminMapper.selectAdminByLoginName(loginName); 96 return admin_list; 97 } 98 99 @RequestMapping("/add/save") 100 @ResponseBody 101 public Map<String, Object> addSave(@RequestParam("name") String name, @RequestParam("login_name") String loginName, @RequestParam("mobile") String mobile, @RequestParam("gender") Admin.GenderOption gender, @RequestParam("email") String email, @RequestParam("login_password") String loginPassword, @RequestParam("confirm_login_password") String confirmLoginPassword) { 102 //DateTime loginTime = new DateTime(); 103 //DigestUtils.md5DigestAsHex(admin.getLoginName().getBytes()); 104 String loginPasswordMd5 = DigestUtils.md5DigestAsHex(loginPassword.getBytes()); 105 Admin admin = new Admin(); 106 admin.setLoginName(loginName); 107 admin.setLoginPassword(loginPasswordMd5); 108 admin.setName(name); 109 admin.setMobile(mobile); 110 admin.setEail(email); 111 admin.setGender(gender); 112 113 //admin.setLoginTime(new Datetime"2020-06-25 17:08:00"); 114 admin.setCreateTime(createTime); 115 //this.adminRepository.save(admin); 116 Integer admin_id = adminMapper.insert(admin); 117 118 Map<String, Object> result = new HashMap<>(2); 119 120 if(admin_id != null) { 121 ///System.out.println("go here"); 122 result.put("status", "success"); 123 result.put("message", "管理员保存成功"); 124 return result; 125 } 126 127 result.put("status", "fail"); 128 result.put("message", "管理员保存失败"); 129 return result; 130 } 131 132 @PostMapping("/edit/save") 133 @ResponseBody 134 public Map<String, Object> editSave(@RequestBody Admin admin) throws Exception { 135 Admin update_admin = new Admin(); 136 update_admin.setId(admin.getId()); 137 update_admin.setEail(admin.getEmail()); 138 update_admin.setGender(admin.getGender()); 139 update_admin.setLoginName(admin.getLoginName()); 140 update_admin.setLoginPassword(admin.getLoginPassword()); 141 update_admin.setMobile(admin.getMobile()); 142 update_admin.setIp(admin.getIp()); 143 update_admin.setLoginTime(admin.getLoginTime()); 144 145 Integer save = adminMapper.updateById(update_admin); 146 147 Map<String, Object> result = new HashMap<>(2); 148 if (save > 0) { 149 result.put("status", "success"); 150 result.put("message", "编辑成功"); 151 } else { 152 result.put("status", "fail"); 153 result.put("message", "编辑失败"); 154 } 155 156 return result; 157 } 158 159 160 @GetMapping("get/all") 161 @ResponseBody 162 public List<Admin> getAll() { 163 //List<Admin> admin_list = adminRepository.findAll(); 164 List<Admin> admin_list = adminMapper.selectAll(); 165 return admin_list; 166 } 167 168 169 @GetMapping("list_mybatis") 170 @ResponseBody 171 public List<Admin> list_mybatis() throws Exception { 172 //List<Admin> list = adminMybitsService.findAdminByLoginName("jack"); 173 List<Admin> list = adminMapper.selectAdminByLoginName("jack"); 174 return list; 175 } 176 177 178 @GetMapping("/delete") 179 @ResponseBody 180 public Map<String, String> delete(@RequestParam("id") Integer id) throws Exception { 181 Map<String, String> map = new HashMap<>(2); 182 if(id == null) { 183 map.put("status", "fail"); 184 map.put("message", "参数异常"); 185 return map; 186 } 187 Integer delRes = adminMapper.deleteById(id); 188 189 if (delRes == null) { 190 map.put("status", "fail"); 191 map.put("message", "登录失败"); 192 } else { 193 map.put("status", "success"); 194 map.put("message", "删除成功"); 195 } 196 197 return map; 198 } 199 200 201 @GetMapping("/change_password") 202 public String changePassword(@RequestParam("id") Integer id, ModelMap map) { 203 204 Admin admin = adminMapper.selectById(id); 205 206 map.addAttribute("admin", admin); 207 return "admin/admin-change-password.html"; 208 } 209 210 211 @GetMapping("/test") 212 @ResponseBody 213 public List<Admin> testAdmin() throws Exception { 214 List<Admin> adminList = adminMapper.selectAll(); 215 System.out.println(adminList); 216 return adminList; 217 218 /*int id = 2; 219 Admin admin = adminMapper.selectById(id); 220 return admin;*/ 221 } 222 }
浏览器访问http://localhost:8080/admin/test