Springboot 集成Mybatis 增删改查 使用XML
基于 https://www.cnblogs.com/suphowe/p/13152087.html 进行修改
一、修改XML配置
src\main\resources\mapper\TestMapper.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 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、删除
分类:
mybatis
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)