spring boot +mybatis 整合 连接数据库测试(从0到1)
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文件
- package com.doublebc.common.domain.student;
- Student类
- /**
- * @author DBC
- * @date create 2019/2/28 12:20
- */
- public class Student {
- private Integer id;
- private String name;
- private String sex;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- }
- package com.doublebc.common.student.persistence;
- import com.doublebc.common.domain.student.Student;
- /**
- * @author DBC
- * @date create 2019/2/28 12:14
- */
- public interface StudentMapper {
- void addStudent(Student student);
- }
- package com.doublebc.common.student.repository;
- import com.doublebc.common.student.persistence.StudentMapper;
- import com.doublebc.common.domain.student.Student;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Repository;
- /**
- * @author DBC
- * @date create 2019/2/28 12:15
- */
- @Repository
- public class StudentRepository {
- @Autowired
- private StudentMapper studentMapper;
- public void addStudent(Student student){
- this.studentMapper.addStudent(student);
- }
- }
- package com.doublebc.common;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- @MapperScan("com.doublebc")
- public class CommonApplication {
- public static void main(String[] args) {
- SpringApplication.run(CommonApplication.class, args);
- }
- }
- <?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.doublebc.common.student.persistence.StudentMapper">
- <insert id="addStudent" useGeneratedKeys="true" keyProperty="id">
- insert into T_STUDENT
- (
- name,
- sex
- )values (
- #{name},
- #{sex}
- )
- </insert>
- </mapper>
- 属性文件
- server.port=8019
- spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
- spring.datasource.username=root
- spring.datasource.password=12345
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- <?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 http://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.3.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.doublebc</groupId>
- <artifactId>common</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>common</name>
- <description>common project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <!-- 持久层 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.0.0</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </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>
- <!--编译时增加xml文件-->
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.xml</include>
- </includes>
- <filtering>true</filtering>
- </resource>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- </resource>
- </resources>
- </build>
- </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)的更多相关文章
- Spring boot Mybatis 整合(完整版)
个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...
- Spring boot Mybatis 整合(注解版)
之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦.接口定义和映 ...
- Spring boot Mybatis 整合
PS: 参考博客 PS: spring boot配置mybatis和事务管理 PS: Spring boot Mybatis 整合(完整版) 这篇博客里用到了怎样 生成 mybatis 插件来写程 ...
- Spring boot Mybatis整合构建Rest服务(超细版)
Springboot+ Mybatis+MySql整合构建Rest服务(涵盖增.删.改.查) 1.概要 1.1 为什么要使用Spring boot? 1.1.1 简单方便.配置少.整合了大多数框架 ...
- Spring Boot Mybatis整合
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 特 ...
- spring boot mybatis 整合教程
本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper 分页 ...
- Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结
Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...
- Spring Boot:整合MyBatis框架
综合概述 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单 ...
- 框架篇:Spring+SpringMVC+Mybatis整合开发
前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...
随机推荐
- Python学习之旅(二十八)
Python基础知识(27):常用内建模块(Ⅲ) 1.urlblib urllib提供了一系列用于操作URL的功能 url是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示, ...
- windows系统dos下查看无线网密码
(1)采用命令:netsh wlan show profiles 查看电脑连接过的无线网: (2)采用命令:netsh wlan show profile name ="wifi 名字&qu ...
- day13 十三、迭代器、生成器、枚举对象
def my_generator(): print(1111) yield '结果1' print(2222) yield '结果2' print(3333) yield '结果3' print(44 ...
- SQLSERVER 检查内容
巡检内容: 1系统信息A. 机器名称:B. 硬件配置:Intel(R) CPU E5-2630 2.3GHz(2处理器),24核,16G内存C. 操作系统版本:Windows Server 2008 ...
- Gym 101873I - Uberwatch - [DP]
题目链接:http://codeforces.com/gym/101873/problem/I 题意: 给出 $n(1 \le n \le 300000)$ 个单位时间,每个单位时间给出一个 $x_i ...
- Django’s cache framework
小结: 1.缓存存储位置:数据库.文件系统.内存 2.通过缓存前缀实现跨服务器缓存 Django’s cache framework | Django documentation | Django h ...
- MGF 637: Financial Modeling
MGF 637: Financial ModelingSpring 2019Extra Credit AssignmentInstructions: This is an extra credit o ...
- mysql创建表时符号``的作用
新建表语句如下: CREATE TABLE `course` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(200) NOT NU ...
- nginx安装lua模块实现高并发
nginx安装lua扩展模块 1.下载安装LuaJIT-2.0.4.tar.gz wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz tar ...
- 【LeetCode每天一题】Combinations(组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...