package com.controller;
import com.pojo.person;
import com.service.PersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author liujing
* @date 2020/9/5--13:07
*/
@Controller
public class PersonController {
@Autowired
private PersionService persionService;
@RequestMapping("/getbyid")
@ResponseBody
public Object getById(int id){
person allById = persionService.getAllById(id);
System.out.println(allById.toString());
return allById;
}
// 添加用户
@ResponseBody
@RequestMapping("/add")
public Object add(person p){
System.out.println(p);
int i = persionService.addPersion(p);
Map<String, String> map = new HashMap<>();
map.put("code","200");
map.put("msg","添加成功");
return map;
}
// 修改用户
@ResponseBody
@RequestMapping("/edit")
public Object edit(person p){
System.out.println(p.toString());
int i1 = persionService.updateById(p);
System.out.println(p);
Map<String, String> map = new HashMap<>();
map.put("code","200");
map.put("msg","编辑成功");
return map;
}
@ResponseBody
@RequestMapping("/showPersion")
public Object showPersion( int page,
int rows){
Object all02 = persionService.getAll02(page,rows);
return all02;
}
@ResponseBody
@RequestMapping("/deleteAll")
public Object deleteAll(HttpServletRequest request){
String[] ids = request.getParameterValues("id");
System.out.println(ids[0]);
System.out.println(ids[1]);
int i = persionService.deleteBatch(ids);
Map<String, String> map = new HashMap<>();
map.put("code","200");
map.put("msg","批量删除成功");
return map;
}
}
service层
代码语言:javascript复制
package com.service;
import com.pojo.person;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @author
* @date 2020/9/5--19:45
*/
public interface PersionService {
public Object getAll02(int page, int rows);
public int deleteBatch(@Param("ids") String[] ids);
public int addPersion(person p);
public person getAllById(int id);
public int updateById(person p);
}
代码语言:javascript复制
package com.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mapper.persionMapper;
import com.pojo.person;
import com.service.PersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.sound.midi.Soundbank;
import java.util.List;
/**
* @author
* @date 2020/9/5--19:46
*/
@Service
public class PersionServiceimpl implements PersionService {
@Autowired
private persionMapper persionMapper;
@Override
public Object getAll02(int page, int rows) {
List<person> all01 = persionMapper.getAll01();
return all01;
}
@Override
public int deleteBatch(String[] ids) {
int i = persionMapper.deleteAll(ids);
return i;
}
@Override
public int addPersion(person p) {
int i = persionMapper.addPersion(p);
return i;
}
@Override
public person getAllById(int id) {
person byId = persionMapper.getById(id);
return byId;
}
@Override
public int updateById(person p) {
int update = persionMapper.update(p);
return update;
}
}
pojo层
代码语言:javascript复制
package com.pojo;
import com.fasterxml.jackson.annotation.JsonFilter;
import org.springframework.stereotype.Component;
import java.util.Objects;
/**
* @author
* @date 2020/9/5--13:05
*/
public class person {
private int id;
private String name;
private String gender;
private String age;
private String phone;
private String address;
public person() {
}
public person(int id, String name, String gender, String age, String phone, String address) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.phone = phone;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
person person = (person) o;
return id == person.id &&
Objects.equals(name, person.name) &&
Objects.equals(gender, person.gender) &&
Objects.equals(age, person.age) &&
Objects.equals(phone, person.phone) &&
Objects.equals(address, person.address);
}
@Override
public int hashCode() {
return Objects.hash(id, name, gender, age, phone, address);
}
@Override
public String toString() {
return "person{"
"id=" id
", name='" name '''
", gender='" gender '''
", age='" age '''
", phone='" phone '''
", address='" address '''
'}';
}
}
mapper层
代码语言:javascript复制
package com.mapper;
import com.pojo.person;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author
* @date 2020/9/5--19:26
*/
@Component
@Mapper
public interface persionMapper {
// 通过id获取
public person getById(int id);
//得到全部
public List<person> getAll01();
//批量删除
public int deleteAll(@Param("ids") String[] ids);
//添加用户
public int addPersion(person p);
// 修改用户
public int update(person p);
}
代码语言:javascript复制
<?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.mapper.persionMapper">
<select id="getAll01" resultType="com.pojo.person" >
SELECT
id,
name ,
age,
gender,
phone,
address
FROM persion
</select>
<select id="getById" resultType="com.pojo.person" >
SELECT
id,
name ,
age,
gender,
phone,
address
FROM persion where id=#{id}
</select>
<delete id="deleteAll">
delete from persion where id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<insert id="addPersion">
insert into persion(name,age,gender,phone,address) values (#{name},#{age},#{gender},#{phone},#{address})
</insert>
<update id="update">
update persion
<trim prefix="set" suffixOverrides=",">
<if test="name != null">
name =#{name},
</if>
<if test="gender != null">
gender =#{gender},
</if>
<if test="age != null">
age =#{age},
</if>
<if test="phone != null">
phone =#{phone},
</if>
<if test="address != null">
address =#{address},
</if>
</trim>
where id=#{id}
</update>
</mapper>