springboot集成mybatis
使用工具
idea、mysql
步骤
1.新建springboot项目
2.设置数据库
2.1修改pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2.2增加配置文件
server:
port: 8090
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3308/ss-0722?characterEncoding=UTF-8&autoReconnect=true&useUnicode=true&useSSL=false&tinyInt1isBit=false&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
3.集成mybatis
3.1 修改pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
需要额外补充 由于xml文件位置未放在resources下,编译时需要将xml也处理,不然mybatis无法找到xml文件
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
3.2 修改pom.xml
mybatis:
mapper-locations: classpath*:com/example/demo/mapper/xml/*.xml
3.3 修改启动类
启动类上增加注解 @MapperScan(“com.example.demo.mapper”) ,这样对应的mapper可以不加@mapper注解
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}