Maybatis使用总结

Maybatis使用注意的点:

1)字段的自动映射(auto-mapping),根据column名字与property.关联嵌套及重复字段(如id)除外

2)关联查询在 resultMap上添加映射。需在POJO里增加属性对象,如account。
<association property="account"  javaType="Account">
<id property="username" column="username"/>
<result column="money" jdbcType="DECIMAL" property="money" /> 
</association>
3)不能使用 select colum as A.colum2来进来自动关联


4)<cache/>放在Mapping file中,关联清空cash的命名空间:<cache-ref namespace="com.someone.application.data.SomeMapper"/>

!建议使用spring的cache框架,因为mybatis的二级缓存控制粒度校粗;sql mapper一般使用prestatment,不便于设置key。


5)使用mybtis-generator时注意版本一致及mysql的版本及其jdbc驱动的一致性(mysql5.6用6.x的驱动会导致找不到主键的问题)

不建议使用主键自动生成功能,避免序列产生锁表的问题。


以下附上mybtis-generator的配置使用。

<?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-config_1_0.dtd" >  
<generatorConfiguration>  
    <!-- 引入配置文件 -->  
    <properties resource="mybatis/generator.properties"/>  
      
    <!-- 指定数据连接驱动jar地址 -->  
    <classPathEntry location="${classPath}" />  
      
    <!-- 一个数据库一个context -->  
    <context id="infoGuardian" targetRuntime="MyBatis3">  
        <!-- 注释 -->  
        <commentGenerator >  
            <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->  
            <property name="suppressDate" value="false" /> <!-- 是否生成注释代时间戳-->  
        </commentGenerator>  
          
        <!-- jdbc连接 -->  
        <jdbcConnection driverClass="${jdbc_driver}"  
            connectionURL="${jdbc_url}" userId="${jdbc_user}"  
            password="${jdbc_password}" />  
          
        <!-- 类型转换 -->  
        <javaTypeResolver>  
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
          
        <!-- 生成实体类地址 -->    
        <javaModelGenerator targetPackage="com.zihai.entity"  
            targetProject="${project}" >  
            <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] -->  
            <property name="enableSubPackages" value="false"/>  
            <!-- 是否针对string类型的字段在set的时候进行trim调用 -->  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
          
        <!-- 生成mapxml文件 -->  
        <sqlMapGenerator targetPackage="mybatis.mapxml"  
            targetProject="${resource}" >  
            <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] -->  
            <property name="enableSubPackages" value="false" />  
        </sqlMapGenerator>  
          
        <!-- 生成mapxml对应client,也就是接口dao -->      
        <javaClientGenerator targetPackage="com.zihai.dao"  
            targetProject="${project}" type="XMLMAPPER" >  
            <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] -->  
            <property name="enableSubPackages" value="false" />  
        </javaClientGenerator>  
          
        <!-- 配置表信息,这里没生成一张表,这里需要改变一次对应表名 -->    
       
        <table schema="zihai" tableName="areas"  mapperName="AreaDao"
            domainObjectName="Area" 
         enableUpdateByExample="false" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="falses" /> 
    </context>  
</generatorConfiguration>
配置文件

project = C:/Users/Administrator.R1CB3GIDDYDKUPO/git/coder/src/main/java
resource = C:/Users/Administrator.R1CB3GIDDYDKUPO/git/coder/src/main/resources
classPath=D:/mvnRepository/mysql/mysql-connector-java/5.1.40/mysql-connector-java-5.1.40.jar
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://127.0.0.1:3306/zihai?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
jdbc_user=root
jdbc_password=111111

maven :eclipse run 使用命令mybatis-generator:generate -e。-e为打印debug。

<plugin>
	<groupId>org.mybatis.generator</groupId>
	<artifactId>mybatis-generator-maven-plugin</artifactId>
	<version>1.3.5</version>
	<configuration>
		<configurationFile>src/main/resources/mybatis/generatorCfg.xml</configurationFile>
		<verbose>true</verbose>
		<overwrite>true</overwrite>
	</configuration>

		 <dependencies>
		<dependency>
			   <groupId>org.mybatis.generator</groupId>
			    <artifactId>mybatis-generator-core</artifactId>
			    <version>1.3.5</version>
			 </dependency>
		 </dependencies>
</plugin>



猜你喜欢

转载自blog.csdn.net/lylhjh/article/details/53081793
今日推荐