您好,登錄后才能下訂單哦!
準備工具:IDEA jdk1.8 Navicat for MySQL Postman
一、新建Project
選擇依賴:mybatis Web Mysql JDBC
項目結構
pom依賴:
<?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.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.beilin</groupId> <artifactId>SpringBoot-Mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBoot-Mybatis</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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> <!--導入mybatis依賴--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <!--導入mysql依賴--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </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> pom.xml
二、創建user表
三、項目配置
1 .在com.beilin下創建controller包、mapper包,entity包;在resources文件夾下創建mapping文件夾(用來存放mapper映射的xml文件)
2.添加properties配置文件:application.properties
#配置mybatis #配置xml映射路徑 mybatis.mapper-locations=classpath:mapping/*.xml #配置實體類別名 mybatis.type-aliases-package=com.beilin.entity #開啟駝峰命名法 mybatis.configuration.map-underscore-to-camel-case=true #配置Mysql連接 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3.在Springboot啟動類添加@MapperScan注解
package com.beilin; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan(value = "com.beilin.mapper") @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } } SpringbootApplication
四、代碼實現
1.實體類User
package com.beilin.entity; public class User { /** * name:學生實體 */ //主鍵id private int id; //姓名 private String name; //年齡 private int age; // Get和 Set方法 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } User.java
2.數據操作層UserMapper
package com.beilin.mapper; import com.beilin.entity.User; import java.util.List; public interface UserMapper { //插入 public void insert(User user); //根據id刪除 public void delete(Integer id); //根據user的id修改 public void update(User user); //根據id查詢 public User getById(Integer id); //查詢全部 public List<User> list(); } UserMapper.java
3.UserMapper映射文件
<?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"> <mapper namespace="com.beilin.mapper.UserMapper"> <!-- 插入一個user --> <insert id="insert" parameterType="user"> insert into user(name,age) values(#{name},#{age}) </insert> <!-- 根據id刪除學生 --> <delete id="delete" parameterType="int"> delete from user where id=#{id} </delete> <!-- 根據id修改學生信息 --> <update id="update" parameterType="user"> update user set name=#{name},age=#{age} where id=#{id} </update> <!-- 根據id查詢 --> <select id="getById" parameterType="int" resultType="user"> select * from user where id=#{id} </select> <!-- 查詢所有 --> <select id="list" parameterType="int" resultType="user"> select * from user </select> </mapper> UserMapper.xml
4.控制層UserController
package com.beilin.controller; import com.beilin.entity.User; import com.beilin.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController public class UserController { @Autowired private UserMapper userMapper; //插入user @RequestMapping("/user") public void insert( User user) { userMapper.insert(user); } //根據id刪除 @RequestMapping("/user1/{id}") public void delete(@PathVariable("id") Integer id) { userMapper.delete(id); } //修改 @RequestMapping("/user2/{id}") public void update(User user,@PathVariable("id") Integer id) { userMapper.update(user); } //根據id查詢學生 @RequestMapping("/user3/{id}") public User getById(@PathVariable("id") Integer id) { User user = userMapper.getById(id); return user; } //查詢全部 @RequestMapping("/users") public List<User> list(){ List<User> users = userMapper.list(); return users; } } 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里面一定有一條是
<selectid="GetById"parameterType="int" resultType="user">****</select>
4.如果接口中的返回值List集合(不知道其他集合也是),那么xml里面的配置,盡量用resultMap(保證resultMap配置正確),不要用resultType
5.最后,在編譯后,到接口所在目錄看一看,很有可能是沒有生產對應的xml文件,因為maven默認是不編譯的,因此,你需要在你的pom.xml的<build></build
>里面,加這么一段:
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> resources
總結
以上所述是小編給大家介紹的SpringBoot整合Mybatis實現CRUD,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。