Ibatis的CRUD

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="Account">

  <!-- Use type aliases to avoid typing the full classname every time. -->
  <typeAlias alias="Account" type="com.ibatis.model.Account"/>

  <!-- Result maps describe the mapping between the columns returned
       from a query, and the class properties.  A result map isn't
       necessary if the columns (or aliases) match to the properties
       exactly. -->
  <resultMap id="AccountResult" class="Account">
    <result property="id" column="ACC_ID"/>
    <result property="firstName" column="ACC_FIRST_NAME"/>
    <result property="lastName" column="ACC_LAST_NAME"/>
    <result property="emailAddress" column="ACC_EMAIL"/>
  </resultMap>

  <!-- Select with no parameters using the result map for Account class. -->
  <select id="selectAllAccounts" resultMap="AccountResult">
    select * from ACCOUNT
  </select>

  <!-- A simpler select example without the result map.  Note the
       aliases to match the properties of the target result class. -->
  <select id="selectAccountById" parameterClass="int" resultClass="Account">
    select
      ACC_ID as id,
      ACC_FIRST_NAME as firstName,
      ACC_LAST_NAME as lastName,
      ACC_EMAIL as emailAddress
    from ACCOUNT
    where ACC_ID = #id#
  </select>
  
  <!-- Insert example, using the Account parameter class -->
  <insert id="insertAccount" parameterClass="Account">
    insert into ACCOUNT (
      ACC_ID,
      ACC_FIRST_NAME,
      ACC_LAST_NAME,
      ACC_EMAIL
    )
    values (
      #id#, #firstName#, #lastName#, #emailAddress#
    )
  </insert>

  <!-- Update example, using the Account parameter class -->
  <update id="updateAccount" parameterClass="Account">
    update ACCOUNT set
      ACC_FIRST_NAME = #firstName#,
      ACC_LAST_NAME = #lastName#,
      ACC_EMAIL = #emailAddress#
    where
      ACC_ID = #id#
  </update>

  <!-- Delete example, using an integer as the parameter class -->
  <delete id="deleteAccountById" parameterClass="int">
    delete from ACCOUNT where ACC_ID = #id#
  </delete>

  <!-- Insert Account with sequence -->
  <insert id="insertAccountBySequence" parameterClass="Account">
   <selectKey resultClass="int" keyProperty="id">
    SELECT SEQ_ACCOUNT_PK_ID.NEXTVAL FROM DUAL
   </selectKey>
   
   insert into ACCOUNT (
      ACC_ID,
      ACC_FIRST_NAME,
      ACC_LAST_NAME,
      ACC_EMAIL
    )
    values (
      #id#, #firstName#, #lastName#, #emailAddress#
    )
  </insert>
 
</sqlMap>

-----------------------------------------------------------------

 

测试:

@Test
 public void insertAccountTest() throws SQLException{
  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  AccountDAO dao = (AccountDAO)ctx.getBean("accountDAO");
  Account account = new Account();
  account.setId(104);
  account.setFirstName("ff");
  account.setLastName("ff");
  account.setEmailAddress("ee");
  dao.insertAccount(account);
  System.out.println("add ok!");
 }
 
 @Test
 public void selectAllAccountsTest() throws SQLException{
  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  AccountDAO dao = (AccountDAO)ctx.getBean("accountDAO");
  for(Account ac : dao.selectAllAccounts()){
   System.out.println(ac);
  }
 }
 
 @Test
 public void selectAccountByIdTest() throws SQLException{
  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  AccountDAO dao = (AccountDAO)ctx.getBean("accountDAO");
  Account account = dao.selectAccountById(100);
  System.out.println(account);
 }
 
 @Test
 public void updateAccountTest() throws SQLException{
  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  AccountDAO dao = (AccountDAO)ctx.getBean("accountDAO");
  Account account = new Account();
  account.setId(100);
  account.setFirstName("ccc");
  account.setLastName("ccc");
  account.setEmailAddress("ccc");
  dao.updateAccount(account);
  System.out.println("updated!");
 }
 
 @Test
 public void deleteAccountTest() throws SQLException{
  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  AccountDAO dao = (AccountDAO)ctx.getBean("accountDAO");
  dao.deleteAccount(0);
  System.out.println("deleted!");
 }
 
 @Test
 public void insertAccountBySequenceTest() throws SQLException{
  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  AccountDAO dao = (AccountDAO)ctx.getBean("accountDAO");
  Account account = new Account();
  account.setId(100);
  account.setFirstName("ccc");
  account.setLastName("ccc");
  account.setEmailAddress("ccc");
  dao.insertAccountBySequence(account);
  System.out.println("saved with sequence!");
 }

猜你喜欢

转载自leon-s-kennedy.iteye.com/blog/1542621