Spring boot教程mybatis访问MySQL的尝试
Windows 10家庭中文版,Eclipse,Java 1.8,spring boot 2.1.0,mybatis-spring-boot-starter 1.3.2,com.github.pagehelper 5.1.6
本文记录了昨晚到今早使用spring boot项目集成mybatis访问数据库的过程——主要是其中的坑。
对了,自己的问题还没解决——有了JPA了,为啥还要用mybatis呢?而且JPA集成了hibernate,,其实,自己对mybatis、hibernate都不太熟悉,这周学一遍教程。
参考链接:
springboot---->集成mybatis开发(一)
https://www.cnblogs.com/huhx/p/baseusespringbootmybatis1.html
本文介绍了spring boot集成mybatis需要的依赖包,
使用XML文件映射,
使用@Mapper方式实现映射,
@Select注解的内容也可以放到xml文件中,
Maven依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
application.yml中配置mybatis:
mybatis:
mapper-locations: classpath:mybatis/sql_*.xml
config-location: classpath:setting/mybatis_config.xml
type-aliases-package: com.linux.huhx.learn.mybatis.bean
注意这里的classpath!因为自己不知道其位置,所以,出现了多次错误。其实,开发时的位置就是 src/main/resources:

疑问,classpath: 后是否要有空格?sql_*.xml 中的 * 号代表类名称-开头大写?mapper-locations 以 s 字母结尾,表示可以有多个?怎么配置?type-aliases-package 表示Bean的位置,每个Bean都是java文件,对应java源码中的包名——即上图的src/main/java下的包的名称。
分页:
真分页、逻辑分页,有什么区别?
mybatis实现的是 逻辑分页,需要添加pagehelper包来实现真正的分页,为何?
pagehelper包引入:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper-version}</version>
</dependency>
还需要在mybatis的配置文件——mybatis_config.xml——下添加插件:
<configuration>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
疑问,Bean类不需要写setter/getter,也不需要使用@Data注解;使用org.apache.ibatis.session.SqlSession 来执行查询操作,但依赖于前面mapper中定义的<select>节点;使用PageHelper 来设置分页,然后再使用SqlSession查询。
编写mapper文件时出现了错误:自己最开始 只是简单地把 <select>节点拷贝到mapper文件中,出现了下面的错误。
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [E:\workplace\java\db-test\target\classes\mybatis\sql_People.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 53; 文档根元素 "select" 必须匹配 DOCTYPE 根 "null"。
参考下面的链接解决了问题:
文档根元素 "mapper" 必须匹配 DOCTYPE 根 "null"
https://blog.csdn.net/linlinxie/article/details/79737021
即 添加下面的内容,包含<select>节点 以及其它节点:
<?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.test.mapper.TestMapper">
...
</mapper>
这里的namespace需要根据自己的Bean做跳转,一个Bean对应一个映射文件吧。要是大工程的话,就会存在很多Bean了,写起来可能会比较麻烦。
解决以上问题后仍然存在的问题:

控制台错误:
2018-11-15 08:51:40.875 ERROR 6504 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for people.queryAllPeopleInfo
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for people.queryAllPeopleInfo] with root cause
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for people.queryAllPeopleInfo
解决
将people.queryAllPeopleInfo中的people改为Bean的全名:
com.benzl.mybatis.bean.People
更多错误:
{
"timestamp": "2018-11-15T01:16:52.009+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Type definition error: [simple type, class com.benzl.mybatis.bean.People]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.benzl.mybatis.bean.People and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.github.pagehelper.Page[0])",
"path": "/people3/get"
}
继续完善Bean类,问题得到解决:
-实现了Serializable接口;
-添加了getter/setter方法;

测试成功:

看来,这里的Bean也是需要改造为真正的Bean的,而不是本文开头说的 不需要。
Spring boot教程mybatis访问MySQL的尝试的更多相关文章
- spring boot:使用mybatis访问多个mysql数据源/查看Hikari连接池的统计信息(spring boot 2.3.1)
一,为什么要访问多个mysql数据源? 实际的生产环境中,我们的数据并不会总放在一个数据库, 例如:业务数据库:存放了用户/商品/订单 统计数据库:按年.月.日的针对用户.商品.订单的统计表 因为统计 ...
- Spring Boot 与 Mybatis、Mysql整合使用的例子
第一步: 创建一个SpringBoot的工程,在其中的Maven依赖配置中添加对JDBC.MyBatis.Mysql Driver的依赖具体如下: <!-- JDBC --> <de ...
- spring boot:使mybatis访问多个druid数据源(spring boot 2.3.2)
一,为什么要使用多个数据源? 1,什么情况下需要使用多个数据源? 当我们需要访问不同的数据库时,则需要配置配置多个数据源, 例如:电商的业务数据库(包括用户/商品/订单等) 和统 ...
- Spring boot通过JPA访问MySQL数据库
本文展示如何通过JPA访问MySQL数据库. JPA全称Java Persistence API,即Java持久化API,它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据 ...
- Spring Boot入门(六):使用MyBatis访问MySql数据库(注解方式)
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 本篇博客我们讲解下在Spring Boot中使用MyBatis访问MySql数据库的简单用法. 1.前期 ...
- Spring Boot 教程(2) - Mybatis
Spring Boot 教程 - Mybatis 1. 什么是Mybatis? MyBatis 是一款优秀的持久层框架,它支持自定义 SQL.存储过程以及高级映射.MyBatis 免除了几乎所有的 J ...
- Spring Boot教程(三十七)整合MyBatis
Spring中整合MyBatis就不多说了,最近大量使用Spring Boot,因此整理一下Spring Boot中整合MyBatis的步骤.搜了一下Spring Boot整合MyBatis的文章,方 ...
- Spring Boot 项目学习 (二) MySql + MyBatis 注解 + 分页控件 配置
0 引言 本文主要在Spring Boot 基础项目的基础上,添加 Mysql .MyBatis(注解方式)与 分页控件 的配置,用于协助完成数据库操作. 1 创建数据表 这个过程就暂时省略了. 2 ...
- Spring Boot整合Mybatis并完成CRUD操作
MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...
随机推荐
- 使用 <!DOCTYPE html> 让 <div><img></div>中的图片下面产生几个像素的空白间隔
今天算是第一次使用 <!DOCTYPE html> 不经意间发现图片下方有5个像素左右的空白间隔,检查半天也没查出原因. 最后百度了一下,网上说这是 <!DOCTYPE html&g ...
- jQuery倒计时代码(超简单)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- springboot的起步依赖
加载自动配置的方式2: springboot读取配置文件的方式: 1.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或applicatio ...
- CENTOS 升级Nodejs 到最新版本
1.去官网下载和自己系统匹配的文件: 英文网址:https://nodejs.org/en/download/ 中文网址:http://nodejs.cn/download/ 通过 uname -a ...
- maven的隔离部署
场景:比如说我们一个项目,在开发的时候是一套配置文件,在发布的时候又是一套配置文件.我们每次都要修改配置文件很烦有木有.所以,我们需要maven的这样的一个功能,就是隔离部署.就是说我们写好几套配置文 ...
- java8的新特性详解-----------Lamda表达式
java8最大的亮点就是引入了Lamda表达式 , 函数式编程的概念 具体啥意思我也不知道.只管用就行了,非常的强大,简洁,一个表达式相当于以前的十几行代码 因为之前要实现这种效果全靠if el ...
- JAVA记录-基础常识
1.==与equals区别 1)==用于基本数据类型的比较,判断引用是否指向堆内存的同一地址.---引用地址 2)equals用于判断两个变量是否是对同一对象的引用,即堆中的内容是否相同,返回值为布尔 ...
- 缓存穿透、雪崩、热点与Redis
(拼多多问:Redis雪崩解决办法) 导读:互联网系统中不可避免要大量用到缓存,在缓存的使用过程中,架构师需要注意哪些问题?本文以 Redis 为例,详细探讨了最关键的 3 个问题. 一.缓存穿透预防 ...
- centos 修改文件权限
给脚本添加可执行权限: chmod -R 777 filename.sh
- clip:rect()
写进度条的时候用过这个方法,记录一下 它的用法是 .test{ clip: rect(<top>, <right>, <bottom>, <>left) ...