SpringBoot整合Mybatis实现CRUD

网友投稿 368 2022-12-28


SpringBoot整合Mybatis实现CRUD

准备工具:IDEA  jdk1.8  Navicat for mysql  Postman

一、新建Project

选择依赖:mybatis Web Mysql JDBC

项目结构

pom依赖:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

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表

三、项目配置

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 list();

}

UserMapper.java

3.UserMapper映射文件

insert into user(name,age) values(#{name},#{age})

delete from user where id=#{id}

update user set name=#{name},age=#{age} where id=#{id}

select * from user where id=#{id}

select * from user

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

总结


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微服务接口测试工具(服务端接口测试)
下一篇:关于宜信sia微服务网关的信息
相关文章

 发表评论

暂时没有评论,来抢沙发吧~