package com.openlab.bean;
public class Member {
private Integer id;
private String name;
private Integer age;
private String address;
public Member() {
}
public Member(String name, Integer age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Member{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
package com.openlab.bean;
import java.util.List;
/**
* @author mao on 2021/3/29.
* @version 1.0 maozhijob@163.com
*/
public class Result {
private int code;
private String msg;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<Member> getData() {
return data;
}
public void setData(List<Member> data) {
this.data = data;
}
private int count ;
private List<Member> data;
}
controller:
package com.openlab.controller;
import com.openlab.bean.Member;
import com.openlab.bean.Result;
import com.openlab.service.IMember;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MemberController {
@Autowired
IMember iMember;
@RequestMapping(value = "/",method = RequestMethod.GET)
public String index(){
return "index";
}
@ResponseBody
@RequestMapping(value = "/findByid",method = RequestMethod.GET)
public Member findByid(Integer id){
Member member = iMember.findById(id);
return member;
}
@ResponseBody
@RequestMapping(value = "/findAll",method = RequestMethod.GET)
public Result findAll(){
Result all = iMember.findAll();
return all;
}
@ResponseBody
@RequestMapping(value = "/updateByid",method = RequestMethod.GET)
public Boolean updateByid(Member member){
Boolean aBoolean = iMember.updateMember(member);
return aBoolean;
}
@ResponseBody
@RequestMapping(value = "/insert",method = RequestMethod.GET)
public boolean insert(Member member){
Boolean saveMember = iMember.saveMember(member);
return saveMember;
}
@ResponseBody
@RequestMapping(value = "/deleteByid",method = RequestMethod.GET)
public boolean deleteByid(Integer id){
Boolean saveMember = iMember.deleteById(id);
return saveMember;
}
}
DAO:
package com.openlab.dao;
import com.openlab.bean.Member;
import org.apache.ibatis.annotations.*;
import org.mybatis.spring.annotation.MapperScan;
import java.util.List;
/**
* @author Administrator
*/
public interface MemberMapper {
@Select("select * from member")
List<Member> findAll();
/**
* @param id
* @return
*/
@Select("select * from member where id=#{id}")
Member findById(Integer id);
@Insert("insert into member (id,name,age,address) values(#{id},#{name},#{age},#{address})")
Boolean saveMember(Member member);
@Update({
"<script> ",
"update member set ",
"<if test = \"name != null\"> ",
" name=#{name}, ",
"</if> ",
"<if test = \"age != null\"> ",
" age=#{age}, ",
"</if> ",
"<if test = \"address != null\"> ",
" address=#{address} ",
"</if> ",
" WHERE id=#{id}",
"</script>"
})
Boolean updateMember(Member member);
@Delete("delete from member where id=#{id}")
Boolean deleteById(@Param("id") Integer id);
}
Service:
package com.openlab.service;
import com.openlab.bean.Member;
import com.openlab.bean.Result;
import java.util.List;
public interface IMember {
public Result findAll();
public Member findById(Integer id);
public Boolean saveMember(Member member);
public Boolean updateMember(Member member);
public Boolean deleteById(Integer id);
}
package com.openlab.service;
import com.openlab.bean.Member;
import com.openlab.bean.Result;
import com.openlab.dao.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class IMemberImpl implements IMember {
@Autowired
MemberMapper memberMapper;
@Override
public Result findAll() {
Result re = new Result();
List<Member> list = memberMapper.findAll();
re.setCode(0);
re.setCount(list.size());
re.setData(list);
re.setMsg("查询成功");
return re;
}
@Override
public Member findById(Integer id) {
return memberMapper.findById(id);
}
@Override
public Boolean saveMember(Member member) {
return memberMapper.saveMember(member);
}
@Override
public Boolean updateMember(Member member) {
return memberMapper.updateMember(member);
}
@Override
public Boolean deleteById(Integer id) {
return memberMapper.deleteById(id);
}
}
/*
Navicat Premium Data Transfer
Source Server : 本地
Source Server Type : MySQL
Source Server Version : 50725
Source Host : localhost:3306
Source Schema : lanmoyun
Target Server Type : MySQL
Target Server Version : 50725
File Encoding : 65001
Date: 29/03/2021 15:23:34
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for member
-- ----------------------------
DROP TABLE IF EXISTS `member`;
CREATE TABLE `member` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(20) NULL DEFAULT NULL,
`address` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of member
-- ----------------------------
INSERT INTO `member` VALUES (2, '张三', 23, '陕西');
INSERT INTO `member` VALUES (4, '张三', 23, '陕西');
SET FOREIGN_KEY_CHECKS = 1;