springboot 整合 tk.mybatis 快速实现 增删改查
1、pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency>
2、配置启动
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import tk.mybatis.spring.annotation.MapperScan; // 注意包路径 @SpringBootApplication @MapperScan(basePackages = {"com.example.demo.mapper"}) // 注意这行 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
3、创建实体类
package com.example.demo.dataobject; import javax.persistence.GeneratedValue; import javax.persistence.Id; import java.sql.Timestamp; /** * Created with IntelliJ IDEA. * Computer: Administrator * Author:DaiGua * Date: 2021/4/15 0015 * Time: 16:00 * Description: No Description */ public class Test { @Id @GeneratedValue(generator = "JDBC") private Integer id; private String name; private Timestamp timestamp; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Timestamp getTimestamp() { return timestamp; } public void setTimestamp(Timestamp timestamp) { this.timestamp = timestamp; } }
4、创建mapper
package com.example.demo.mapper; import com.example.demo.dataobject.Test; import org.springframework.stereotype.Repository; import tk.mybatis.mapper.common.Mapper; /** * Created with IntelliJ IDEA. * Computer: Administrator * Author:DaiGua * Date: 2021/4/15 0015 * Time: 15:58 * Description: No Description */ @Repository public interface TestMapper extends Mapper<Test> { }
5、创建service
package com.example.demo.service; /** * Created with IntelliJ IDEA. * Computer: Administrator * Author:DaiGua * Date: 2021/4/15 0015 * Time: 16:04 * Description: No Description */ public interface TestService { void addData(); }
package com.example.demo.service.impl; import com.example.demo.dataobject.Test; import com.example.demo.mapper.TestMapper; import com.example.demo.service.TestService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.sql.Timestamp; import java.util.Date; import java.util.UUID; /** * Created with IntelliJ IDEA. * Computer: Administrator * Author:DaiGua * Date: 2021/4/15 0015 * Time: 16:04 * Description: No Description */ @Service public class TestServiceImpl implements TestService { final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private TestMapper testMapper; @Override public void addData() { Test test = new Test(); test.setName(UUID.randomUUID().toString()); test.setTimestamp(new Timestamp(new Date().getTime())); int insert = testMapper.insert(test); logger.info("this add success result:" + insert); } }
6、创建测试controller
package com.example.demo.comtroller; import com.example.demo.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created with IntelliJ IDEA. * Computer: Administrator * Author:DaiGua * Date: 2021/4/15 0015 * Time: 16:12 * Description: No Description */ @RestController @RequestMapping("/test") public class TestController { @Autowired private TestService testService; @GetMapping("/add") public String test(){ for (int i = 0; i < 200; i++) { testService.addData(); } return "success"; } }
注、mapper中可用的方法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)