Maybatis使用总结
来源:互联网 发布:美国大学不要面试知乎 编辑:程序博客网 时间:2024/06/10 11:18
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/javaresource = C:/Users/Administrator.R1CB3GIDDYDKUPO/git/coder/src/main/resourcesclassPath=D:/mvnRepository/mysql/mysql-connector-java/5.1.40/mysql-connector-java-5.1.40.jarjdbc_driver=com.mysql.jdbc.Driverjdbc_url=jdbc:mysql://127.0.0.1:3306/zihai?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8jdbc_user=rootjdbc_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>
- Maybatis使用总结
- maybatis中三种模糊查询
- springMvc+spring4+maybatis简单例子
- 关于maybatis中resultType和resultMap
- maybatis MapperMethod(接口类型方法参数转换)
- 使用总结
- 【eXtremeComponents总结系列】使用总结
- 【eXtremeComponents总结系列】使用总结
- AFNetworking使用总结 与使用
- Notification使用总结(基本使用)
- Tiles使用心得,使用总结
- WSE使用总结
- MASM宏使用总结
- 水晶报表使用总结
- struts+hibernate使用总结
- MASM宏使用总结
- 转载 内存使用总结
- MySQL使用总结(1)
- 查找文件的类 struct _finddata_t结构体用法
- Unity基础包 FirstPersonController下的CurveControlledBob和LerpContolledBob 脚本研究
- JSON详解
- sql之left join、right join、inner join的区别
- Spring与MX4J集成实现JMX管理
- Maybatis使用总结
- Redis data淘汰机制
- PAT_乙级 1007 素数猜想对
- MFC深度剖析消息反射机制
- 【图文信息】C#群发微信图文信息(Senparc.Weixin.MP.dll)
- php需要用到哪些工具
- copy mutableCopy
- 当启动tomcat时出现tomcat setting should be set in tomcat preference page
- 关于哈希表