Java Mybatis Xml写法 查询语句的动态设计总结
生活随笔
收集整理的這篇文章主要介紹了
Java Mybatis Xml写法 查询语句的动态设计总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
太原工作 2019-6.25?
文章總結
主要Xml中? Page分頁對象中? 定義一個Object 類型的屬性,后臺Dao中接受的時候 參數是Page分頁對象,?
這時可以根據Page中的condition對象 是否被定義 去判斷 查詢數據庫時是否在? where后面動態的添加判斷語句
?
?
在controller Page分頁對象
public class Pager { /*中間的變量就不寫了 不是重點*/private Object condition;}Controller調用? ?實例化Page對象 把對象發給到Page的Condition字段中
Pager pagerMember = new Pager(); pagerMember.setPageSize(2); BookContactMember bookContactMember = new BookContactMember(); bookContactMember.setName(""); bookContactMember.setTelphone(""); bookContactMember.setContactId(bookContact.getId().toString()); pagerMember.setCondition(bookContactMember);Dao 中
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.test.service.module.bookContact.dao.mapper.BookContactMapper"> <resultMap id="BaseResultMap" type="com.leimingtech.core.entity.base.BookContact"><result column="id" property="id" jdbcType="INTEGER" /><result column="user_id" property="userId" jdbcType="VARCHAR" /><result column="contact_title" property="contactTitle" jdbcType="VARCHAR" /><result column="contact_describe" property="contactDescribe" jdbcType="VARCHAR" /><result column="contact_img" property="contactImg" jdbcType="VARCHAR" /><result column="trade_type" property="tradeType" jdbcType="VARCHAR" /><result column="contact_type" property="contactType" jdbcType="VARCHAR" /><result column="phone_is_read" property="phoneIsRead" jdbcType="VARCHAR" /><result column="card_change" property="cardChange" jdbcType="VARCHAR" /><result column="in_must_item" property="inMustItem" jdbcType="VARCHAR" /><result column="in_check" property="inCheck" jdbcType="VARCHAR" /><result column="is_check" property="isCheck" jdbcType="VARCHAR" /><result column="check_status" property="checkStatus" jdbcType="VARCHAR" /><result column="check_remark" property="checkRemark" jdbcType="VARCHAR" /><result column="sort_type" property="sortType" jdbcType="VARCHAR" /><result column="read_num" property="readNum" jdbcType="INTEGER" /><result column="create_time" property="createTime" jdbcType="TIMESTAMP" /><result column="is_del" property="isDel" jdbcType="VARCHAR" /><result column="del_remark" property="delRemark" jdbcType="VARCHAR" /></resultMap><sql id="whereStatement"><where>1 = 1<if test="condition !=null"><if test="condition.contactTitle != null and condition.contactTitle != ''">AND contact_title LIKE <if test="dbName == 'oracle'">'%'||#{condition.contactTitle}||'%'</if><if test="dbName == 'mssql'">'%'+#{condition.contactTitle}+'%'</if><if test="dbName == 'mysql'">concat('%',#{condition.contactTitle},'%')</if></if><if test="condition.contactType != null and condition.contactType != ''">AND contact_type = #{condition.contactType}</if><if test="condition.tradeType != null and condition.tradeType != ''">AND trade_type = #{condition.tradeType}</if><if test="condition.inCheck != null and condition.inCheck != ''">AND in_check = #{condition.inCheck}</if><if test="condition.isCheck != null and condition.isCheck != ''">AND is_check = #{condition.isCheck}</if><if test="condition.checkStatus != null and condition.checkStatus != ''">AND check_status = #{condition.checkStatus}</if><if test="condition.isDel != null and condition.isDel != ''">AND is_del = #{condition.isDel}</if></if></where></sql><select id="findBookContactPagerList" resultMap="BaseResultMap"parameterType="com.leimingtech.service.utils.page.Pager">select * from book_contact<include refid="whereStatement" /><if test="orderBy == 0">order by id desc</if><if test="orderBy == 1">order by id asc</if></select> </mapper>?
總結
以上是生活随笔為你收集整理的Java Mybatis Xml写法 查询语句的动态设计总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Jquery 点击当前的标签对象获取值
- 下一篇: Mybatis Xml 传入动态字段 排