准备工具:IDEA jdk1.8 Navicat for MySQL Postman
一、新建Project
选择依赖:mybatis Web Mysql JDBC
项目结构
pom依赖:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.7.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.beilin</groupId> 12 <artifactId>SpringBoot-Mybatis</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>SpringBoot-Mybatis</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-jdbc</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-web</artifactId> 28 </dependency> 29 30 <!--导入mybatis依赖--> 31 <dependency> 32 <groupId>org.mybatis.spring.boot</groupId> 33 <artifactId>mybatis-spring-boot-starter</artifactId> 34 <version>2.1.0</version> 35 </dependency> 36 37 <!--导入mysql依赖--> 38 <dependency> 39 <groupId>mysql</groupId> 40 <artifactId>mysql-connector-java</artifactId> 41 <version>5.1.47</version> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-starter-test</artifactId> 47 <scope>test</scope> 48 </dependency> 49 </dependencies> 50 51 <build> 52 <plugins> 53 <plugin> 54 <groupId>org.springframework.boot</groupId> 55 <artifactId>spring-boot-maven-plugin</artifactId> 56 </plugin> 57 </plugins> 58 </build> 59 60 </project>
二、创建user表
三、项目配置
1 .在com.beilin下创建controller包、mapper包,entity包;在resources文件夹下创建mapping文件夹(用来存放mapper映射的xml文件)
2 .添加properties配置文件:application.properties
1 #配置mybatis 2 3 #配置xml映射路径 4 mybatis.mapper-locations=classpath:mapping/*.xml 5 #配置实体类别名 6 mybatis.type-aliases-package=com.beilin.entity 7 #开启驼峰命名法 8 mybatis.configuration.map-underscore-to-camel-case=true 9 10 #配置Mysql连接 11 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC 12 spring.datasource.username=root 13 spring.datasource.password=123456 14 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.在Springboot启动类添加@MapperScan注解
1 package com.beilin; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 @MapperScan(value = "com.beilin.mapper") 8 @SpringBootApplication 9 public class SpringbootApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(SpringbootApplication.class, args); 13 } 14 15 }
四、代码实现
1.实体类User
1 package com.beilin.entity; 2 3 public class User { 4 /** 5 * name:user实体 6 */ 7 8 //主键id 9 private int id; 10 //姓名 11 private String name; 12 //年龄 13 private int age; 14 15 // Get和 Set方法 16 public int getId() { 17 return id; 18 } 19 20 public void setId(int id) { 21 this.id = id; 22 } 23 24 public String getName() { 25 return name; 26 } 27 28 public void setName(String name) { 29 this.name = name; 30 } 31 32 public int getAge() { 33 return age; 34 } 35 36 public void setAge(int age) { 37 this.age = age; 38 } 39 } 40 41 User.java
2.数据操作层UserMapper
1 package com.beilin.mapper; 2 3 import com.beilin.entity.User; 4 5 import java.util.List; 6 7 public interface UserMapper { 8 9 //插入 10 public void insert(User user); 11 12 //根据id删除 13 public void delete(Integer id); 14 15 //根据user的id修改 16 public void update(User user); 17 18 //根据id查询 19 public User getById(Integer id); 20 21 //查询全部 22 public List<User> list(); 23 24 }
3.UserMapper映射文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="com.beilin.mapper.UserMapper"> 4 5 <!-- 插入一个user --> 6 <insert id="insert" parameterType="user"> 7 insert into user(name,age) values(#{name},#{age}) 8 </insert> 9 10 <!-- 根据id删除user --> 11 <delete id="delete" parameterType="int"> 12 delete from user where id=#{id} 13 </delete> 14 15 <!-- 根据id修改user信息 --> 16 <update id="update" parameterType="user"> 17 update user set name=#{name},age=#{age} where id=#{id} 18 </update> 19 20 <!-- 根据id查询 --> 21 <select id="getById" parameterType="int" resultType="user"> 22 select * from user where id=#{id} 23 </select> 24 25 <!-- 查询所有 --> 26 <select id="list" parameterType="int" resultType="user"> 27 select * from user 28 </select> 29 30 </mapper>
4.控制层UserController
1 package com.beilin.controller; 2 3 import com.beilin.entity.User; 4 import com.beilin.mapper.UserMapper; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.*; 7 8 import java.util.List; 9 10 @RestController 11 public class UserController { 12 13 @Autowired 14 private UserMapper userMapper; 15 16 //插入user 17 @RequestMapping("/user") 18 public void insert( User user) { 19 userMapper.insert(user); 20 } 21 22 //根据id删除 23 @RequestMapping("/user1/{id}") 24 public void delete(@PathVariable("id") Integer id) { 25 userMapper.delete(id); 26 } 27 //修改 28 @RequestMapping("/user2/{id}") 29 public void update(User user,@PathVariable("id") Integer id) { 30 userMapper.update(user); 31 } 32 33 //根据id查询学生 34 @RequestMapping("/user3/{id}") 35 public User getById(@PathVariable("id") Integer id) { 36 User user = userMapper.getById(id); 37 return user; 38 } 39 40 //查询全部 41 @RequestMapping("/users") 42 public List<User> list(){ 43 List<User> users = userMapper.list(); 44 return users; 45 } 46 47 }
测试使用PostMan或者直接在浏览器测试
过程中所遇到的问题:
报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
解释:就是说,你的Mapper接口,被SpringBoot注入后,却无法正常的使用mapper.xml的sql;
首先检查下自己的代码是否错误,sql语句是否正确,在此基础上对照下面的解决方法。
这里的可能发生的情况有如下几种:
- 接口已经被扫描到,但是代理对象没有找到,即使尝试注入,也是注入一个错误的对象(可能就是null)
- 接口已经被扫描到,代理对象找到了,也注入到接口上了,但是调用某个具体方法时,却无法使用(可能别的方法是正常的)
二者报错的结果是一样的,这里就提供几种排查方法:
1.mapper接口和mapper.xml是否在同一个包(package)下?名字是否一样(仅后缀不同)?
比如,接口名是UserMapper.java;对应的xml就应该是UserMapper.xml
2.mapper.xml的命名空间(namespace)是否跟mapper接口的包名一致?
比如,你接口的包名是com.beilin.mapper,接口名是UserMapper.java,那么你的mapper.xml的namespace应该是com.beilin.mapper.UserMapper
3.接口的方法名,与xml中的一条sql标签的id一致
比如,接口的方法List<User> GetById();那么,对应的xml里面一定有一条是<select id="GetById" parameterType="int" resultType="user">****</select>
4.如果接口中的返回值List集合(不知道其他集合也是),那么xml里面的配置,尽量用resultMap(保证resultMap配置正确),不要用resultType
5.最后,在编译后,到接口所在目录看一看,很有可能是没有生产对应的xml文件,因为maven默认是不编译的,因此,你需要在你的pom.xml的<build></build>里面,加这么一段:
1 <resources> 2 <resource> 3 <directory>src/main/java</directory> 4 <includes> 5 <include>**/*.xml</include> 6 </includes> 7 <filtering>true</filtering> 8 </resource> 9 </resources>