<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    少年阿賓

    那些青春的歲月

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

    今天在項目中用了mybatis的resultMap。以前用的時候都是一些簡單的查詢,修改,分頁。這次涉及到了POJO對象之間的一對多和多對一的關系映射。

        mybatis有幾種使用方式, 我喜歡用mapper的方式,然后用spring來管理mybatis.

        開發工具是Eclipse jee, mybatis版本是3.0.5, mybatis-sprint-1.0.1

       工程文件目錄

    mybatis-config.xml 是mybatis的配置文件:

     

    <configuration>
        <settings>
            <setting name="cacheEnabled" value="false" />
            <setting name="useGeneratedKeys" value="true" />
            <setting name="defaultExecutorType" value="REUSE" />
        </settings>
    	<mappers>
            <mapper resource="com/exam/persistence/mapper/ClientMapper.xml" />
        </mappers>
    </configuration>

    applicationContext.xml:

     

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<property name="dataSource" ref="dataSource" />
    	<property name="configLocation" value="/WEB-INF/mybatis-config.xml" />
    </bean>

    <bean id="clientMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

        <property name="mapperInterface" value="com.exam.persistence.mapper.ClientMapper" />

        <property name="sqlSessionFactory" ref="sqlSessionFactory" />

    </bean>

    配置文件就是這些了。主要的東西在下面:

    業務邏輯涉及到三張表: Client, Subscriber, Account. 其中Client表和Subscriber表是多對一的關系. Client表和Account是一對多的關系。

     在com.example.persistence.mapper目錄下,需要創建兩個文件分別是:ClientMapper.java 和 ClientMapper.xml

    對象映射關系主要在ClientMapper.xml中定義:

     

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.exam.persistence.mapper.ClientMapper">
        <resultMap id="clients" type="com.exam.entity.Clients">
        	<id property="externalId" column="externalId" />
        	<result property="id" column="id"/>
        	<result property="subscriptionId" column="subscriptionId"/>
        	<association property="subscription" column="subscriptionId" javaType="com.exam.entity.Subscription" select="selectSubscription"/>
        	<collection property="accounts" column="id" ofType="com.exam.entity.Accounts" select="selectAccounts"/>
        </resultMap>
        
        <select id="getClientByID" resultMap="clients" parameterType="java.lang.String">
           select 
               client.internal_id as id,
               client.external_id as externalId,
           from CLIENT client 
                left outer join SUBSCRIPTION subscription on client.subscription_id = subscription.subscription_id
                left outer join ACCOUNT accounts on client.internal_id = accounts.id
            where client.external_id = #{external_id}
        </select>
        
        <select id="selectAccounts" parameterType="int" resultType="com.exam.entity.Accounts">
        	select * from ACCOUNT where client_id=#{client_id}
        </select>
        <select id="selectSubscription" parameterType="java.lang.String" resultType="com.exam.entity.Subscription">
        	select * from SUBSCRIPTION where subscription_id = #{subscription_id}
        </select>
    </mapper>

    這個xml文件定義了一個resultMap id="clients". clients里面包含了一個assoction(多對一)和一個collection(一對多). 這兩個分別對應了兩個select id.

     ClientMapper.java是一個interface:

    import java.util.List;
    import com.exam.entity.Clients;
    public interface ClientMapper {
      public List<Clients> getClientByID(String external_id);
    }

    其中函數名"getClientByID"應該和ClientMapper.xml中的select id的值相同。

     在com.exam.entity目錄下面需要定義Clients, Accounts, Subscription 三個POJO類。就不在這里寫POJO類了。

    這樣我們在service邏輯中就可以使用ClientMapper.

     

    public class ClientServiceImpl implements ClientService {
    	private ClientMapper clientMapper;
    
    	public ClientMapper getClientMapper() {
    		return deviceMapper;
    	}
    
    	public void setClientMapper(ClientMapper clientMapper) {
    		this.clientMapper = clientMapper;
    	}
    
             @Override
    	public List<Clients> getClientByID(String external_id) {
    	    return getClientMapper().getClientByID(external_id);
    	}
    }我們需要在applicationContext中把clientMapper注入到這個server類中就ok了。
    先寫到這里。




    http://m.oschina.net/blog/29845
    posted on 2012-03-28 22:23 abin 閱讀(4846) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 男的把j放进女人下面视频免费| 成人毛片免费观看| 亚洲精品视频在线观看免费| 午夜宅男在线永久免费观看网| 色偷偷亚洲男人天堂| 亚洲国产精品乱码一区二区| 五月婷婷综合免费| 大片免费观看92在线视频线视频| 亚洲av中文无码乱人伦在线播放| 好爽…又高潮了免费毛片| 中文字字幕在线高清免费电影| 亚洲国产综合精品| 亚洲 综合 国产 欧洲 丝袜| 99爱在线精品视频免费观看9 | 亚洲AV无码片一区二区三区| 亚洲色爱图小说专区| 国产h视频在线观看免费| 久久精品成人免费国产片小草| 国产成人精品日本亚洲网址 | 久久丫精品国产亚洲av| 永久在线毛片免费观看| 99爱在线精品视频免费观看9| 美女又黄又免费的视频| 亚洲自国产拍揄拍| 亚洲成A人片在线观看无码不卡| 拍拍拍又黄又爽无挡视频免费| 久久久久久国产精品免费免费男同 | 特黄特色大片免费| 男人天堂2018亚洲男人天堂| 国产精品国产亚洲精品看不卡| 四虎免费久久影院| 日韩欧毛片免费视频| 久久久久成人精品免费播放动漫| 男女交性无遮挡免费视频| 国产午夜亚洲精品| 亚洲沟沟美女亚洲沟沟| 亚洲男人的天堂在线播放| 亚洲热线99精品视频| 亚洲国产精品成人久久蜜臀| 精品少妇人妻AV免费久久洗澡 | 亚洲国产成人精品无码区在线观看|