Mybatis学习记录1
一、Mybatis简介
1.1 什么是Mybatis

-
MyBatis 是一款优秀的持久层框架
-
它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
-
MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
-
MyBatis原本是apache上的开源项目iBatis,后与于2010年迁移到Google code ,并改名为Mybatis。
-
2013年11月迁移到GitHub上
如何获取Mybatis:
maven:
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
GitHub:Releases · mybatis/mybatis-3 (github.com)
1.2 持久化
数据持久化:
- 持久化就是程序的数据在持久状态和瞬时状态转化的过程
- 内存:断电即失
- 数据库(JDBC),io文件持久化
- 生活:冷藏、水果罐头
为什么需要持久化?
-
有一些对象,不能让他丢掉,如用户金钱流动信息
-
内存太贵
1.3持久层
Dao层,Service层,Controller层...
- 完成持久化工作的代码块
- 层与层之间的界限十分明显
1.4 为什么需要Mybatis?
- 方便
- 传统的JDBC代码太复杂。简化框架
- 帮助程序员将数据存入到数据库中
- 不用Mybatis也可以,学了更容易上手
- 优点:
- 简单易学。
- 灵活。
- sql和代码的分离,提高了可维护性。
- 提供映射标签,支持对象与数据库的orm字段关系映射
- 提供对象关系映射标签,支持对象关系组建维护
- 提供xml标签,支持编写动态sql。
- 使用的人多
2.使用Mybatis
思路:搭建环境->导入Mybatis->编写代码->测试!
2.1 搭建环境
搭建数据库
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
`userName` varchar(255) DEFAULT NULL COMMENT '用户名',
`pwd` varchar(255) DEFAULT NULL COMMENT '密码',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
新建项目
-
建立普通项目
-
导入依赖
![]()
2.2 创建一个模块
-
编写mybatis的配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT&allowMultiQueries=true&relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> </mappers> </configuration> -
编写mybatis的工具类
package utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class MybatisUitl { private static SqlSessionFactory sqlSessionFactory = null; static { try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
2.3 编写代码
-
实体类
package pojo; import lombok.*; @NoArgsConstructor @AllArgsConstructor @Setter @Getter @ToString public class User { private int id; private String userName; private String pwd; } -
接口
package dao; import pojo.User; import java.util.List; public interface UserMapper { List<User> getUserList2(); } -
接口实现类由原来的UserDaoImpl转化为Mapper.xml文件
<?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="dao.UserMapper"> <select id="getUserList2" resultType="pojo.User"> select * from user </select> </mapper>
2.4 测试
-
测试可能出现的问题
注意点1:
org.apache.ibatis.binding.BindingException: Type interface cn.demo.dao.UserDao is not known to the MapperRegistry.
MapperRegistry是什么?
核心注册文件中注册Mappers
注意点2:
Caused by: java.io.IOException: Could not find resource cn/demo/dao/UserMapper.xml
这一类问题是maven的问题,解决方案
<!-- 在build中配置resources,来防止资源导出失败的问题 --> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources>注意点3:
解决错误 Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project qc-offline-report: There are test failures.
今天对maven项目进行打包,遇到了一个问题,解决方案如下:
到pom.xml的plugins标签中添加<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin>

浙公网安备 33010602011771号