前言:
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。
MyBatis的主要成员
- Configuration MyBatis所有的配置信息都保存在 Configuration对象之中,配置文件中的大部分配置都会存储到该类中
- SqlSession 作为MyBatis工作的主要顶层API,表示和数据库交互时的会话,完成必要数据库增删改查功能
- Executor MyBatis执行器,是MyBatis 调度的核心,负责SQL语句的生成和查询缓存的维护
- StatementHandler 封装了JDBC Statement操作,负责对JDBC statement 的操作,如设置参数等
- ParameterHandler 负责对用户传递的参数转换成JDBC Statement 所对应的数据类型
- ResultSetHandler 负责将JDBC返回的ResultSet结果集对象转换成List类型的集合
- TypeHandler 负责java数据类型和jdbc数据类型(也可以说是数据表列类型)之间的映射和转换
- MappedStatement MappedStatement维护一条<select|update|delete|insert>节点的封装
- SqlSource 负责根据用户传递的parameterObject,动态地生成SQL语句,将信息封装到BoundSql对象中,并返回
- BoundSql 表示动态生成的SQL语句以及相应的参数信息
一、创建web工程
二、引入jar包(数据库使用mysql)
三、在src目录下创建mybatis-config.properties配置文件和db.properties数据库配置文件
db.properties
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
user=root
password=123456
mybatis-config.properties
<?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 resource="db.properties">
</properties>
<typeAliases>
<!-- 给具体的JavaBean设置别名
<typeAlias type="com.hd.pojo.Student" alias="student"/>
-->
<!-- 设置默认的包,查找的时候会在该包下查找,名称不区分大小写!!! -->
<package name="com.wy.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driverClass}"/>
<property name="url" value="${jdbcUrl}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--Mapper.xml的路径-->
<mapper resource="com/wy/pojo/StudentMapper.xml"/>
</mappers>
</configuration>
四、创建javaBean以及相对应的mapper.xml
StudentMapper.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">
<!--namespace 必须唯一,通常是Mapper文件路径-->
<mapper namespace="com.wy.pojo.StudentMapper">
<!-- 查询所有的用户信息
id:自定义 唯一 见名知意
resultType:接收数据的类型
-->
<!--javaBean和数据库字段不一致时-->
<!-- <resultMap type="userbean" id="baseMap">
指定字段和属性的映射关系
<id column="s_id" property="sid"/>
<result column="s_name" property="sname"/>
<result column="s_birth" property="sbirth"/>
<result column="s_sex" property="ssex"/>
</resultMap> -->
<!-- sql块 :将常用的sql部分提取出来,公用 -->
<sql id="baseSql">
s_id,s_name,s_birth,s_sex
</sql>
<!--resultType="Student" 如果mybatis-config.xml中没有指定默认查找的包这里需要改为resultType="com.wy.pojo.Studentcom.wy.pojo.Student" -->
<select id="select" resultType="Student">
select
<include refid="baseSql"></include>
from
student s
</select>
</mapper>
五、测试
@Test
public void selectAll() throws IOException {
// 1.加载配置文件
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
// 2.获取对应的SQLSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 3.获取对应的session对象
SqlSession session = factory.openSession();
List<Student> list = session.selectList("com.wy.pojo.StudentMapper.select");
for (Student student : list) {
System.out.println(student);
}
// 5.关闭session
session.close();
}