Mybatis开发流程
简介
Mybatis是一个ORM类型框架,解决的数据库访问和操作的问题,对现有JDBC技术的封装。
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。(官方介绍)
开发步骤
1. 坐标依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
2.配置文件
mybatis-config.xml
<?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>
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
<!--别名,别名也推荐使用大写开头-->
<typeAliases>
<typeAlias type="edu.hzb.entity.User" alias="User"/>
</typeAliases>
<!-- 数据库环境,可配置多个,通过default属性指定数据库环境-->
<environments default="default">
<environment id="default">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</dataSource>
</environment>
<!-- <environment id="oracle">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
<property name="username" value="hr"/>
<property name="password" value="hr"/>
</dataSource>
</environment>-->
</environments>
<!--注册mapper文件-->
<mappers>
<!--<package name=""-->
<mapper resource="UserDAOMapper.xml"/>
</mappers>
</configuration>
3.entity实体
package edu.hzb.entity;
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}
4.数据库表
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
5.DAO接口
public interface UserDAO {
List<User> selectList();
}
6.Mapper文件
<?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="edu.hzb.dao.UserDAO">
<resultMap id="BaseResultMap" type="edu.hzb.entity.User">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
<sql id="BaseColumn">
id, name, age
</sql>
<select id="selectList" resultMap="BaseResultMap">
select <include refid="BaseColumn"></include>
from t_user
</select>
</mapper>
7.测试
public class TestMapper {
@Test
public void test1() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//Mybatis会通过代理设计模式会为UserDAO接口创建代理实现类
UserDAO userDAO = sqlSession.getMapper(UserDAO.class);
List<User> users = userDAO.selectList();
for (User user : users) {
System.out.println("user = " + user);
}
}
@Test
public void test2() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
/*
Mybatis底层就是通过这种方式调用,test1中的调用方式只不过是在此基础上通过动态代理进行封装,
所以显得test中的调用方式更好理解
*/
//通过mapper文件中的namespace + sql标签的id值就可以确定执行哪一个sql
List<User> users = sqlSession.selectList("edu.hzb.dao.UserDAO.selectList");
for (User user : users) {
System.out.println("user = " + user);
}
}
}