`
leon.s.kennedy
  • 浏览: 106464 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

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!");
 }

2
2
分享到:
评论
12 楼 leon.s.kennedy 2012-05-29  
sokoo108 写道
hellostory 写道
sokoo108 写道
一般我是不写 resultMap 的,定义实体类属性的时候就于数据库字段一致


这个在实际开发中往往过于理想化!
加入订单类Order和订单明细类OrderItem都有一个字段叫做createTime(制单时间)或modifyTime(最后修改时间),并且需要一次查询取出一条订单及其明细(结合标签<association>),那么在SQL语句中createTime必须冲突~,诸如此类太多了。。。


是的,你说的这类问题的确遇到过,类似这种问题一般在sql里就被 as 了。


与其用sql as 多表联查容易混淆,倒不如定义个resultMap一目了然
至少我是这么认为的
11 楼 sokoo108 2012-05-28  
hellostory 写道
sokoo108 写道
一般我是不写 resultMap 的,定义实体类属性的时候就于数据库字段一致


这个在实际开发中往往过于理想化!
加入订单类Order和订单明细类OrderItem都有一个字段叫做createTime(制单时间)或modifyTime(最后修改时间),并且需要一次查询取出一条订单及其明细(结合标签<association>),那么在SQL语句中createTime必须冲突~,诸如此类太多了。。。


是的,你说的这类问题的确遇到过,类似这种问题一般在sql里就被 as 了。
10 楼 hellostory 2012-05-28  
sokoo108 写道
一般我是不写 resultMap 的,定义实体类属性的时候就于数据库字段一致


这个在实际开发中往往过于理想化!
加入订单类Order和订单明细类OrderItem都有一个字段叫做createTime(制单时间)或modifyTime(最后修改时间),并且需要一次查询取出一条订单及其明细(结合标签<association>),那么在SQL语句中createTime必须冲突~,诸如此类太多了。。。
9 楼 feijing 2012-05-28  
这是一套封装的方法。
8 楼 leon.s.kennedy 2012-05-28  
sokoo108 写道
一般我是不写 resultMap 的,定义实体类属性的时候就于数据库字段一致

有些时候不一致
好比DB中的字段命名规范为:表名缩写+下划线+字段名
实体类中映射时看着别扭,倒不如让他原生态 加个resultMap = =
7 楼 sokoo108 2012-05-28  
一般我是不写 resultMap 的,定义实体类属性的时候就于数据库字段一致
6 楼 pppqqq800 2012-05-28  
leon.s.kennedy 写道
pppqqq800 写道
已经定义了
  <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>

就不用在sql 里写as 了吧~


这个没有试过,个人感觉可以不用as
resultMap是 实体类属性 与DB中的列 映射


以前看过spring 的demo   就是将bean 做成全映射 这样SQL就省去AS ,我是觉得ibatis挺好的  够灵活。
5 楼 leon.s.kennedy 2012-05-28  
jinpengaigo 写道
个人感觉 myibatis还是很好用的  起码不用调用哪些指定的方法  直接dao里映射到xml里的方法  很简洁


Mybaits也得调用对应的方法  sqlSession.insert("insertStudent", stu);
不知道您说的 dao里映射到xml里的方法 是怎么回事,还请指点
4 楼 leon.s.kennedy 2012-05-28  
pppqqq800 写道
已经定义了
  <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>

就不用在sql 里写as 了吧~


这个没有试过,个人感觉可以不用as
resultMap是 实体类属性 与DB中的列 映射
3 楼 jinpengaigo 2012-05-28  
个人感觉 myibatis还是很好用的  起码不用调用哪些指定的方法  直接dao里映射到xml里的方法  很简洁
2 楼 pppqqq800 2012-05-28  
已经定义了
  <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>

就不用在sql 里写as 了吧~
1 楼 missing1314521 2012-05-28  
正在学习ibatis,谢谢分享。

相关推荐

Global site tag (gtag.js) - Google Analytics