Mybatis常用代码
以下使用的数据库是Mysql。
Mybatis动态Sql:
Mapper.xml如下:
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。<select id="selectOrderList" resultMap="BaseResultMap"
parameterType="com.model.Order">
select
<include refid="Base_Column_List" />
from t_order
where 1=1
<if test="id != null and id != 0">and id = #{id,jdbcType=INTEGER} </if>
<if test="serialId != null and serialId != '' ">and serialId = #{serialId,jdbcType=VARCHAR} </if>
</select>
如果不想写1=1,也可以直接使用where标签。
where标签知道只有在一个以上的if条件有值的情况下才去插入“WHERE”子句。而且,若最后的内容是“AND”或“OR”开头的,where 标签也知道如何将他们去除。
示例如下:
<select id="selectOrderList" resultMap="BaseResultMap"
parameterType="com.model.Order">
select
<include refid="Base_Column_List" />
from t_order
<where>
<if test="id != null and id != 0"> id = #{id,jdbcType=INTEGER} </if>
<if test="serialId != null and serialId != '' ">and serialId = #{serialId,jdbcType=VARCHAR} </if>
</where>
</select>
对应的Dao层如下:
此处直接将对象作为方法参数,假设参数为Order对象,传递到xml中的参数就包括了Order对象的属性变量,如上的id、serialId。
List<Order> selectOrderList(Order order);
如果仅有一两个变量,也可以直接传递变量,如下:
List<Order> selectOrderList( @Param("id ")Integer id , @Param("serialId ")String serialId );
Mybatis模糊查询:
模糊查询可以使用LIKE关键字和CONCAT()函数。
假设要查询的字段为product,从Dao层传递过来的参数为productName。
示例如下:
WHERE 1=1
<if test="productName!=null and productName!='' "> AND product LIKE CONCAT('%',#{ productName , jdbcType=VARCHAR },'%') </if>
Mybatis使用IN关键字指定条件范围:
IN关键字,需要通过foreach标签来实现。
其中,collection对应的是Dao层传递过来的参数(一般是集合或数组),
item是自己命名的,表示范围中的变量名称。
separator是指分隔符。index是指下标。
示例如下:
<select id="queryOrderUsedCount" resultType="java.lang.String">
SELECT count(*)
FROM t_check_account
WHERE 1=1
<if test=" clientIdList!=null">
AND fclient_id IN
<foreach collection="clientIdList" item="clientId" index="index" open="(" close=")" separator="," >
#{clientId}
</foreach>
</if>
</select>
对应的Dao层为:
String queryOrderListByCondition(@Param("clientIdList")String clientIdList);
Mybatis查询条件范围判断。
经常需要用Mysql查询在某个时间段的数据。
由于Mybatis中可能会将大于号>和小于号<视为标签,所以需要加上 <[CDATA[ ]]>字符。
示例如下:
WHERE 1=1
<if test="beginTime!=null and beginTime!='' "> <![CDATA[ AND t1.fcreate_time >= #{ beginTime , jdbcType=VARCHAR } ]]> </if>
<if test="endTime!=null and endTime!='' "> <![CDATA[ AND t1.fcreate_time < #{ endTime, jdbcType=VARCHAR } ]]> </if>
Mybatis多表查询。
多表查询,分为一对一、一对多、多对多。
简单的Sql语句,一对一可以通过association标签实现,一对多和多对多通过collection标签实现。
详情见: https://www.cnblogs.com/expiator/p/9328338.html
复杂的Sql语句,可以直接设置返回的resultMap为Map,通过Map的键值对解析。
示例如下:
<select id="queryOrderList" resultType="java.util.HashMap">
SELECT
t2.allnum,
t2.username
FROM t_order t1
LEFT JOIN t_order_detail t2 ON t2.orderid = t1.id
WHERE 1=1
<if test="beginTime!=null and beginTime!='' "> <![CDATA[ AND t1.fcreate_time >= #{ beginTime , jdbcType=VARCHAR } ]]> </if>
<if test="endTime!=null and endTime!='' "> <![CDATA[ AND t1.fcreate_time < #{ endTime, jdbcType=VARCHAR } ]]> </if>
<if test="productName!=null and productName!='' "> AND t1.productname LIKE CONCAT('%',#{ productName , jdbcType=VARCHAR },'%')</if>
</select>
对应的Dao层如下:
List<Map<String,Object>> queryOrderList( @Param("productName")String productName ,
@Param("beginDate") String beginDate, @Param("endDate") String endDate );
返回类型为List<Map<String,Object>>,遍历List,获取Map中键对应的值即可。
Controller层如下所示:
//....
//忽略其他无关逻辑
List<Map<String,Object>> orderList=orderService.queryOrderList( productName, beginDate , endDate);
if( orderList.size()>0 ) {
//取第一行的订货信息
Map<String, Object> orderMap=orderList.get(0);
//Object转换为Integer类型
Integer allNum = (Integer) orderMap.get("allnum") ;
//Object转换为String类型
String userName = String.valueOf( orderMap.get("username ") ) ;
}
参考资料:
https://www.cnblogs.com/expiator/p/9328338.html
https://blog.csdn.net/u011781521/article/details/79669180
https://www.cnblogs.com/cyttina/p/3894428.html
