spring boot 整合mybatis

1.打开idea创建一个项目

2.在弹出的窗口中选择spring initializr(初始化项目),点击next

3.接下来填写group 与artifact

(groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。

  groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
  比如我创建一个项目,我一般会将groupId设置为cn.dbc,cn表示域为中国,dbc是我个人姓名缩写,artifactId设置为testProj,表示你这个项目的名称是testProj,依照这个设置,你的包结构最好是cn.pq.testProj打头的,如果有个StudentDeng,它的全路径就是cn.pq.testProj.StudentDeng)

4.接着选择基本依赖项,也就是基础jar包

5.接下来页面继续点击next即可完成创建项目

6.建立基本包类结构进行测试,包括pom文件

  1. package com.doublebc.common.domain.student;
  2. Student
  3. /**
  4. * @author DBC
  5. * @date create 2019/2/28 12:20
  6. */
  7. public class Student {
  8. private Integer id;
  9. private String name;
  10. private String sex;
  11.  
  12. public Integer getId() {
  13. return id;
  14. }
  15.  
  16. public void setId(Integer id) {
  17. this.id = id;
  18. }
  19.  
  20. public String getName() {
  21. return name;
  22. }
  23.  
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27.  
  28. public String getSex() {
  29. return sex;
  30. }
  31.  
  32. public void setSex(String sex) {
  33. this.sex = sex;
  34. }
  35. }
  1. package com.doublebc.common.student.persistence;
  2.  
  3. import com.doublebc.common.domain.student.Student;
  4.  
  5. /**
  6. * @author DBC
  7. * @date create 2019/2/28 12:14
  8. */
  9. public interface StudentMapper {
  10. void addStudent(Student student);
  11. }

  

  1. package com.doublebc.common.student.repository;
  2.  
  3. import com.doublebc.common.student.persistence.StudentMapper;
  4. import com.doublebc.common.domain.student.Student;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Repository;
  7.  
  8. /**
  9. * @author DBC
  10. * @date create 2019/2/28 12:15
  11. */
  12. @Repository
  13. public class StudentRepository {
  14.  
  15. @Autowired
  16. private StudentMapper studentMapper;
  17.  
  18. public void addStudent(Student student){
  19. this.studentMapper.addStudent(student);
  20. }
  21. }

  

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

  

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.doublebc.common.student.persistence.StudentMapper">
  5.  
  6. <insert id="addStudent" useGeneratedKeys="true" keyProperty="id">
  7. insert into T_STUDENT
  8. (
  9. name,
  10. sex
  11. )values (
  12. #{name},
  13. #{sex}
  14. )
  15. </insert>
  16.  
  17. </mapper>

  

  1. 属性文件
  2. server.port=8019
  3.  
  4. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
  5. spring.datasource.username=root
  6. spring.datasource.password=12345
  7. spring.datasource.driver-class-name=com.mysql.jdbc.Driver

  

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.doublebc</groupId>
  12. <artifactId>common</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>common</name>
  15. <description>common project for Spring Boot</description>
  16.  
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20.  
  21. <dependencies>
  22. <!-- 持久层 -->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-data-jpa</artifactId>
  26. </dependency>
  27.  
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.mybatis.spring.boot</groupId>
  34. <artifactId>mybatis-spring-boot-starter</artifactId>
  35. <version>2.0.0</version>
  36. </dependency>
  37.  
  38. <dependency>
  39. <groupId>mysql</groupId>
  40. <artifactId>mysql-connector-java</artifactId>
  41. <scope>runtime</scope>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-test</artifactId>
  46. <scope>test</scope>
  47. </dependency>
  48. </dependencies>
  49.  
  50. <build>
  51. <plugins>
  52. <plugin>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-maven-plugin</artifactId>
  55. </plugin>
  56. </plugins>
  57. <!--编译时增加xml文件-->
  58. <resources>
  59. <resource>
  60. <directory>src/main/java</directory>
  61. <includes>
  62. <include>**/*.xml</include>
  63. </includes>
  64. <filtering>true</filtering>
  65. </resource>
  66. <resource>
  67. <directory>src/main/resources</directory>
  68. <filtering>true</filtering>
  69. </resource>
  70. </resources>
  71. </build>
  72. </project>

  

问题一:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):异常

1:检查xml文件所在的package名称是否和interface对应的package名称一一对应

2:检查xml文件的namespace是否和xml文件的package名称一一对应

3:检查函数名称能否对应上

4:去掉xml文件中的中文注释

5:随意在xml文件中加一个空格或者空行然后保存

如果以上没问题,那么在pom文件中加上红色那一段,然后build重新编译一下项目,看xml文件是否被编译成功,并且检查是否与mapper.class字节码文件在同一包下。

如果在target目录下有xml,但与mapper.class又没在同一目录下,可包名明明是相同的啊?如何解决?

注意:我们在resources目录下建包结构的时候需要一层一层建,或者用“/”隔开,不能用点“.”来分隔多个目录。

所以符合以上规则重新建立一个与Mapper.java同路径的包名,即可正确编译。

问题二:在建包的时候,使用点“.”与“/”符合的区别.

在java包中则不可用/来建包结构,可以用点“.”

例如:文件的相对路径为com/demo/student与com/demo.student的区别

这样应该就没问题啦!

附上结果:

spring boot +mybatis 整合 连接数据库测试(从0到1)的更多相关文章

  1. Spring boot Mybatis 整合(完整版)

    个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...

  2. Spring boot Mybatis 整合(注解版)

    之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦.接口定义和映 ...

  3. Spring boot Mybatis 整合

    PS: 参考博客 PS: spring boot配置mybatis和事务管理 PS: Spring boot Mybatis 整合(完整版)   这篇博客里用到了怎样 生成 mybatis 插件来写程 ...

  4. Spring boot Mybatis整合构建Rest服务(超细版)

     Springboot+ Mybatis+MySql整合构建Rest服务(涵盖增.删.改.查) 1.概要 1.1 为什么要使用Spring  boot? 1.1.1 简单方便.配置少.整合了大多数框架 ...

  5. Spring Boot Mybatis整合

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 特 ...

  6. spring boot mybatis 整合教程

    本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper 分页 ...

  7. Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结

    Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...

  8. Spring Boot:整合MyBatis框架

    综合概述 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单 ...

  9. 框架篇:Spring+SpringMVC+Mybatis整合开发

    前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...

随机推荐

  1. Python学习之旅(二十八)

    Python基础知识(27):常用内建模块(Ⅲ) 1.urlblib urllib提供了一系列用于操作URL的功能 url是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示, ...

  2. windows系统dos下查看无线网密码

    (1)采用命令:netsh wlan show profiles 查看电脑连接过的无线网: (2)采用命令:netsh wlan show profile name ="wifi 名字&qu ...

  3. day13 十三、迭代器、生成器、枚举对象

    def my_generator(): print(1111) yield '结果1' print(2222) yield '结果2' print(3333) yield '结果3' print(44 ...

  4. SQLSERVER 检查内容

    巡检内容: 1系统信息A. 机器名称:B. 硬件配置:Intel(R) CPU E5-2630 2.3GHz(2处理器),24核,16G内存C. 操作系统版本:Windows Server 2008 ...

  5. Gym 101873I - Uberwatch - [DP]

    题目链接:http://codeforces.com/gym/101873/problem/I 题意: 给出 $n(1 \le n \le 300000)$ 个单位时间,每个单位时间给出一个 $x_i ...

  6. Django’s cache framework

    小结: 1.缓存存储位置:数据库.文件系统.内存 2.通过缓存前缀实现跨服务器缓存 Django’s cache framework | Django documentation | Django h ...

  7. MGF 637: Financial Modeling

    MGF 637: Financial ModelingSpring 2019Extra Credit AssignmentInstructions: This is an extra credit o ...

  8. mysql创建表时符号``的作用

    新建表语句如下: CREATE TABLE `course` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(200) NOT NU ...

  9. nginx安装lua模块实现高并发

    nginx安装lua扩展模块 1.下载安装LuaJIT-2.0.4.tar.gz wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz tar ...

  10. 【LeetCode每天一题】Combinations(组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...