mybatis 支持注解支持sql.方便与spring集成。
1 映射
1.1文件的映射
注意xml文件一定要规范,xml文件如果在最前面的<?xml 加个空格,就是报错,这种错误极其难找,所以碰到这种操蛋问题可费神,规范极其重要
<mapper resource="com/xx/xxx/xxx/XXXMapper.xml"/>
1.2表,字段的映射
<resultMap type="com.xxx.xxx.mapper.MyEntity" id="MyEntityResult">
<id property="id" column="me_id" javaType="java.lang.Integer"/>
<result property="title" column="me_title" javaType="java.lang.String"/>
</resultMap>
property 对应对象的属性名,
column为列名或者列的别名,javaType或者jdbcType都可以对应,对于基本数据类型,比如MyEntity的Integer id是主键,要用id标签,jdbcType是java.sql.jdbcType下的类型,都是大写的,javaType下如果是基本的数据类型可以直接写java.lang.Integer或者Integer都可以,这里的例子是javaType,项目中用的最多的是jdbcType。type是要写类的全名,id是标示,id标示可以提供给其他用,比如select标签,select语句查询的内容映射的是resultMap的type对应的类属性
1.3 resultMap和resultType映射
resultMap映射到resultMap的唯一标示id,间接的映射到entity对象,比如select里面的resultMap属性,在resultMap标签里面是可以做类型转换的,typeHander=
resultType 映射到一个全定名的java类,可以是一个Map,可能是entity,如果是Map,mybatis会把查询的结果集放到Map集合中,resultType必须保证java属性和查询结果的一致性,而且利用resultType没法利用类型转换
2 传参
mybatis都给提供了参数
<E> List<E> selectList(String statement, Object parameter);
那么xml配置可以通过 #{xxxx} 传递参数,如果是一个entity,xxxx就代表他的属性名
mybatis采用了预编译方法,#{}会被解析成? ${}会被解析成= ,所以对字段排序的时候${}可以用上场
3 ONGL
ONGL 在struts2也有,mybatis也有,mybatis的ONGL 是可以直接用entity属性名的,而且还可以调用java的方法,常见的有
if判断
对于entity if标签可以直接用entity的属性
循环:
<delete id="deleteBatch" parameterType="java.util.List">
delete from MyEntity where id in(
<foreach collection="list" item = "item" separator=",">
#{item}
</foreach>
)
</delete>
<where>
<where>
<if test="title!=null and ! " ".equals(title.trim())"></if>
</where>
<set> 只用于update语句中
<set>
<if test="title!=null and ! " ".equals(title.trim())">title=${title},</if>
</set>
ongl的引用
<trim> 可以替代where或者set
如
<trim prefix="where" prefixOverrides="and/or">
<!-- ...........与where效果一样 截取前缀的and或者or -->
</trim>
<trim prefix="set" suffixOverrides=",">
<!-- ...........与set效果一样 截取前缀的逗号 -->
</trim>
<choose> 当if判断极其复杂的时候 代替if else if语句
<sql>
<include>
4 多对一,一对多
更多mybatis请前往
分享到:
相关推荐
本工程为 MyBatis-Plus 的官方示例,项目结构如下: mybatis-plus-sample-quickstart: 快速开始示例 mybatis-plus-sample-quickstart-springmvc: 快速开始示例(Spring MVC版本) mybatis-plus-sample-reduce-...
mybatis在持久层框架中还是比较火的,一般项目都是基于ssm。虽然mybatis可以直接在xml中...《MyBatis-Plus入门文档》主要介绍了MyBatis-Plus入门使用,以及关于mybatis-plus的更多介绍及特性,感兴趣的可以下载学习一下
mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip ...
赠送jar包:mybatis-plus-boot-starter-3.5.1.jar; 赠送原API文档:mybatis-plus-boot-starter-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-boot-starter-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-...
赠送jar包:mybatis-spring-2.0.0.jar; 赠送原API文档:mybatis-spring-2.0.0-javadoc.jar; 赠送源代码:mybatis-spring-2.0.0-sources.jar; 赠送Maven依赖信息文件:mybatis-spring-2.0.0.pom; 包含翻译后的API...
赠送jar包:mybatis-plus-extension-3.5.1.jar; 赠送原API文档:mybatis-plus-extension-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-extension-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
赠送jar包:mybatis-plus-annotation-3.5.1.jar; 赠送原API文档:mybatis-plus-annotation-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
修改 mybatis-generate 源码实现实体类加上字段注释后的 mybatis-generator-core 包。 今天用到了mybatis的逆向工程,生成实体类和mapper文件,无奈数据库字段太多,逆向工程生成的字段都没有注释,每次需要了解字段...
赠送jar包:mybatis-plus-core-3.1.0.jar; 赠送原API文档:mybatis-plus-core-3.1.0-javadoc.jar; 赠送源代码:mybatis-plus-core-3.1.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-core-3.1.0.pom; ...
赠送jar包:mybatis-spring-boot-autoconfigure-1.3.2.jar; 赠送原API文档:mybatis-spring-boot-autoconfigure-1.3.2-javadoc.jar; 赠送源代码:mybatis-spring-boot-autoconfigure-1.3.2-sources.jar; 赠送...
赠送jar包:mybatis-plus-boot-starter-3.1.0.jar; 赠送原API文档:mybatis-plus-boot-starter-3.1.0-javadoc.jar; 赠送源代码:mybatis-plus-boot-starter-3.1.0-sources.jar; 赠送Maven依赖信息文件:mybatis-...
赠送jar包:mybatis-generator-core-1.3.7.jar; 赠送原API文档:mybatis-generator-core-1.3.7-javadoc.jar; 赠送源代码:mybatis-generator-core-1.3.7-sources.jar; 赠送Maven依赖信息文件:mybatis-generator-...
mybatis-plus源码(mybatis-plus-3.5.1.zip)
mybatis-generator-config_1_0.dtd文件存在于mybatis-generator-core-1.3.2.jar包中,路径如下org/mybatis/generator/config/xml/mybatis-generator-config_1_0.dtd 可以设置开发工具的dtd配置,配置...
赠送jar包:mybatis-plus-boot-starter-3.5.1.jar; 赠送原API文档:mybatis-plus-boot-starter-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-boot-starter-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-...
mybatis配置的重要文件包括mybatis-3-config.dtd mybatis-3-mapper.dtd
赠送jar包:mybatis-paginator-1.2.15.jar; 赠送原API文档:mybatis-paginator-1.2.15-javadoc.jar; 赠送源代码:mybatis-paginator-1.2.15-sources.jar; 赠送Maven依赖信息文件:mybatis-paginator-1.2.15.pom;...