概念:MyBatis是一个优秀的基于java的持久层(dao层)框架,支持自定义SQL,存储过程和高级映射,对原有的JDBC操作进行了封装,几乎消除了所有的JDBC代码,并且可以使用简单的XML来配置执行SQL语句,自动完成ORM操作。
基础使用流程(以普通java项目(且配置maven)为例)
构建Maven项目
1.1 新建项目
![](https://i-blog.csdnimg.cn/blog_migrate/f5fff90b2dd628fc0db1883b90ce5b1c.png)
1.2 选择Maven目录
![](https://i-blog.csdnimg.cn/blog_migrate/ec32b5f85d9ef617616f9ad3dcbf08ee.png)
1.3填写GAV坐标
![](https://i-blog.csdnimg.cn/blog_migrate/1648a89dd50b75f32d8019ea5b7339bf.png)
2.MyBatis环境搭建
2.1 pom.xml中引入MyBatis核心依赖(根据情况选择不同的版本)
<?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>
<!--项目配置-->
<groupId>com.qf</groupId>
<artifactId>hello-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<!--依赖 -->
<dependencies>
<!--MyBatis核心依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--MySql驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</project>
2.2创建连接数据库的 properties文件(注意 文件的位置 在 resources 下)
![](https://i-blog.csdnimg.cn/blog_migrate/6f615b5205a4eb491485015c90288660.png)
![](https://i-blog.csdnimg.cn/blog_migrate/763ab53f56a502181c4b11dc38bc755e.png)
2.3创建MyBatis核心配置文件
![](https://i-blog.csdnimg.cn/blog_migrate/d42aa87dd2c4b322de29f0518400e509.png)
<?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>
<!-- 引入properties 文件 连接数据库的基本参数-->
<properties resource="db.properties"/>
<!-- 引入mybatis自带的日志文件-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 类的别名 写到entity包,则该包下的所有类均可使用别名-->
<typeAliases>
<package name="com.zhigang.entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 3.5步骤完成次部分的配置-->
</mappers>
</configuration>
3 MyBatis开发步骤
3.1创建数据库表
3.2定义实体类(表的列名最好和实体类的属性名一致,方便自动映射)
3.3定义dao接口(不写实现类)
3.4编写Mapper.xml文件
![](https://i-blog.csdnimg.cn/blog_migrate/1779a40a157e9dcce405812e054d4b44.png)
<?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.zhigang.dao.IBookDao">
<select id="getOne" resultType="Book">
select * from book where id = #{id}
</select>
<select id="getByIdAndName" resultType="Book">
select * from book where id = #{id} and name = #{name}
</select>
<select id="getAll" resultType="com.zhigang.entity.Book">
select * from book
</select>
<insert id="add" parameterType="com.zhigang.entity.Book">
<selectKey keyProperty="id" resultType="int" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into book values (#{id},#{name},#{author},#{price})
</insert>
<delete id="delete" >
delete from book where id = #{id}
</delete>
<update id="update" parameterType="com.zhigang.entity.Book">
update book set id=#{id},name=#{name},author=#{author},price=#{price} where id = #{id}
</update>
</mapper>
3.5 将Mapper.xml注册到mybatis-config.xml中
![](https://i-blog.csdnimg.cn/blog_migrate/2595fd3966e45f7f1a314f4af4989dd5.png)
3.6测试 (注意 增 删 改 需要手动提交事务)
package com.qf.mybatis.part1.basic;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
public class HelloMyBatis {
@Test
public void test1() throws IOException {
//1.获得读取MyBatis配置文件的流对象
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//2.构建SqlSession连接对象的工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂获得连接对象
SqlSession sqlSession = factory.openSession();
//4.通过连接对象获得接口实现类对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
//5.调用接口中的方法
System.out.println(userDao.selectUserById(1));
}
}