ibatis3.0儲存過存的一些細節。

來源:互聯網
上載者:User

1.因為支援注釋和代碼方式產生mapper,所以原來的  parameterMap反對使用,也就是不建議用parameter的順序支對應"?" 方式的參數。

   因為mapper的資料是一個純xml的,而xml的元素如果沒有標記為序列是沒有順序的,所以用

<parameterMap>

  <parameter property="p1" .../>

  <parameter property="p2" .../>

<parameterMap>

這樣的循序關聯性對應procedure(?,?)的參數是不可靠的。

所以3.0直接使用#{p1},#{p2,otherAttribute........}這樣的命令參數可以直接和map的KEY以及BEAN的屬性名稱相匹配,所以應該使用

parameterType來指定一個綁定了具名引數的Bean或map.

 

2.直接看例子:

table: Person

id int(8) auto_increment pk

name varchar(20)

ago int(8)

 

SessionFactoryConf.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
  "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
  <environments default="development">
      <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="org.gjt.mm.mysql.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/ibatis"/>
        <property name="username" value="ibts_root"/>
        <property name="password" value="coffee&amp;tea"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="com/axman/PersonMapper.xml"/>
  </mappers>
</configuration>

 

 

 

a:無傳回值

在navicat中建立一個預存程序,proc_test(pname,pago):

BEGIN
    #Routine body goes here...
    insert into Person (name,ago) values (pname,pago);
END

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
    PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
    "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.axman.PersonMapper">
  <select id="getPerson" parameterType="int" resultType="com.axman.Person">
    select * from Person where id = #{id}
  </select>
  <update id="execProcedure" statementType="CALLABLE" parameterType="com.axman.Person">
      { call proc_test(#{name},#{ago})}
  </update>
</mapper>

 

        String resource = "com/axman/SessionFactoryConf.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = factory.openSession();
        try {
//          Map m = new HashMap();
//          m.put("name","ibatisVersion");
//          m.put("ago",40);
          Person  p = new Person();
          p.setName("ibatisVerion");
          p.setAgo(40);

          session.update("com.axman.PersonMapper.execProcedure", p);
        } finally {
          session.close();
        }

無傳回值調用時使用update,不要用select[ForXXX],否則會死等

 

 

b:返回一個值,這例用返回Key來說只是說明返回單值,實際上的返回KEY可以直接註冊AutoGenrateKeys擷取,這裡只是為了返回一個單一對象,和“select 1 ”一樣。

BEGIN
    #Routine body goes here...
    insert into Person (name,ago) values (pname,pago);

     select last_insrt_id();
END

 

 

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
    PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
    "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.axman.PersonMapper">
  <select id="getPerson" parameterType="int" resultType="com.axman.Person">
    select * from Person where id = #{id}
  </select>
  <select id="execProcedure" statementType="CALLABLE" parameterType="com.axman.Person">
      { call proc_test(#{name},#{ago})}
  </select>
</mapper>

這時不需要指定resultTyep

 

        String resource = "com/axman/SessionFactoryConf.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = factory.openSession();
        try {
//          Map m = new HashMap();
//          m.put("name","ibatisVersion");
//          m.put("ago",40);
          Person  p = new Person();
          p.setName("ibatisVerion");
          p.setAgo(40);

          Integer i = (Integer)session.selectOne("com.axman.PersonMapper.execProcedure", p);
        } finally {
          session.close();
        }

c: 返回結果維集:

 

BEGIN
    #Routine body goes here...
    insert into Person (name,ago) values (pname,pago);

     select * from Person;

END

 

 

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
    PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
    "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.axman.PersonMapper">
  <select id="getPerson" parameterType="int" resultType="com.axman.Person">
    select * from Person where id = #{id}
  </select>
  <select id="execProcedure" statementType="CALLABLE" parameterType="com.axman.Person" resultType="hashmap">
      { call proc_test(#{name},#{ago})}
  </select>
</mapper>

這時需要指定resultTyep

 

 

        String resource = "com/axman/SessionFactoryConf.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = factory.openSession();
        try {
//          Map m = new HashMap();
//          m.put("name","ibatisVersion");
//          m.put("ago",40);
          Person  p = new Person();
          p.setName("ibatisVerion");
          p.setAgo(40);

          List<map>  l = session.selectList("com.axman.PersonMapper.execProcedure", p);

          for(Map m : l){

             System.out.println(m.get("id")+":"+m.get("name")+":"m.get("ago");

          }
        } finally {
          session.close();
        }

 

 

d:對於多結果集,需要修改源碼。即使攔截注入也無法一次返回多結果和輸出參數,因為多個結果和輸出參數需要一個對象做為容器把它們同時返回出來,所以只能修改它的原始碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.