欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

SpringBoot整合Mybatis(CRUD的实现)

程序员文章站 2022-05-03 15:09:10
准备工具:IDEA jdk1.8 Navicat for MySQL Postman 一、新建Project 选择依赖:mybatis Web Mysql JDBC 项目结构 pom依赖: 1 2

准备工具:idea   jdk1.8  navicat for mysql  postman

 

一、新建project

选择依赖:mybatis  web  mysql  jdbc

SpringBoot整合Mybatis(CRUD的实现)

项目结构

SpringBoot整合Mybatis(CRUD的实现)

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表

SpringBoot整合Mybatis(CRUD的实现)

 

 

 、项目配置

1 .在com.beilin下创建controller包、mapper包,entity包;在resources文件夹下创建mapping文件夹(用来存放mapper映射的xml文件)

SpringBoot整合Mybatis(CRUD的实现)

 

 

 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:学生实体
 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 }

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删除学生 -->
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>

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语句是否正确,在此基础上对照下面的解决方法。

这里的可能发生的情况有如下几种:

  1. 接口已经被扫描到,但是代理对象没有找到,即使尝试注入,也是注入一个错误的对象(可能就是null)
  2. 接口已经被扫描到,代理对象找到了,也注入到接口上了,但是调用某个具体方法时,却无法使用(可能别的方法是正常的)

二者报错的结果是一样的,这里就提供几种排查方法:

   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="getbyidparametertype="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>