ibatis一对多映射


   参考:http://blog.sina.com.cn/s/blog_7f1700e70100wlqe.html


数据库:


CREATE TABLE `book` (
  `id` int(10) NOT NULL ,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ;


CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL,
  `name` varchar(50) DEFAULT NULL,
  `book_id` int(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK_user_1` (`book_id`),
  CONSTRAINT `FK_user_1` FOREIGN KEY (`book_id`) REFERENCES `book` (`id`)
) ;


(1)sqlMapConfig.xml配置


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

<!DOCTYPE sqlMapConfig 

    PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" 

    "http://www.ibatis.com/dtd/sql-map-config-2.dtd">



<sqlMapConfig>

	<settings cacheModelsEnabled="true" enhancementEnabled="true"

		lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"

		maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />


	<transactionManager type="JDBC">

		<dataSource type="SIMPLE">

			<!-- JDBC连接需要的配置 -->

			<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />

			<property name="JDBC.ConnectionURL"

				value="jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8" />

			<property name="JDBC.Username" value="root" />

			<property name="JDBC.Password" value="wodwl" />



			<!-- 连接池配置 -->

			<property name="Pool.MaximumActiveConnections" value="10" />

			<property name="Pool.MaximumIdleConnections" value="5" />

			<property name="Pool.MaximumCheckoutTime" value="120000" />

			<property name="Pool.TimeToWait" value="500" />

			<property name="Pool.PingQuery" value="select 1 from sample" />

			<property name="Pool.PingEnabled" value="false" />

			<property name="Pool.PingConnectionsOlderThan" value="1" />

			<property name="Pool.PingConnectionsNotUsedFor" value="1" />

		</dataSource>

	</transactionManager>
	

	<!-- 映射文件 -->

	<sqlMap resource="com/wodwl/map/User.xml" />
	<sqlMap resource="com/wodwl/onemany/User.xml" />


</sqlMapConfig>

 


(2)User.xml配置


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

<!DOCTYPE sqlMap PUBLIC

    "-//iBATIS.com//DTD SQL Map 2.0//EN"

    "http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="user1">

	<typeAlias alias="user1" type="com.wodwl.onemany.User"/>
	<typeAlias alias="book1" type="com.wodwl.onemany.Book"/>
	<typeAlias alias="integer1" type="java.lang.Integer"/>
	
	<resultMap class="book1" id="bookResult">
		<result property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="users" column="id" select="user1.getUsersByBookId"/>
	</resultMap>
	
	<select id="selectAllBooks" resultMap="bookResult">
		select id,name from book
	</select>
	
	<select id="getUsersByBookId" parameterClass="integer1" resultClass="user1">
		select id,book_id,name from user where book_id=#value#
	</select>

</sqlMap>

(3)Book.class

package com.wodwl.onemany;

public class User {
	private Integer id;
	private String name;
	private Integer book_id;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getBook_id() {
		return book_id;
	}
	public void setBook_id(Integer book_id) {
		this.book_id = book_id;
	}

}

(4)User.class

package com.wodwl.onemany;

public class User {
	private Integer id;
	private String name;
	private Integer book_id;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getBook_id() {
		return book_id;
	}
	public void setBook_id(Integer book_id) {
		this.book_id = book_id;
	}

}


(5)测试类

package com.wodwl.onemany;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class Test {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws IOException, SQLException {

		String resource="com/wodwl/SqlMapConfig.xml";
		Reader reader=Resources.getResourceAsReader(resource);
		SqlMapClient smc=SqlMapClientBuilder.buildSqlMapClient(reader);
		
		List<Book> books=smc.queryForList("user1.selectAllBooks");
		Book book=books.get(0);
		List users=(List)book.getUsers();
		for(Iterator iterator=users.iterator();iterator.hasNext();){
			User user=(User)iterator.next();
			System.out.println(user.getName());
		}
	

	}

}



阅读更多
个人分类: ibatis
上一篇ibatis参考
下一篇使用maven构建hibernate
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭