mybatis关于resultMap的简单使用
1 2 3 4 5 6 7 8 | CREATE TABLE today_task ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '主键' , task_name VARCHAR ( 20 ) COMMENT '任务名称' , task_owner VARCHAR ( 20 ) COMMENT '任务属主' , task_content VARCHAR( 255 ) COMMENT '任务内容' , sex CHAR( 1 ) COMMENT '任务性别' , birth DATETIME COMMENT '任务创建时间' ) |
1 2 3 4 5 6 | INSERT INTO today_task(task_name,task_owner,task_content,sex,birth) VALUE ( '学习mybatis' , 'admin' , '' , '男' ,NOW()), ( '学习spring' , 'admin' , '' , '男' ,NOW()), ( '学习springmvc' , 'admin' , '' , '男' ,NOW()), ( '学习linux' , 'admin' , '' , '男' ,NOW()), ( '学习消息中间件' , 'admin' , '' , '男' ,NOW()) |
1 2 3 4 5 6 7 8 9 10 11 | <resultMap id= "getTodayTaskList" type= "com.java.bean.TodayTask" > <id property= "taskId" column= "id" /> <result property= "taskName" column= "task_name" /> <result property= "taskOwner" column= "task_owner" /> <result property= "taskContent" column= "task_content" /> </resultMap> <select id= "getTaskList" resultMap= "getTodayTaskList" > SELECT id,task_name,task_owner,task_content FROM today_task ORDER BY birth DESC </select> |
id元素 ,用于设置主键字段与领域模型属性的映射关系
result元素 ,用于设置普通字段与领域模型属性的映射关系
1 2 3 4 5 6 7 8 9 10 | package com.java.mapper; import com.java.bean.TodayTask; import java.util.List; public interface TodayTaskMapper { List<TodayTask> getTaskList(); } |
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 | package com.java; import com.java.bean.TodayTask; import com.java.mapper.TodayTaskMapper; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; /** * @author yourheart * @Description * @create 2022-05-15 22:21 */ @RunWith (SpringRunner. class ) @SpringBootTest @Slf4j public class TodayTaskTests { @Autowired private TodayTaskMapper todayTaskMapper; @Test public void test(){ List<TodayTask> taskList = todayTaskMapper.getTaskList(); taskList.forEach(a->{ log.info(a.toString()); }); } } |
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 | package com.java.bean; import lombok.Data; import java.util.Date; /** * @author yourheart * @Description * @create 2022-05-15 22:17 */ @Data public class TodayTask { /** * 主键id */ private Integer taskId; /** * 任务名称 */ private String taskName; /** * 任务属主 */ private String taskOwner; /** * 任务内容 */ private String taskContent; /** * 任务性别 */ private String sex; /** * 任务创建时间 */ private Date birth; } |
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 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.2 . 1 .RELEASE</version> <relativePath/> </parent> <groupId>com.mysql</groupId> <artifactId>mysql-service</artifactId> <version> 1.0 -SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https: //mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version> 5.1 . 49 </version> </dependency> <!--lombok依赖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version> 1.18 . 16 </version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version> 2.1 . 0 </version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project> |
1 2 3 4 5 6 7 8 9 10 11 12 13 | server.port= 3307 logging.level.com.java=debug logging.level.web=debug spring.devtools.add-properties= false spring.datasource.driver- class -name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql: //127.0.0.1:3306/yourheart-dev?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true spring.datasource.username=root spring.datasource.password=root mybatis.mapper-locations=classpath:mapping/*.xml |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.java; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author yourheart * @Description * @create 2022-05-14 23:53 */ @SpringBootApplication @MapperScan (basePackages = "com.java.mapper" ) public class MysqlApplication { public static void main(String[] args) { SpringApplication.run(MysqlApplication. class , args); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异