网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringBoot整合Mybatis实现CRUD

准备工具: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"?>

  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.7.RELEASE
     
  
  com.beilin
  SpringBoot-Mybatis
  0.0.1-SNAPSHOT
  SpringBoot-Mybatis
  Demo project for Spring Boot

  
    1.8
  
  
    
      org.springframework.boot
      spring-boot-starter-jdbc
    
    
      org.springframework.boot
      spring-boot-starter-web
    

    
    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      2.1.0
    

    
    
      mysql
      mysql-connector-java
      5.1.47
    

    
      org.springframework.boot
      spring-boot-starter-test
      test
    
  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  


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 list();
}
UserMapper.java

3.UserMapper映射文件

<?xml version="1.0" encoding="UTF-8"?>


  
  
  insert into user(name,age) values(#{name},#{age})
  
  
  
  delete from user where id=#{id}
  
  
  
  update user set name=#{name},age=#{age} where id=#{id}
  
  
  
  
  

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 list(){
    List 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 GetById();那么,对应的xml里面一定有一条是

****

4.如果接口中的返回值List集合(不知道其他集合也是),那么xml里面的配置,尽量用resultMap(保证resultMap配置正确),不要用resultType

5.最后,在编译后,到接口所在目录看一看,很有可能是没有生产对应的xml文件,因为maven默认是不编译的,因此,你需要在你的pom.xml的>里面,加这么一段:


      
        src/main/java
        
          **/*.xml
        
        true
      
 
resources

总结

以上所述是小编给大家介绍的SpringBoot整合Mybatis实现CRUD,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!


当前名称:SpringBoot整合Mybatis实现CRUD
文章位置:http://bjjierui.cn/article/jieogi.html