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

SpringBoot整合Mybatis实现CRUD

程序员文章站 2022-08-03 20:32:19
准备工具:idea  jdk1.8  navicat for mysql  postman 一、新建project 选择依赖:mybatis web mysql jdb...

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

一、新建project

选择依赖:mybatis web mysql jdbc

SpringBoot整合Mybatis实现CRUD

项目结构

SpringBoot整合Mybatis实现CRUD

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表

SpringBoot整合Mybatis实现CRUD

、项目配置

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

SpringBoot整合Mybatis实现CRUD

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,希望对大家有所帮助