在springboot环境下tk-mybatis的使用记录
1. 新建springboot工程
访问https://start.spring.io/,新建一个springboot工程。
自动生成的工程主要的注意点如下:
1)pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
将Spring Boot应用打包为可执行的jar或war文件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2. 使用tk-mybatis构建数据库访问示例
1)application.properties
resources目录下的application.properties文件配置如下:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
logging.level.com.tkmybatislearning.demo=DEBUG
2)entity
新建entity实体类Country.java, 命名与数据库的表名country对应:
package com.tkmybatislearning.demo.entity;
import javax.persistence.Id;
public class Country {
@Id
private Integer id;
private String countryname;
private String countrycode;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCountryname() {
return countryname;
}
public void setCountryname(String countryname) {
this.countryname = countryname;
}
public String getCountrycode() {
return countrycode;
}
public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
}
3)mapper
新增mapper接口文件CountryMapper.java,继承tk的Mapper,可以自定义方法:
package com.tkmybatislearning.demo.mapper;
import org.apache.ibatis.annotations.Select;
import com.tkmybatislearning.demo.entity.Country;
import tk.mybatis.mapper.common.Mapper;
public interface CountryMapper extends Mapper<Country> {
@Select("select * from country where countryname = #{countryname}")
Country selectByCountryName(String countryname);
}
4)service
新增service服务文件CountryService.java,自动注入CountryMapper,可以使用tk自带的方法,对数据库表进行CRUD操作:
package com.tkmybatislearning.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tkmybatislearning.demo.entity.Country;
import com.tkmybatislearning.demo.mapper.CountryMapper;
@Service
public class CountryService {
@Autowired
private CountryMapper countryMapper;
public void testCountry() {
//从 MyBatis 或者 Spring 中获取 countryMapper,然后调用 selectAll 方法
List<Country> countries = countryMapper.selectAll();
//根据主键查询
Country country = countryMapper.selectByPrimaryKey(1);
//或者使用对象传参,适用于1个字段或者多个字段联合主键使用
Country query = new Country();
query.setId(1);
country = countryMapper.selectByPrimaryKey(query);
}
}
3. 新建应用启动程序
DemoApplication.java:
package com.tkmybatislearning.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.tkmybatislearning.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4. 使用单元测试验证示例
新建DemoApplicationTests.java:
package com.tkmybatislearning.demo;
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 com.tkmybatislearning.demo.service.CountryService;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class DemoApplicationTests {
@Autowired
private CountryService countryService;
@Test
public void contextLoads() {
countryService.testCountry();
}
}