Mybatis概览
习惯使用hibernate,mybatis互联网常使用,也掌握一次。
Mybatis的spring支持
Mybatis的DAO只写接口,没有实现类。实现类是mapper.xml自动生成的。配置如下:
**
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.tcl.mie.credit.persistence.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>**
Mybatis的Dao个人理解已经不是纯粹的ORM关系了,在DAO里面配置了与业务相关的多表查询结果对象,这样其查询的过程就不是纯的对一个表的操作,常常有多表操作,返回贴近业务的对象结合。
**
<resultMap id="cmsModelMap" type="com.tcl.mie.credit.common.module.CreditGainTaskModel">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="item_code" property="taskId" jdbcType="VARCHAR" />
...
<result column="type" property="type" jdbcType="INTEGER" />
</resultMap>
<resultMap id="cmsVoTempMap" type="com.tcl.mie.credit.common.vo.CreditGainTaskVoTemp">
<id column="id" property="id" jdbcType="INTEGER" />
<result column="item_code" property="taskId" jdbcType="VARCHAR" />
...
<result column="description" property="description" jdbcType="VARCHAR" />
</resultMap>
**
像这样,后两个查询对象已经不是单纯的对单表查询了。因此总结myBatis的粒度是字段,而不是表。hibernate的粒度是表,后者对表进行操作,表被映射为对象。才是真正的ORM。
通过数据库生成mybatis文件
1、使用mysql batch 设计数据库,设计完成后,到处sql脚本。
2、sql脚本导入到mysql中,生成表。
3、通过maven的mybatis-generator-maven-plugin插件,可以反向生成模型相关代码。
4、编写generatorConfig.xml文件,设置表和model映射关系。可以自动生成modle、DAO(interface)、mapper。
**<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config10.dtd" >
<context id="context" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" /><!-- 是否取消注释 -->
<property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://172.26.32.9:3308/databee?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true"
userId="develop" password="Aa123456" />
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator
targetPackage="com.tcl.databee.persistence.model"
targetProject="src/main/java"
/>
<sqlMapGenerator
targetPackage="mappers"
targetProject="src/main/resources"
/>
<javaClientGenerator
targetPackage="com.tcl.databee.persistence.dao"
targetProject="src/main/java"
type="XMLMAPPER"
/>
<!-- 数据库与与对象映射 -->
<table tableName="app" domainObjectName="App" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" />
<table tableName="app_device" domainObjectName="AppDevice" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" />
<table tableName="merchant" domainObjectName="Merchant" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" />
<table tableName="refer" domainObjectName="Refer" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" />
<table tableName="source" domainObjectName="Source" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" />
<table tableName="refer_statistics_day" domainObjectName="ReferStatistics" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" />
**