工具
创建springboot项目
1、File->New->Project->Spring Initializr
注意选择自己需要的spring boot版本
点击Finish完成
2、pom.xml文件引入tk.mybatis
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- mybatis -->
<!-- <dependency>-->
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!-- <version>2.1.0</version>-->
<!-- </dependency>-->
<!-- tk.mybatis -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- <version>8.0.23</version>-->
<scope>runtime</scope>
</dependency>
<!-- lombok 看个人编码习惯可要可不要 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
</dependencies>
3、修改启动类
增加@MapperScan
package cn.com.xy.demo;
import tk.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = {"cn.com.xy.demo.*.mapper"})
public class TestSpringDemoApplication {
public static void main(String[] args) {
SpringApplication.run(TestSpringDemoApplication.class, args);
}
}
4、配置数据库连接(此配置为本地数据库)
server.port = 9999
spring.datasource.url = jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf-8&nullCatalogMeansCurrent=true
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.initial-size = 1
spring.datasource.min-idle = 1
spring.datasource.max-active = 20
spring.datasource.test-on-borrow = true
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
mybatis.type-aliases-package = cn.com.xy.demo.ts.entity
mybatis.apper-locations = classpath:mapper/*.xml
# 打印sql语句
logging.level.com.example.demo.ts.mapper = debug
逆向工程生成实体类
1、idea连接本地数据库
2、View -> Tool Windows -> Persistence
3、Persistence -> Generate Persistence Mapping -> By Database Schema
4、选择指定的表
5、创建dao和service层即可
踩坑一:
Error invoking SqlProvider method 'public java.lang.String tk.mybatis.mapper.mapperhelper.MapperTemp
扫描的包引入错误
踩坑二:
编译运行时提示解决:保证java 编译的版本一致