- 添加依赖
- 添加配置文件(MySql Mybatis )
- 创建Mapper接口
- 创建xml映射文件
添加依赖
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-jdbc
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.0
修改配置文件
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 getAll();
}
创建userService
package com.demo.service;
import com.demo.pojo.User;
import java.util.List;
public interface UserService {
List 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 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 getAll() {
return userService.getAll();
}
}
创建UserMapper.xml映射文件
select * from demo_user
编辑启动类
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 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=女)]
成功获取表格中的所有信息!



