添加依赖
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>SpringBoot3mybatis</artifactId><version>0.0.1-SNAPSHOT</version><name>SpringBoot3mybatis</name><description>SpringBoot3mybatis</description><properties><java.version>1.8</java.version></properties><dependencies><!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
配置数据库连接
spring:datasource:url: jdbc:mysql://localhost:3306/mybatisdb?serverTimezone=UTC&useUnicode=true&charEncoding=utf-8username: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driver
测试连接
测试连接没问题,数据库就连接上了
@SpringBootTestclass SpringBoot3mybatisApplicationTests {@AutowiredDataSource dataSource;@Testvoid contextLoads() throws SQLException {System.out.println(dataSource.getClass());System.out.println(dataSource.getConnection());}}
Mapper接口
package com.example.springboot3mybatis.mapper;import com.example.springboot3mybatis.pojo.Emp;import org.apache.ibatis.annotations.Mapper;import org.springframework.stereotype.Repository;import java.util.List;//这个注解表示了这是一个mybatis的mapper类@Mapper@Repositorypublic interface EmpMapper {List<Emp> queryEmps();Emp queryEmp(int id);int addEmp(Emp emp);}
mybatis的一些配置
注意事项:
- 他妈个逼的,mybatis是和spring同级!同级!同级!复制笔记复制岔了,导致mybatis在spring的子级,报错研究了尼玛一天,没想到在这里。给自己一个警告,下次笔记代码完整复制,不要一句一句的复制。特别是这种对缩进很严格的东西。
关于这个报错:解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
spring:datasource:url: jdbc:mysql://localhost:3306/mybatisdb?serverTimezone=UTC&useUnicode=true&charEncoding=utf-8username: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driver#整合mybatismybatis:#取别名的包type-aliases-package: com.example.springboot3mybatis.pojo#mapper映射文件的路径mapper-locations: classpath:mybatis/mapper/*.xml#下划线驼峰映射configuration:map-underscore-to-camel-case: true
mapper映射文件
在resource/mybatis/mapper目录下
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.example.springboot3mybatis.mapper.EmpMapper"><select id="queryEmps" resultType="Emp">select*from t_emp</select><select id="queryEmp" resultType="Emp">select * from t_emp where eid=#{id}</select><insert id="addEmp" parameterType="Emp">insert t_emp values(null,#{empName},#{age},#{sex},#{email},#{Dept.did})</insert></mapper>
控制器层
dao,service层就没写,笔记测试代码。
如上配置下来,就能访问接口了
package com.example.springboot3mybatis.controller;import com.example.springboot3mybatis.mapper.EmpMapper;import com.example.springboot3mybatis.pojo.Emp;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestControllerpublic class EmpController {@Autowiredprivate EmpMapper empMapper;@GetMapping("/queryemps")public List<Emp> queryEmps() {List<Emp> emps = empMapper.queryEmps();return emps;}}
