ibatis 实现一对多代码

package users.dto;

public class UserDealDTO {
	private String deal_id;
	private int quantity;
	public String getDeal_id() {
		return deal_id;
	}
	public void setDeal_id(String deal_id) {
		this.deal_id = deal_id;
	}
	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
	
	public String toString(){
		return "[deal_id = " + deal_id + ",Quantity = " + quantity + "]";
	}
}
package users.dto;

import java.util.List;

public class UserDTO {
	private String id;
	private String username;
	private String password;
	private List<UserDealDTO> userDeals;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List<UserDealDTO> getUserDeals() {
		return userDeals;
	}
	public void setUserDeals(List<UserDealDTO> userDeals) {
		this.userDeals = userDeals;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}



package users.dto;


public class UserInfoDTO {
	private String username;
	private String password;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}



表结构  select id,username,password from user_struts  以及 select deal_id,quantity from user_struts_deal where id ,id为外键

xml配置

<!-- 实现一对多的方式  一 
	<resultMap class="userInfo" id="userInfoResult" groupBy="id"><!--此id名称是property对应的名称-->
		<result property="id" column="id"/>   
  		<result property="username" column="username"/>
  		<result property="password" column="password"/>
  		<result property="userDeals" resultMap="User.userDealResult"/>   
   </resultMap>
    这个地方的resultMap必须加User这个namespace前缀 
   
    <resultMap class="userDeal" id="userDealResult" groupBy="deal_id">  <!--此deal_id名称是property对应的名称-->
        <result property="deal_id" column="deal_id" />
        <result property="quantity" column="quantity" />
    </resultMap>  
  
   <select id="getUserDeals" parameterClass="int" resultMap="userInfoResult">
   		select t.id as id,t.username as username,d.deal_id as deal_id,d.quantity as quantity
   		 	from user_struts t
   		join user_struts_deal d on
   			t.id = d.id
   		where t.id = #value#
   </select>
-->
<!--一对多实现方式二 -->
	<resultMap class="userInfo" id="userInfoResult" >
		<result property="id" column="id"/>   
  		<result property="username" column="username"/>
  		<result property="password" column="password"/>
  		<result property="userDeals" column="id" select="userDealResult"/>  <!--此id名称是column对应的名称--> 
   </resultMap>
   
   <select id="getUserDeals" parameterClass="int" resultMap="userInfoResult">
   		select id,username,password from user_struts where id = #value#
   </select>
   
   <select id="userDealResult" parameterClass="int" resultClass="userDeal">
   		select deal_id,quantity from user_struts_deal where id = #value#
   </select>
biz-context.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:TEST" />
        <property name="username" value="dbo" />
        <property name="password" value="oracle" />
    </bean>
    
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">   
        <property name="dataSource">   
            <ref local="dataSource"/>   
        </property>   
        <property name="configLocation">   
            <value>/Test/springdemotest/sqlmapping-config.xml</value>   
        </property>
    </bean>
    
   <bean id="SpringContextUtil" 
       class="users.util.SpringContextUtil"/>

	<bean id="loginService" 
		class="users.login.service.Impl.LoginServiceImpl">
	</bean>
	
	<bean id="userDAO" 
		class="users.login.userDAO.Impl.UserDAOImpl">
	    <property name="dataSource">
			<ref local="dataSource" />
		</property>
		<property name="sqlMapClient">
			<ref bean="sqlMapClient" />
		</property>
	</bean>
	
    <bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref local="dataSource"/>
		</property>
	</bean>	
	
	<bean id="userDAOProxy"
	class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
	<property name="transactionManager">
		<ref bean="transactionManager" />
	</property>
	<property name="target">
		<ref local="userDAO" />
	</property>
	<property name="transactionAttributes">
		<props>
			<prop key="insert*">PROPAGATION_REQUIRED</prop>
			<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
		</props>
	</property>
	</bean>
	
    <bean id="LoginAction" class="users.login.action.LoginAction">    
    </bean>
    
</beans> 
	


两种实现一对多的配置方式

测试代码

public class SpringDemoTest {
	public static void main(String args[]) throws SQLException{
		//ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
		ApplicationContext ctx = new ClassPathXmlApplicationContext("Test/springdemotest/biz-context.xml");
		UserDAO dao = (UserDAO)ctx.getBean("userDAO");
		SqlMapClient sqlMapClient = (SqlMapClient) ctx.getBean("sqlMapClient");
		//UserDTO userDTO = dao.getUserDeal(1);
		UserDTO userDTO = (UserDTO) sqlMapClient.queryForObject("getUserDeals", 2);
		List<UserDealDTO> userDeals = userDTO.getUserDeals();
		System.out.println("userID = " + userDTO.getId() + ", password = " + userDTO.getPassword() + ", user = " + userDTO.getUsername());
		for(UserDealDTO deals:userDeals){
			System.out.println(deals);
		}
	}

}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值