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