How to use the Sqlmap of splicing SQL statements

Source: Internet
Author: User
Tags cdata

1. Why use?

The main is to get the value in the code, and then bring into the SQL statement splicing query

2. How do I use it?

1) The bean inherits from the BaseEntity class, which has

    /*** Custom SQL (SQL ID, SQL content)*/    protectedMap<string, string>Sqlmap; @JsonIgnore @XmlTransient PublicMap<string, string>Getsqlmap () {if(Sqlmap = =NULL) {Sqlmap=Maps.newhashmap (); }        returnSqlmap; }      Public voidSetsqlmap (map<string, string>Sqlmap) {         This. Sqlmap =Sqlmap; }

2) How to write in XML?

<SelectID= "Findlist"Resulttype= "Zlfbbean">SELECT * FROM (select<includerefID= "Zlfbbeancolumns"/>From zl_hczz i<includerefID= "Zlfbbeanjoins"/>            <where>and I.del_flag = #{del_flag_normal} ${SQLMAP.DSF}//Add the restrictions in service here<ifTest= "ZLBH ! = null and ZLBH! =" ">and i.zlbh = #{zlbh}</if>            <ifTest= "CreateBy! = null and createby.id! = null and Createby.id! =" ">and i.create_by = #{createby.id}</if>            <offTest= "Office! = null and office.id! = null and Office.id! =" ">and i.office_id = #{office.id}</if>            </where>ORDER by i.create_date desc,i.update_date DESC) T<where>and T.del_flag = #{del_flag_normal}<ifTest= "Beginindate ! = null and Beginindate! =" ">and t.create_date<! [Cdata[>= #{beginindate}]]>            </if>            <offTest= "Endindate ! = null and Endindate! =" ">and t.create_date<! [Cdata[<= #{endindate}]]>            </if>        </where>    </Select>

3) in service:

 PublicPage<zlfbbean> FINDZLFB (page<zlfbbean>Page,zlfbbean Bean) {        //Generate Data Permission filter condition (DSF is shorthand for datascopefilter, use ${SQLMAP.DSF} in XML to call permissions SQL)User User =Userutils.getuser (); String DSF= Datascopefilter (User, "L", "K");//returned is such as and (K.id= ' User.getid () ')        if((Bean.getcreateby () = =NULL|| Bean.getcreateby (). GetId (). Equals ("")) && (bean.getoffice () = =NULL|| Bean.getoffice (). GetId (). Equals ("")) {DSF= dsf+ "OR i.zlbh in (" + "SELECT DISTINCT t1.zlbh from Zl_hczz T1 joins ZL_HCZZSP T2 on T1.ZLBH=T2.ZLBH wh Ere t2. create_by = "+User.getid ()+ ")" + "OR i.zlbh in (" + "selects DISTINCT t1.zlbh from Zl_hczz T1 joins Zl_hczzqs T2 on T1.ZLBH=T2.ZLBH where T2. create_by = "+user.getid () +" OR ((SELECT COUNT (1) from Sys_user WHERE id= "+user.getid () +" and qsqx= ' "and office_id= '" +u Ser.getoffice (). GetId () + "') > 0) and T2.  Qs_dw = ' "+user.getoffice (). GetId () +" ' "+") "+" OR i.zlbh in ("+" Select DISTINCT t1.zlbh from Zl_hczz T1 joins ZL_HCZZFK T2 on T1.ZLBH=T2.ZLBH where T2. create_by = "+User.getid ()+" ) "; } bean.getsqlmap (). Put ("DSF", DSF); Adds a value to the Sqlmap collection, that is, the value of adding an DSF string is DSF//Set Paging Parametersbean.setpage (page); //Perform paged queriespage.setlist (Zlfbdao.findlist (bean)); returnpage; }

How to use the Sqlmap of splicing SQL statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.