SpringBoot整合Mybatis
- 添加依赖
- 添加配置文件(MySql Mybatis )
- 创建Mapper接口
- 创建xml映射文件
添加依赖
<!--引入数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--springBoot数据库连接 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--spring整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
修改配置文件
server:
port: 8090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
#如果数据库密码以数字0开头 则必须使用""号包裹
#password: "01234"
#SpringBoot整合Mybatis配置
mybatis:
type-aliases-package: com.jt.pojo
mapper-locations: classpath:/mybatis/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:
application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
注:配置文件中的中文注释很有可能影响程序从而报错
创建Mapper接口
package com.demo.mapper;
import com.demo.pojo.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
List<User> getAll();
}
创建userService
package com.demo.service;
import com.demo.pojo.User;
import java.util.List;
public interface UserService {
List<User> getAll();
}
创建UserServiceImpl
package com.demo.service;
import com.demo.mapper.UserMapper;
import com.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public List<User> getAll() {
return userMapper.getAll() ;
}
}
创建UserController
package com.demo.controller;
import com.demo.pojo.User;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("getAll")
public List<User> getAll() {
return userService.getAll();
}
}
创建UserMapper.xml映射文件
<?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">
<!--规则: namespace必须与接口一一对应 -->
<mapper namespace="com.demo.mapper.UserMapper">
<!--
定义别名包: 添加了别名包之后可以简化resultType编辑.
-->
<select id="getAll" resultType="User">
select * from demo_user
</select>
<!--驼峰命名规则
表字段: user_id,user_name
对象的属性: userId,userName
resultType: 保证属性与字段名称必须一致.
Mybatis提供了驼峰命名规则:
规则: 字段user_id~~~去除_线~~~之后映射对象的属性
userId
-->
</mapper>
编辑启动类
package com.demo.springmybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.demo.mapper") //Spring内部为mybatis创建动态代理对象--JDK代理
public class SpringMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMybatisApplication.class, args);
}
}
编辑测试类
package com.demo.springmybatis;
import com.demo.mapper.UserMapper;
import com.demo.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class SpringMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testGetAll(){
System.out.println(userMapper.getClass());
List<User> userList = userMapper.getAll();
System.out.println(userList);
}
}
测试结果
class com.sun.proxy.$Proxy71
2021-10-06 16:19:00.475 INFO 8980 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-10-06 16:19:00.767 INFO 8980 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
[User(id=1, name=lily, age=5, sex=女), User(id=2, name=mary, age=18, sex=女)]
成功获取表格中的所有信息!