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 }

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

Spring Boot使用Mybatis实现增删改查的更多相关文章

  1. 上手spring boot项目(三)之spring boot整合mybatis进行增删改查的三种方式。

    1.引入依赖. <!--springboot的web起步依赖--><dependency> <groupId>org.springframework.boot< ...

  2. 上手spring boot项目(三)之spring boot整合mybatis进行增删改查

    使用mybatis框架进行增删改查大致有两种基础方式,一种扩展方式.两种基础方式分别是使用xml映射文件和使用方法注解.扩展方式是使用mybatis-plus的方式,其用法类似于spring-data ...

  3. 使用 Spring Boot 搭建一套增删改查(无多余代码)

    前言 这是我学习 Spring Boot 的第三篇文章,终于可以见到效果了.错过的同学可以看看之前的文章 我们为什么要学习 Spring Boot Spring Boot 入门详细分析 在入门的基础上 ...

  4. Spring Boot实现学生信息增删改查

    上一篇博客写了如何初始化一个简单的Spring Boot项目,这次详细记录一下如何连接数据库并实现增删改查基本操作. 我使用的是MySQL 5.5+Navicat,MySQL量级比较轻,当然微软的SQ ...

  5. Spring Boot GraphQL 实战 02_增删改查和自定义标量

    hello,大叫好,我是小黑,又和大家见面啦~ 今天我们来继续学习 Spring Boot GraphQL 实战,我们使用的框架是 https://github.com/graphql-java-ki ...

  6. spring boot集成mongodb的增删改查

    添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  7. Spring JdbcTemplate框架搭建及其增删改查使用指南

    Spring JdbcTemplate框架搭建及其增删改查使用指南 前言: 本文指在介绍spring框架中的JdbcTemplate类的使用方法,涉及基本的Spring反转控制的使用方法和JDBC的基 ...

  8. 学习MyBatis必知必会(5)~了解myBatis的作用域和生命周期并抽取工具类MyBatisUtil、mybatis执行增删改查操作

    一.了解myBatis的作用域和生命周期[错误的使用会导致非常严重的并发问题] (1)SqlSessionFactoryBuilder [ 作用:仅仅是用来创建SqlSessionFactory,作用 ...

  9. Spring Boot入门系列(六)如何整合Mybatis实现增删改查

    前面介绍了Spring Boot 中的整合Thymeleaf前端html框架,同时也介绍了Thymeleaf 的用法.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/z ...

随机推荐

  1. js 向上滚屏

    <!doctype html><html><head><meta charset="utf-8"><title>< ...

  2. Composer 包版本的范围指定(版本约束)

    1. 包版本范围指定(自动下载版本约束范围中的最新版) 名称 实例 说明 不指定版本 根据当前Path环境变量中的php版本下载最合适的最新版 确切的版本 6.0.1 指定下载的具体版本号 范围 &g ...

  3. 手把手带你体验鸿蒙 harmonyOS

    wNlRGd.png 前言 本文已经收录到我的 Github 个人博客,欢迎大佬们光临寒舍: 我的 GIthub 博客 学习导图 image.png 一.为什么要尝鲜 harmonyos? wNlfx ...

  4. 【NOIP2014模拟】高级打字机

    题目描述 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序,支持如下3种操作: T x:在文章末尾打下一个小写字母x.(typ ...

  5. 软件工程与UML作业1

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...

  6. apche编译安装

    常见的网页类型:htm,html,shtml,stm.php,asp,aspx,shtm,jsp等等Apache本事只处理静态页面,处理动态页面需要使用libphp5.so这个模块去工作,编译php也 ...

  7. SessionMiddleware源码分析

    settings.py文件中 MIDDLEWARE = [ 'django.contrib.sessions.middleware.SessionMiddleware', ] # from djang ...

  8. 7.kafka HA

  9. Docker:三、深入Docker容器&Asp.net发版

    各位看官,我们前面已经有过两篇文章的研究了,哈哈,都是皮毛... 今天我们来看看docker容器内部,一探究竟... 一.进入docker容器 进入Linux容器非常简单,直接使用如下命令即可: do ...

  10. linux学习(十)linux安装MySQL

    一.前言 由于我使用的是阿里云的服务器,后面会加入配置阿里云的部分,非阿里云的linux系统可以省略后面的步骤,根据自己系统的情况进行配置~ PS:我安装的是mysql5.7.24的版本,其他版本的M ...