MyBatis (四) 註解和預存程序

來源:互聯網
上載者:User
1. sql 公用片段
<sql id="publicfindemp">        select e.*,depname from employee e ,department d        where d.depid=e.depid</sql>

使用 sql 公用片段:

 <include refid="publicfindemp"/>
 <!--3.1 分頁查詢和查詢所有-->    <select id="findEmloyee" parameterType="PageUtil" resultMap="empMap">        <choose>            <!-- 分頁查詢 -->            <when test="startrow!=0 and pagesize>0" >                <include refid="publicfindemp"/>                limit #{startrow},#{pagesize}            </when>            <!-- 查詢所有 -->            <otherwise>                <include refid="publicfindemp"/>            </otherwise>        </choose>    </select>
2. 預存程序原理
delimiter //create procedure pro_emp(in startrow int,in pagesize int)begin   select e.*,d.depname from employee e,department d    where d.depid=e.depid limit startrow,pagesize;end //delimiter;
delimiter //create procedure proc_emptotalcount(out empcount int)begin    select count(*) into empcount from employee e,department d    where d.depid = e.depid;end//delimiter;

在 mapper.xml 裡面這麼寫:
1)IN

<select id="findByPrimaryKey" parameterMap="paramMap" >>>>注意要寫parameterMapstatementType="CALLABLE" >>>>表示是預存程序resultType="com.pojo.Employee">        CALL EmployeeSys.proc_empbyid(?)</select><parameterMap id="paramMap" type="Employee">  <parameter property="empId" mode="IN" jdbcType="INTEGER"/> </parameterMap>

type:封裝 java 對象的類型
jdbcType:參數的資料類型,必須是引用資料類型,必須是大寫;
mode:預存程序的參數的類型,如果是輸入參數則是IN,輸出參數則是OUT;
2)OUT

 <select id="getTotalcount"  parameterMap="totalcountMap"  statementType="CALLABLE">      CALL EmployeeSys.proc_emptotalcount(?)  </select><parameterMap id="totalcountMap" type="PageUtil">     <parameter property="totalcount" mode="OUT" jdbcType="INTEGER"/></parameterMap>

3)關於 out 的 測試

        System.out.println("---getTotalcount(PageUtil pageUtil)---");        PageUtil pageUtil1 = new PageUtil(1,2);        mapper.getTotalcount(pageUtil1);        System.out.println(pageUtil1.getTotalcount());

理解:預存程序 out 的是:out 出property=”totalcount”,並且是封裝在type=”PageUtil”裡面的,所以用pageUtil1.getTotalcount() 來接。select count(*) into empcount ,方法不需要傳回值,直接進屬性裡面撈。 3)註解

在 mapper介面裡面直接寫sql 語句,就不需要使用 xml 檔案了:

 @Select("select * from project") List<Workorder> findName(); // 分頁查詢 @Select("select w.*,p.projectname from Workorder w,project p where   p.id=w.projectid limit #{startrow},#{pagesize}") List<Workorder> findPage(PageUtil pageUtil);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.