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文件映射,

扫描二维码关注公众号,回复: 4082759 查看本文章

使用@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的,而不是本文开头说的 不需要。

猜你喜欢

转载自www.cnblogs.com/luo630/p/9962918.html