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中可用的方法