Mybatis总结一——日期比较,参数传递,Mapper文件的编写
1 参数传递
(1)如果不想把参数放在一个对象内封装,使用多个参数。
Dao接口层方法传参:
Integer selectAnnualSales(@Param("tenantId")String tenantId,@Param("beginDate") String beginDate,@Param("endDate")String endDate,@Param("shopCode")String shopCode,@Param("type")Integer type);
对应的Mapper.xml文件代码
<select id="selectAnnualSales" resultType="java.lang.Integer">
select sum(payment) from t_third_common_consumption
<where>
and date(create_time) BETWEEN date(#{beginDate}) and date(#{endDate})
and shop_code=#{shopCode}
and tenant_id=#{tenantId}
<choose>
<when test="type!=null">
and type = #{type} and payment >= 0
</when>
<otherwise>
and ( type = 0 or type = 1 )
</otherwise>
</choose>
</where>
</select>
(1) 其中,注解@Param作用使得mapper中可以直接用#{参数名}取值,如果dao层方法没有该注解,则mapper层不能用#{参数名}取值,只能用0 ,1 等索引下标对应方法中传来的参数,如#{0}。
(2)<where>元素会自动根据条件的语句数,删除无用的and。如本例会自动删除第一个and,之后的and会保留。
(3)<choose>
<when></when>
<otherwise></otherwise>
</choose>
相当与if/else。
(4)<select> 元素的属性 id 要和dao层方法名一致,且唯一。
2:日期使用
数据库中的日期格式为:yyyy-MM-dd,字段类型为date
后台传递的参数也应该符合该格式(非类型)
例如: Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat ("yyyy-MM-dd");
String sDate=format.format(date); //将date转换为该格式的字符串
在mapper文件的sql语句中,比较日期的大小,要将字符串转换为date(数据库字段对应的类型,格式)类型,用date()函数。
例如 :select * from table1 where date(create_time) between date(sDate) and date(endDate);
date()会将该字符串转换为该字符串格式的日期类型。注意如果传入的日期为:yyyy-MM-dd HH:mm:ss .使用date()后会自动变成 yyyy-MM-dd . 如 2019-02-01 23:32:32 会变成 2019-02-01
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- mybatis mapper把传递来的参数当作字段写入结果集中
- mybatis 参数传递总结
- mybatis操作数据库是如果mapper文件中的id和java代码中的参数名字不同是报的错
- mybatis传递参数总结
- Mybatis框架中Mapper文件传值参数获取
- mybatis学习日志(1) : mapper.xml传递两个及多个参数
- 关于Mybatis中Mapper.xml文件传多个参数和like问题
- MyBatis Mapper 传递多个参数
- myBatis,mapper查询方法参数传递出错
- MyBatis映射文件中多参数传递的问题
- JavaEE_Mybatis_SpringMVC_自定义的参数绑定,页面的日期类型传递到数据库中datetime
- Mybatis学习总结(四).Mapper映射文件
- Mybatis框架中Mapper文件传值参数获取。【Mybatis】
- 01_MyBatis EHCache集成及所需jar包,ehcache.xml配置文件参数配置及mapper中的参数配置
- ASP.NET中网页间传递参数用什么方法比较与总结
- 01_MyBatis EHCache集成及所需jar包,ehcache.xml配置文件参数配置及mapper中的参数配置
- oracle数据库,使用Mybatis生成的mapper.xml文件查询日期型参数时,时分秒丢失的问题
- MyBatis映射文件中用#和$传递参数的特点
- Mybatis中传递多个参数的4种方法总结