经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Windows » 查看文章
SpringBoot整合Mybatis(CRUD的实现)
来源:cnblogs  作者:北城以南  时间:2019/9/10 10:47:40  对本文有异议

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

 

一、新建Project

选择依赖:mybatis  Web  Mysql  JDBC

项目结构

pom依赖:

  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. 4 <modelVersion>4.0.0</modelVersion>
  5. 5 <parent>
  6. 6 <groupId>org.springframework.boot</groupId>
  7. 7 <artifactId>spring-boot-starter-parent</artifactId>
  8. 8 <version>2.1.7.RELEASE</version>
  9. 9 <relativePath/> <!-- lookup parent from repository -->
  10. 10 </parent>
  11. 11 <groupId>com.beilin</groupId>
  12. 12 <artifactId>SpringBoot-Mybatis</artifactId>
  13. 13 <version>0.0.1-SNAPSHOT</version>
  14. 14 <name>SpringBoot-Mybatis</name>
  15. 15 <description>Demo project for Spring Boot</description>
  16. 16
  17. 17 <properties>
  18. 18 <java.version>1.8</java.version>
  19. 19 </properties>
  20. 20 <dependencies>
  21. 21 <dependency>
  22. 22 <groupId>org.springframework.boot</groupId>
  23. 23 <artifactId>spring-boot-starter-jdbc</artifactId>
  24. 24 </dependency>
  25. 25 <dependency>
  26. 26 <groupId>org.springframework.boot</groupId>
  27. 27 <artifactId>spring-boot-starter-web</artifactId>
  28. 28 </dependency>
  29. 29
  30. 30 <!--导入mybatis依赖-->
  31. 31 <dependency>
  32. 32 <groupId>org.mybatis.spring.boot</groupId>
  33. 33 <artifactId>mybatis-spring-boot-starter</artifactId>
  34. 34 <version>2.1.0</version>
  35. 35 </dependency>
  36. 36
  37. 37 <!--导入mysql依赖-->
  38. 38 <dependency>
  39. 39 <groupId>mysql</groupId>
  40. 40 <artifactId>mysql-connector-java</artifactId>
  41. 41 <version>5.1.47</version>
  42. 42 </dependency>
  43. 43
  44. 44 <dependency>
  45. 45 <groupId>org.springframework.boot</groupId>
  46. 46 <artifactId>spring-boot-starter-test</artifactId>
  47. 47 <scope>test</scope>
  48. 48 </dependency>
  49. 49 </dependencies>
  50. 50
  51. 51 <build>
  52. 52 <plugins>
  53. 53 <plugin>
  54. 54 <groupId>org.springframework.boot</groupId>
  55. 55 <artifactId>spring-boot-maven-plugin</artifactId>
  56. 56 </plugin>
  57. 57 </plugins>
  58. 58 </build>
  59. 59
  60. 60 </project>
pom.xml

二、创建user表

 

 

 、项目配置

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

 

 

 2 .添加properties配置文件:application.properties

  1. 1 #配置mybatis
  2. 2
  3. 3 #配置xml映射路径
  4. 4 mybatis.mapper-locations=classpath:mapping/*.xml
  5. 5 #配置实体类别名
  6. 6 mybatis.type-aliases-package=com.beilin.entity
  7. 7 #开启驼峰命名法
  8. 8 mybatis.configuration.map-underscore-to-camel-case=true
  9. 9
  10. 10 #配置Mysql连接
  11. 11 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
  12. 12 spring.datasource.username=root
  13. 13 spring.datasource.password=123456
  14. 14 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
application.properties

3.在Springboot启动类添加@MapperScan注解

  1. 1 package com.beilin;
  2. 2
  3. 3 import org.mybatis.spring.annotation.MapperScan;
  4. 4 import org.springframework.boot.SpringApplication;
  5. 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. 6
  7. 7 @MapperScan(value = "com.beilin.mapper")
  8. 8 @SpringBootApplication
  9. 9 public class SpringbootApplication {
  10. 10
  11. 11 public static void main(String[] args) {
  12. 12 SpringApplication.run(SpringbootApplication.class, args);
  13. 13 }
  14. 14
  15. 15 }
SpringbootApplication

四、代码实现

1.实体类User

  1. 1 package com.beilin.entity;
  2. 2
  3. 3 public class User {
  4. 4 /**
  5. 5 * name:学生实体
  6. 6 */
  7. 7
  8. 8 //主键id
  9. 9 private int id;
  10. 10 //姓名
  11. 11 private String name;
  12. 12 //年龄
  13. 13 private int age;
  14. 14
  15. 15 // Get和 Set方法
  16. 16 public int getId() {
  17. 17 return id;
  18. 18 }
  19. 19
  20. 20 public void setId(int id) {
  21. 21 this.id = id;
  22. 22 }
  23. 23
  24. 24 public String getName() {
  25. 25 return name;
  26. 26 }
  27. 27
  28. 28 public void setName(String name) {
  29. 29 this.name = name;
  30. 30 }
  31. 31
  32. 32 public int getAge() {
  33. 33 return age;
  34. 34 }
  35. 35
  36. 36 public void setAge(int age) {
  37. 37 this.age = age;
  38. 38 }
  39. 39 }
User.java

2.数据操作层UserMapper

 

  1. 1 package com.beilin.mapper;
  2. 2
  3. 3 import com.beilin.entity.User;
  4. 4
  5. 5 import java.util.List;
  6. 6
  7. 7 public interface UserMapper {
  8. 8
  9. 9 //插入
  10. 10 public void insert(User user);
  11. 11
  12. 12 //根据id删除
  13. 13 public void delete(Integer id);
  14. 14
  15. 15 //根据user的id修改
  16. 16 public void update(User user);
  17. 17
  18. 18 //根据id查询
  19. 19 public User getById(Integer id);
  20. 20
  21. 21 //查询全部
  22. 22 public List<User> list();
  23. 23
  24. 24 }
UserMapper.java

 

3.UserMapper映射文件

  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. 3 <mapper namespace="com.beilin.mapper.UserMapper">
  4. 4
  5. 5 <!-- 插入一个user -->
  6. 6 <insert id="insert" parameterType="user">
  7. 7 insert into user(name,age) values(#{name},#{age})
  8. 8 </insert>
  9. 9
  10. 10 <!-- 根据id删除学生 -->
  11. 11 <delete id="delete" parameterType="int">
  12. 12 delete from user where id=#{id}
  13. 13 </delete>
  14. 14
  15. 15 <!-- 根据id修改学生信息 -->
  16. 16 <update id="update" parameterType="user">
  17. 17 update user set name=#{name},age=#{age} where id=#{id}
  18. 18 </update>
  19. 19
  20. 20 <!-- 根据id查询 -->
  21. 21 <select id="getById" parameterType="int" resultType="user">
  22. 22 select * from user where id=#{id}
  23. 23 </select>
  24. 24
  25. 25 <!-- 查询所有 -->
  26. 26 <select id="list" parameterType="int" resultType="user">
  27. 27 select * from user
  28. 28 </select>
  29. 29
  30. 30 </mapper>
UserMapper.xml

4.控制层UserController

  1. 1 package com.beilin.controller;
  2. 2
  3. 3 import com.beilin.entity.User;
  4. 4 import com.beilin.mapper.UserMapper;
  5. 5 import org.springframework.beans.factory.annotation.Autowired;
  6. 6 import org.springframework.web.bind.annotation.*;
  7. 7
  8. 8 import java.util.List;
  9. 9
  10. 10 @RestController
  11. 11 public class UserController {
  12. 12
  13. 13 @Autowired
  14. 14 private UserMapper userMapper;
  15. 15
  16. 16 //插入user
  17. 17 @RequestMapping("/user")
  18. 18 public void insert( User user) {
  19. 19 userMapper.insert(user);
  20. 20 }
  21. 21
  22. 22 //根据id删除
  23. 23 @RequestMapping("/user1/{id}")
  24. 24 public void delete(@PathVariable("id") Integer id) {
  25. 25 userMapper.delete(id);
  26. 26 }
  27. 27 //修改
  28. 28 @RequestMapping("/user2/{id}")
  29. 29 public void update(User user,@PathVariable("id") Integer id) {
  30. 30 userMapper.update(user);
  31. 31 }
  32. 32
  33. 33 //根据id查询学生
  34. 34 @RequestMapping("/user3/{id}")
  35. 35 public User getById(@PathVariable("id") Integer id) {
  36. 36 User user = userMapper.getById(id);
  37. 37 return user;
  38. 38 }
  39. 39
  40. 40 //查询全部
  41. 41 @RequestMapping("/users")
  42. 42 public List<User> list(){
  43. 43 List<User> users = userMapper.list();
  44. 44 return users;
  45. 45 }
  46. 46
  47. 47 }
UserController.java

 

测试使用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. 1 <resources>
  2. 2 <resource>
  3. 3 <directory>src/main/java</directory>
  4. 4 <includes>
  5. 5 <include>**/*.xml</include>
  6. 6 </includes>
  7. 7 <filtering>true</filtering>
  8. 8 </resource>
  9. 9 </resources>
resources

 

 

 

 

 

 

 

 

原文链接:http://www.cnblogs.com/wx60079/p/11461158.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号