Springboot 集成Mybatis 使用XML
一、新建表
1 2 3 4 5 6 7 | CREATE TABLE `suphowe`.`Untitled` ( `id` int (11) NOT NULL, `user` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `tel` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; |
插入数据
二、Springboot配置
1.添加依赖
1 2 3 4 5 6 | <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> |
2.配置信息
src\main\resources\mybatis\mybatis-config.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 25 26 | <?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <settings> <setting name= "callSettersOnNulls" value= "true" /> <setting name= "cacheEnabled" value= "true" /> <setting name= "lazyLoadingEnabled" value= "true" /> <setting name= "aggressiveLazyLoading" value= "true" /> <setting name= "multipleResultSetsEnabled" value= "true" /> <setting name= "useColumnLabel" value= "true" /> <setting name= "useGeneratedKeys" value= "false" /> <setting name= "autoMappingBehavior" value= "PARTIAL" /> <setting name= "defaultExecutorType" value= "SIMPLE" /> <setting name= "mapUnderscoreToCamelCase" value= "true" /> <setting name= "localCacheScope" value= "SESSION" /> <setting name= "jdbcTypeForNull" value= "NULL" /> </settings> <typeAliases> <typeAlias alias= "Integer" type= "java.lang.Integer" /> <typeAlias alias= "Long" type= "java.lang.Long" /> <typeAlias alias= "HashMap" type= "java.util.HashMap" /> <typeAlias alias= "LinkedHashMap" type= "java.util.LinkedHashMap" /> <typeAlias alias= "ArrayList" type= "java.util.ArrayList" /> <typeAlias alias= "LinkedList" type= "java.util.LinkedList" /> </typeAliases> </configuration> |
application.properties
1 2 3 4 5 6 7 8 9 10 | #################### 数据库配置 ################################ spring.primary.datasource.driver- class -name=com.mysql.jdbc.Driver spring.primary.datasource.url=jdbc:mysql: //127.0.0.1:3306/suphowe?useUnicode=true&characterEncoding=utf8 spring.primary.datasource.username=root spring.primary.datasource.password=root # ===================mybatis======================== mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.soft.entity |
src\main\resources\mapper\TestMapper.xml
1 2 3 4 5 6 7 | <?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 > </mapper> |
三、Java代码
Test.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 | package com.soft.entity; public class Test { private Integer id; private String user; private String name; private String tel; public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getUser() { return user; } public void setUser(String user) { this .user = user; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getTel() { return tel; } public void setTel(String tel) { this .tel = tel; } @Override public String toString() { return "Test{" + "id=" + id + ", user='" + user + '\ '' + ", name='" + name + '\ '' + ", tel='" + tel + '\ '' + '}' ; } } |
MybatisTestMapper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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(); } |
IMybatisServiceTest.java
1 2 3 4 5 6 7 8 9 10 | package com.soft.service; import com.soft.entity.Test; import java.util.List; public interface IMybatisServiceTest { List<Test> findAll(); } |
MybatisTestServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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.List; @Service public class MybatisTestServiceImpl implements IMybatisServiceTest { @Autowired private MybatisTestMapper mybatisTestMapper; @Override public List<Test> findAll(){ return mybatisTestMapper.findAll(); } } |
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 | package com.soft.controller; import com.google.gson.Gson; import com.soft.entity.Test; import com.soft.entity.User; 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, "0" , list); return new Gson().toJson(result); } } |
四、测试
分类:
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)