SpringBoot mybatis integration and Notes
The main steps
- Add rely mybatis
- Configuration data in the configuration file source information
- Write pojo mapper interfaces mapeer mapping file
- Manual configuration package scanning mybatis
Add @MapperScan the main startup class
1: import dependencies
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
2: Configuration Data source information
In the configuration application.yml
#DB Configation
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
// Note If you can not connect to the database problem GMT, adding later tx useUnicode = true & characterEncoding = utf -8 & useSSL = false & serverTimezone = appear?
Url: jdbc: MySQL: // 127.0.0.1:3306/tx
username: root
password: 813100
jpa:
database: MySQL
show-sql: true
generate-ddl: true
3: Writing pojo entity class and the corresponding interface and a mapper mapping file
pojo entity class
package com.offcn.springbootdemo1.pojo;
public class UUser {
Private Integer ID;
Private String username;
Private String password;
Private String name;
// add here set, get, and rewriting constructor toString
}
mapper Interface
package com.offcn.springbootdemo1.mapper;
import com.offcn.springbootdemo1.pojo.UUser;
import java.util.List;
public interface UUserMapper {
List<UUser> selectUUser();
}
mapper mapping file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.offcn.springbootdemo1.mapper.UUserMapper">
<select id="selectUUser" resultType="com.offcn.springbootdemo1.pojo.UUser">
select * from user
</select>
</mapper>
注意:如果mapper接口和mapper映射文件放在同一个地方
那么在运行会出现错误
解决方案:
1:在resources目录下建立一个和mapper接口相同的目录结构,把mapper映射文件放进去
2:如果想把mapper接口和mapper映射文件放在一起
那么在pom.xml中添加如下配置
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
4:手动配置mybatis扫描
在启动类上添加注解@MapperScan
package com.offcn.springbootdemo1;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.offcn.springbootdemo1.mapper")
public class Springbootdemo1Application {
public static void main(String[] args) {
SpringApplication.run(Springbootdemo1Application.class, args);
}
}
5:在Controller中进行测试
package com.offcn.springbootdemo1.controller;
//导包
@Controller
public class UUserController {
@Resource
private UUserMapper userMapper;
@RequestMapping("aa")
@ResponseBody
public List<UUser> selectUUser(){
List<UUser> uUsers = userMapper.selectUUser();
return uUsers;
}
}