SpringBoot整合Mybatis实现CRUD

准备工具: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,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

以上是 SpringBoot整合Mybatis实现CRUD 的全部内容, 来源链接: utcz.com/z/323620.html

回到顶部