MyBatis笔记——基本配置与访问
来源:互联网 发布:linux more 到最后 编辑:程序博客网 时间:2024/06/03 18:14
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;
}
}
- MyBatis笔记——基本配置与访问
- Mybatis基本配置与执行
- MyBatis基本配置与测试
- Hadoop学习笔记—1.基本介绍与环境配置
- Mybatis步步进阶(二)——SQL的基本配置与执行
- Mybatis步步进阶(二)——SQL的基本配置与执行
- mybatis-mybatis与spring整合的基本配置
- MyBatis MapperScannerConfigurer配置——MyBatis学习笔记之八
- MyBatis MapperScannerConfigurer配置——MyBatis学习笔记之一
- 笔记(一)——MyBatis框架的基本应用
- mybatis-mybatis的基本配置
- mybatis 基本配置
- MyBatis-genenator基本配置
- MyBatis的基本配置
- Mybatis-spring基本配置
- [mybatis]基本XML配置
- MyBatis基本配置
- MyBatis基本配置入门
- InnoDB Adaptive Hash Index浅析
- iOS中字典转模型的方法及底层原理
- 关于三维模型网格分块的一点小技巧(不定时更新))
- Linux 搜索命令(locate/find/whereis/which/grep)
- Windows下装memcacahe
- MyBatis笔记——基本配置与访问
- BZOJ 1875 [SDOI2009]HH去散步 (矩阵快速幂)
- pxe
- Android中单选框选中改变图片的示例
- Kafka学习笔记——Kafka简介与使用场景介绍
- angular 以ui-select为例 指令用法小记
- 六大设计原则 (1)
- 归并排序
- ssh 远程执行命令简介