本章Sprintboot整合tkmybatis实现数据库访问,默认已通过tk-mybatis generator自动生成了po、dao和*mapper.xml(参考“003-数据库-tkmybatis-01-使用tk-mybatis generator 自动生成代码”),补全项目springboot相关依赖,包括spring-boot-starter、spring-boot-starter-web。
1、在pom.xml文件中添加依赖
<!--tk.mybatis依赖-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!-- mysql驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
<scope>runtime</scope>
</dependency>
2、定义service文件,包括接口和实现类
这里用IOrdersService和OdersService举例
IOrdersService.java
package com.example.demotkmybatis.service;
import com.example.demotkmybatisgenerator.pojo.Orders;
import java.util.List;
public interface IOrdersService {
public List<Orders> findAll();
}
OdersService.java(在实现类中,通过autowired注入Dao层OrdersMapper类,其中OrdersMapper类是tik-mybatis generator自动生成的)
package com.example.demotkmybatis.service.impl;
import com.example.demotkmybatis.service.IOrdersService;
import com.example.demotkmybatisgenerator.mapper.OrdersMapper;
import com.example.demotkmybatisgenerator.pojo.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OdersService implements IOrdersService {
@Autowired
private OrdersMapper ordersMapper;
@Override
public List<Orders> findAll() {
return ordersMapper.selectAll();
}
}
3、定义controller文件
OrderController.java
package com.example.demotkmybatis.controller;
import com.example.demotkmybatis.service.IOrdersService;
import com.example.demotkmybatis.service.impl.UserService;
import com.example.demotkmybatisgenerator.pojo.Orders;
import com.example.demotkmybatisgenerator.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class OrderController {
@Autowired
private IOrdersService iOrdersService;
@RequestMapping("/findAllOrders")
public List<Orders> findAll() {
List<Orders> ordersList = iOrdersService.findAll();
return ordersList;
}
}
4、定义springboot启动类
DemoTkmybatisApplication.java
package com.example.demotkmybatis;
import tk.mybatis.mapper.provider.base.BaseSelectProvider;
import tk.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demotkmybatisgenerator.mapper")
public class DemoTkmybatisApplication {
public static void main(String[] args) {
SpringApplication.run(DemoTkmybatisApplication.class, args);
}
}
注意@MapperScan一定要配置,该注解制定了DAO层mapper的扫描路径,如果没有配置,那么service中注入的mapper对象,比如OrdersMapper,会注入失败
5、添加application.properties文件
根据实际情况修改配置内容
# tomcat配置
server.port=8080
# datasource配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/foodie-shop?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
# TkMybatis配置
## mapper映射文件,配置自动生成的*mapper.xml文件的位置
mybatis.mapper-locations=classpath*:mapper/*.xml
## 所有POJO类所在包路径
mybatis.type-aliases-package=com.example.demotkmybatisgenerator.pojo
## 通用mapper的配置,默认值为tk.mybatis.mapper.common.Mapper,tk-mybatis generator自动生成的mapper文件都继承自该类
mapper.mappers: tk.mybatis.mapper.common.Mapper
6、启动application应用程序