準備工具:IDEA jdk1.8 Navicat for MySQL Postman 一、新建Project 選擇依賴:mybatis Web Mysql JDBC 項目結構 pom依賴: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xml ...
準備工具: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>pom.xml
二、創建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.Driverapplication.properties
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 }SpringbootApplication
四、代碼實現
1.實體類User
1 package com.beilin.entity; 2 3 public class User { 4 /** 5 * name:學生實體 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 }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 }UserMapper.java
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刪除學生 --> 11 <delete id="delete" parameterType="int"> 12 delete from user where id=#{id} 13 </delete> 14 15 <!-- 根據id修改學生信息 --> 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>UserMapper.xml
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 }UserController.java
測試使用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>resources