1, 在application.yml中配置
点击查看代码
#mybatis
#配置mapper路径
mybatis:
mapper-locations: classpath:mapper/*.xml
-------------------------------------------------------------
增删改查的实现代码application.yml配置文件
#tomcat 端口
server:
port: 8888
servlet:
context-path: /api
# 数据库四大金刚
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/class009_fjg?characterEncoding=utf8
username: root
password: root
#mybatis
#配置mapper路径
mybatis:
mapper-locations: classpath:mapper/*.xml
po类
package com.bh.po;
public class Dept {
private int deptno;
private String dname;
private String loc;
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
dao层
package com.bh.dao;
import com.bh.po.Dept;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface DeptDAO {
public List<Dept> findall();
public Dept findById(Dept dept);
public int removeByPk(Dept dept);
public int save(Dept dept);
public int modify(Dept dept);
}
mapper.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.bh.dao.DeptDAO">
<select id="findall" resultType="com.bh.po.Dept">
select * from dept
</select>
<select id="findById" resultType="com.bh.po.Dept">
select * from dept where deptno=#{deptno}
</select>
<delete id="removeByPk" parameterType="com.bh.po.Dept">
delete from dept where deptno=#{deptno}
</delete>
<insert id="save" parameterType="com.bh.po.Dept">
insert into dept values(#{deptno},#{dname},#{loc})
</insert>
<update id="modify" parameterType="com.bh.po.Dept">
update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}
</update>
</mapper>
R类
package com.bh.po;
import java.util.Map;
public class R {
private int code;
private String message;
private Map<String,Object> result;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Map<String, Object> getResult() {
return result;
}
public void setResult(Map<String, Object> result) {
this.result = result;
}
}
service层
package com.bh.service;
import com.bh.po.Dept;
import com.bh.po.R;
public interface DeptServic {
public R findall();
public R findById(Dept dept);
public R remove(Dept dept);
public R save(Dept dept);
public R modify(Dept dept);
}
service的实现类
package com.bh.service.impl;
import com.bh.dao.DeptDAO;
import com.bh.po.Dept;
import com.bh.po.R;
import com.bh.service.DeptServic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class DeptServiceImpl implements DeptServic {
@Autowired
private DeptDAO deptDAO;
@Override
public R findall() {
R r = new R();
Map<String, Object> reMap = new HashMap<>();
List<Dept> resultList = deptDAO.findall();
if (resultList.size() ==0 || resultList ==null){
r.setMessage("检索不成功");
r.setCode(2000);
reMap.put("items",null);
r.setResult(reMap);
}else {
r.setMessage("检索成功");
r.setCode(200);
reMap.put("items",resultList);
r.setResult(reMap);
}
return r;
}
@Override
public R findById(Dept dept) {
Dept byId = deptDAO.findById(dept);
R r = new R();
Map<String, Object> reMap = new HashMap<>();
if (byId == null){
r.setMessage("检索不成功");
r.setCode(2000);
reMap.put("items",null);
r.setResult(reMap);
}else {
r.setMessage("检索成功");
r.setCode(200);
reMap.put("items",byId);
r.setResult(reMap);
}
return r;
}
@Override
public R remove(Dept dept) {
R r = new R();
Map<String, Object> reMap = new HashMap<>();
int i = deptDAO.removeByPk(dept);
if (i == 0){
r.setCode(2000);
r.setMessage("删除失败");
reMap.put("items","失败");
r.setResult(reMap);
}else {
r.setCode(200);
r.setMessage("删除成功");
reMap.put("items",i);
r.setResult(reMap);
}
return r;
}
@Override
public R save(Dept dept) {
R r = new R();
Map<String, Object> reMap = new HashMap<>();
int save = deptDAO.save(dept);
if (save == 0){
r.setCode(2000);
r.setMessage("保存失败");
reMap.put("items","失败");
r.setResult(reMap);
}else {
r.setCode(200);
r.setMessage("保存成功");
reMap.put("items",save);
r.setResult(reMap);
}
int a=100;
int c=0;
return r;
}
@Override
public R modify(Dept dept) {
R r = new R();
Map<String, Object> reMap = new HashMap<>();
int modify = deptDAO.modify(dept);
if (modify == 0){
r.setCode(2000);
r.setMessage("修改失败");
reMap.put("items","失败");
r.setResult(reMap);
}else {
r.setCode(200);
r.setMessage("修改成功");
reMap.put("items",modify);
r.setResult(reMap);
}
return r;
}
}
controller层
package com.bh.controller;
import com.bh.po.Dept;
import com.bh.po.R;
import com.bh.service.impl.DeptServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/dept")
public class DeptController {
@Autowired
private DeptServiceImpl deptService;
/*查询全部人员信息*/
@RequestMapping("/findall")
public R findall(){
R findall = deptService.findall();
return findall;
}
/*根据主键查询*/
@RequestMapping("/findbyid")
public R findById(@RequestBody Map<String,Object> params){
Dept dept = new Dept();
dept.setDeptno((Integer) params.get("id"));
R byId = deptService.findById(dept);
return byId;
}
/*根据主键进行删除*/
@RequestMapping("/removebypk")
public R removeByPk(@RequestBody Map<String,Object> params){
Dept dept = new Dept();
dept.setDeptno((Integer) params.get("id"));
R remove = deptService.remove(dept);
return remove;
}
/*保存一条数据*/
@RequestMapping("/save")
public R save(@RequestBody Map<String,Object> params){
Dept dept = new Dept();
dept.setDeptno((Integer) params.get("id"));
dept.setDname((String) params.get("name"));
dept.setLoc((String) params.get("loc"));
R save = deptService.save(dept);
return save;
}
/*根据主键修改*/
@RequestMapping("/modify")
public R modify(@RequestBody Map<String,Object> params){
Dept dept = new Dept();
dept.setDeptno((Integer) params.get("id"));
dept.setDname((String) params.get("name"));
dept.setLoc((String) params.get("loc"));
R modify = deptService.modify(dept);
return modify;
}
}