MyBatis简介
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apachesoftware foundation 迁移到了googlecode,并且改名为MyBatis 。2013年11月迁移到Github。
MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动、创建connection、创建statement、手动设置参数、结果集检索等jdbc繁杂的过程代码。
MyBatis通过xml或注解的方式将要执行的各种statement(statement、preparedStatemnt、CallableStatement)配置起来,并通过java对象和statement中的sql进行映射生成最终执行的sql语句,最后由mybatis框架执行sql并将结果映射成java对象并返回。
MyBatis架构
1、mybatis配置SqlMapConfig.xml,此文件作为mybatis的全局配置文件,配置了mybatis的运行环境等信息。mapper.xml文件即sql映射文件,文件中配置了操作数据库的sql语句。此文件需要在SqlMapConfig.xml中加载。
2、通过mybatis环境等配置信息构造SqlSessionFactory即会话工厂
3、由会话工厂创建sqlSession即会话,操作数据库需要通过sqlSession进行。
4、mybatis底层自定义了Executor执行器接口操作数据库,Executor接口有两个实现,一个是基本执行器、一个是缓存执行器。
5、MappedStatement也是mybatis一个底层封装对象,它包装了mybatis配置信息及sql映射信息等。mapper.xml文件中一个sql对应一个Mapped Statement对象,sql的id即是Mapped statement的id。
6、MappedStatement对sql执行输入参数进行定义,包括HashMap、基本类型、pojo,Executor通过Mapped Statement在执行sql前将输入的java对象映射至sql中,输入参数映射就是jdbc编程中对preparedStatement设置参数。
7、MappedStatement对sql执行输出结果进行定义,包括HashMap、基本类型、pojo,Executor通过Mapped Statement在执行sql后将输出结果映射至java对象中,输出结果映射过程相当于jdbc编程中对结果的解析处理过程。
配置文件
SqlMapConfig.xml
<configuration>
<environmentsdefault="development">
<environmentid="development">
<transactionManagertype="JDBC">
<propertyname=""value=""/>
</transactionManager>
<dataSourcetype="UNPOOLED">
<propertyname="driver"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="jdbc:mysql://localhost:3306/micromessage"/>
<propertyname="username"value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapperresource="config/sql/mapper.xml"/>
</mappers>
</configuration>
mapper.xml(java.sql.Types.查看对应数据中的数据类型)
<mappernamespace="Message">
<resultMaptype="bean.Message"id="MessageResult">
<!-- column对应的时执行sql命令后出来的结果集中的列名,非实际表中的 -->
<idcolumn="id"jdbcType="INTEGER"property="id"/>
<resultcolumn="command"jdbcType="VARCHAR"property="command"/>
<resultcolumn="description"jdbcType="VARCHAR"property="description"/>
<resultcolumn="content"jdbcType="VARCHAR"property="content"/>
</resultMap>
<selectid="queryMessageList"parameterType="bean.Message"resultMap="MessageResult">
select * from message where 1=1
<iftest="command!=nulland !"".equals(command.trim())">
andcommand like '%' #{command} '%'
</if>
<iftest="description!=nulland !"".equals(description.trim())">
anddescription like '%' #{description} '%'
</if>
</select>
<deleteid="deleteOne"parameterType="int">
deletefrom message where id=#{_parameter}
</delete>
<deleteid="deleteBatch"parameterType="java.util.List">
deletefrom message where id in (
<foreachcollection="list"item="item"separator=",">
#{item}
</foreach>
)
</delete>
</mapper>
访问数据库类
public class DBAccess {
public SqlSession getSqlSession()throws IOException {
//通过配置文件获取数据库信息
Reader reader = Resources.getResourceAsReader("config/SqlMapConfig.xml");
//通过配置信息构建一个SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//通过SqlSessionFactory打开一个数据库会话
SqlSession sqlSession =sqlSessionFactory.openSession();
return sqlSession;
}
}