Chinaunix首页 | 论坛 | 博客
  • 博客访问: 89868
  • 博文数量: 13
  • 博客积分: 1523
  • 博客等级: 上尉
  • 技术积分: 245
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-14 14:54
文章分类

全部博文(13)

文章存档

2011年(5)

2010年(6)

2009年(1)

2008年(1)

我的朋友

分类: Java

2011-12-01 09:32:47

一:描述下:

  使用2个表,book,user表。一对多关系,一本书有多个作者。

表语句
  1. CREATE TABLE `book` (
  2.   `oid` int(10) NOT NULL ,
  3.   `name` varchar(50) DEFAULT NULL,
  4.   PRIMARY KEY (`oid`)
  5. )
  6.   
  7. DROP TABLE IF EXISTS `user`;
  8. CREATE TABLE `user` (
  9.   `id` int(10) unsigned NOT NULL,
  10.   `name` varchar(50) DEFAULT NULL,
  11.   `book_oid` int(10) DEFAULT NULL,
  12.   PRIMARY KEY (`id`),
  13.   KEY `FK_user_1` (`book_oid`),
  14.   CONSTRAINT `FK_user_1` FOREIGN KEY (`book_oid`) REFERENCES `book` (`oid`)
  15. )

POJO类
  1. package com.pojo;
  2.   
  3. public class User {
  4.  private Integer id;
  5.  private String name;
  6.  private Integer book_oid;


  7. ........一下是get set 注意在ibatis定义的pojo类中要有个空的构造函数哦~~
  8. Java代码
  9.   
  10. package com.pojo;
  11.   
  12. import java.util.List;
  13.   
  14. public class Book {
  15.  private Integer oid;
  16.  private String name;
  17.  private List users;


  18. .......get set 注意:必须定义个list ,ibatis在实现多表关联查询时。
配置文件:对应pojo的 book.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE sqlMap
  3. PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
  4. "">
  5. <sqlMap namespace="test">
  6.   <typeAlias alias="User" type="com.pojo.User"/>
  7.   <typeAlias alias="Book" type="com.pojo.Book"/>
  8.   
  9.   <resultMap id="BookResult" class="Book">
  10.   <result property="oid" column="oid"/>
  11.   <result property="name" column="name"/>
  12.   <result property="users" column="oid" select="getUsersByBookId"/>
  13.   </resultMap>
  14.  

  1.     
  2.  <select id="selectAllBooks" resultMap="BookResult">
  3.  <![CDATA[
  4.  select oid,b.name from book b
  5.  ]]>
  6.  </select>
  7.       
  8.  <select id="getUsersByBookId" parameterClass="int" resultClass="User">
  9.  <![CDATA[
  10.  select id,book_oid,u.name from user u where book_oid = #value#
  11.  ]]>
  12.  </select>
  13. </sqlMap>


  14. 在定义个配置文件调用上面的配置文件:如下:

  15.   
  16. <?xml version="1.0" encoding="UTF-8" ?>
  17. <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "">
  18. <sqlMapConfig>
  19. <settings cacheModelsEnabled="true"
  20.            enhancementEnabled="true"
  21.         lazyLoadingEnabled="true"
  22.         errorTracingEnabled="true"
  23.         maxRequests="132"
  24.         maxSessions="110"
  25.         maxTransactions="15"
  26.         useStatementNamespaces="false" />
  27.  <transactionManager type="JDBC">
  28.   <dataSource type="DBCP">
  29.    <property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
  30.    <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/test" />
  31.    <property name="JDBC.Username" value="root" />
  32.    <property name="JDBC.Password" value="root" />
  33.      
  34.   </dataSource>
  35.  </transactionManager>
  36.  <sqlMap resource="com/crfss/book.xml" />
  37. </sqlMapConfig>


  38. 三:测试类:

  39. package com.test;
  40.   
  41. import java.io.IOException;
  42. import java.io.Reader;
  43. import java.util.Iterator;
  44. import java.util.List;
  45.   
  46. import com.crfss.MainDb;
  47. import com.crfss.Un;
  48. import com.ibatis.common.resources.Resources;
  49. import com.ibatis.sqlmap.client.SqlMapClient;
  50. import com.ibatis.sqlmap.client.SqlMapClientBuilder;
  51. import com.pojo.Book;
  52. import com.pojo.User;
  53.   
  54.   
  55. public class TestDB {
  56.   
  57.  public static void main(String[] args) {
  58.         String resource = "sql-map-config.xml";
  59.         try {
  60.             Reader reader = Resources.getResourceAsReader(resource);
  61.             SqlMapClient mapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
  62.             reader.close();
  63.             List<Book> books=mapClient.queryForList("selectAllBooks");
  64.             Book book=(Book)books.get(0);
  65.             //System.out.println(book.getName());
  66.             List users=(List) book.getUsers();
  67.            
  68.            for (Iterator iterator = users.iterator(); iterator.hasNext();) {
  69.             User user = (User) iterator.next();
  70.             System.out.println(user.getBook_oid());
  71.             System.out.println(user.getName());
  72.             System.out.println(user.getId());
  73.         }
  74.         } catch (IOException e) {
  75.             e.printStackTrace();
  76.         } catch (Exception e) {
  77.             e.printStackTrace();
  78.         }
  79.     }
  80. }

阅读(4634) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~