Springboot 集成Mybatis 增删改查 使用XML

基于 https://www.cnblogs.com/suphowe/p/13152087.html 进行修改

一、修改XML配置

src\main\resources\mapper\TestMapper.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.soft.dao.MybatisTestMapper">

    <select id="findAll" resultType="com.soft.entity.Test">
        select * from sys_test
    </select>

    <select id="findByName" resultType="com.soft.entity.Test">
        select * from sys_test where name=#{name}
    </select>

    <insert id="insertSysTest" parameterType="com.soft.entity.Test">
        insert into sys_test (id, user, name, tel) values (#{id},#{user},#{name},#{tel})
    </insert>

    <update id="updateSysTest" parameterType="com.soft.entity.Test">
        update sys_test set user=#{user},name=#{name},tel=#{tel} where id=#{id}
    </update>

    <delete id="deleteSysTest" parameterType="com.soft.entity.Test">
        delete from sys_test where id=#{id}
    </delete>
</mapper>

二、修改Java代码

MybatisTestMapper.java

package com.soft.dao;

import com.soft.entity.Test;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component
public interface MybatisTestMapper {

    List<Test> findAll();

    List<Test> findByName(Test test);

    int insertSysTest(Test test);

    int updateSysTest(Test test);

    int deleteSysTest(Test test);
}

  

MybatisTestServiceImpl.java
package com.soft.service.impl;

import com.soft.dao.MybatisTestMapper;
import com.soft.entity.Test;
import com.soft.service.IMybatisServiceTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class MybatisTestServiceImpl implements IMybatisServiceTest {

    @Autowired
    private MybatisTestMapper mybatisTestMapper;

    @Override
    public List<Test> findAll() {
        return mybatisTestMapper.findAll();
    }

    @Override
    public List<Test> findByName(String name) {
        Test test = new Test();
        test.setName(name);
        return mybatisTestMapper.findByName(test);
    }

    @Override
    public HashMap<String, Object> insertSysTest(int id, String user, String name, String tel) {
        Test test = new Test();
        test.setId(id);
        test.setUser(user);
        test.setName(name);
        test.setTel(tel);
        int result = mybatisTestMapper.insertSysTest(test);
        HashMap<String, Object> returnMap = new HashMap<>(1);
        returnMap.put("count", result);
        return returnMap;
    }

    @Override
    public HashMap<String, Object> updateSysTest(int id, String user, String name, String tel) {
        Test test = new Test();
        test.setId(id);
        test.setUser(user);
        test.setName(name);
        test.setTel(tel);
        int result = mybatisTestMapper.updateSysTest(test);
        HashMap<String, Object> returnMap = new HashMap<>(1);
        returnMap.put("count", result);
        return returnMap;
    }

    @Override
    public HashMap<String, Object> deleteSysTest(int id) {
        Test test = new Test();
        test.setId(id);
        int result = mybatisTestMapper.deleteSysTest(test);
        HashMap<String, Object> returnMap = new HashMap<>(1);
        returnMap.put("count", result);
        return returnMap;
    }
}

IMybatisServiceTest.java

package com.soft.service;

import com.soft.entity.Test;

import java.util.HashMap;
import java.util.List;

public interface IMybatisServiceTest {

    List<Test> findAll();

    List<Test> findByName(String name);

    HashMap<String, Object> insertSysTest(int id, String user, String name, String tel);

    HashMap<String, Object> updateSysTest(int id, String user, String name, String tel);

    HashMap<String, Object> deleteSysTest(int id);
}

 

MybatisTestController.java

package com.soft.controller;

import com.google.gson.Gson;
import com.soft.entity.Test;
import com.soft.service.IMybatisServiceTest;
import com.soft.util.BsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@Api(value = "Mybatis测试,Xml")
@RequestMapping(value = "/mybatis")
public class MybatisTestController {

    @Autowired
    private IMybatisServiceTest mybatisServiceTest;

    @Autowired
    public BsUtil bsUtil;

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    @ApiOperation(value = "Mybatis查询测试,查询全部", notes = "查询全部")
    public String findUser() {
        List<Test> list = mybatisServiceTest.findAll();
        HashMap<String, Object> result = new HashMap<>();
        bsUtil.createReturnMsg(result, 200, list);
        return new Gson().toJson(result);
    }

    @RequestMapping(value = "/findByName", method = RequestMethod.POST)
    @ApiOperation(value = "Mybatis查询测试,按名称查询", notes = "按名称查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", value = "name", dataType = "String")
    })
    public String findByName(String name) {
        List<Test> list = mybatisServiceTest.findByName(name);
        HashMap<String, Object> result = new HashMap<>();
        bsUtil.createReturnMsg(result, 200, list);
        return new Gson().toJson(result);
    }

    @RequestMapping(value = "/insertSysTest", method = RequestMethod.POST)
    @ApiOperation(value = "Mybatis测试,新增", notes = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "user", value = "user", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "name", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "tel", value = "tel", dataType = "String")
    })
    public String insertSysTest(int id, String user, String name, String tel) {
        HashMap<String, Object> insertSysTest = mybatisServiceTest.insertSysTest(id, user, name, tel);
        HashMap<String, Object> result = new HashMap<>();
        bsUtil.createReturnMsg(result, 200, insertSysTest);
        return new Gson().toJson(result);
    }

    @RequestMapping(value = "/updateSysTest", method = RequestMethod.POST)
    @ApiOperation(value = "Mybatis测试,修改", notes = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "user", value = "user", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "name", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "tel", value = "tel", dataType = "String")
    })
    public String updateSysTest(int id, String user, String name, String tel) {
        HashMap<String, Object> updateSysTest = mybatisServiceTest.updateSysTest(id, user, name, tel);
        HashMap<String, Object> result = new HashMap<>();
        bsUtil.createReturnMsg(result, 200, updateSysTest);
        return new Gson().toJson(result);
    }

    @RequestMapping(value = "/deleteSysTest", method = RequestMethod.POST)
    @ApiOperation(value = "Mybatis测试,删除", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "int")
    })
    public String deleteSysTest(int id) {
        HashMap<String, Object> deleteSysTest = mybatisServiceTest.deleteSysTest(id);
        HashMap<String, Object> result = new HashMap<>();
        bsUtil.createReturnMsg(result, 200, deleteSysTest);
        return new Gson().toJson(result);
    }
}

 

三、测试

1.新增

 

 

 

 2、修改

 

 

 

 3、查询

 

 4、删除

 

 

posted @ 2020-06-18 14:41  suphowe  阅读(1030)  评论(0编辑  收藏  举报