2、构建数据库表,user表和address表,建表语句如下:(使用mysql数据库)
- create table user(
- id int(10) not null auto_increment,
- userName varchar(20) ,
- passWord varchar(20),
- primary key(id)
- );
- insert into user(userName,passWord) values('admin1','admin1');
- insert into user(userName,passWord) values('admin2','admin2');
- insert into user(userName,passWord) values('admin3','admin3');
- insert into user(userName,passWord) values('admin4','admin4');
- create table address(
- user_id int(10) not null ,
- addressName varchar(20) ,
- addressCode varchar(20)
- );
- insert into address(user_id,addressName,addressCode) values(1,'addressName1','addressCode1');
- insert into address(user_id,addressName,addressCode) values(1,'addressName2','addressCode2');
- insert into address(user_id,addressName,addressCode) values(2,'addressName3','addressCode3');
- insert into address(user_id,addressName,addressCode) values(3,'addressName4','addressCode4');
3、生成相应的JavaBean
User.java
- package com.tonyj.pojo;
- import java.sql.Date;
- import java.util.ArrayList;
- import java.util.List;
- public class User {
- private int id;
- private String userName;
- private Date expireDate=null;
- private String passWord;
- private List<Address> address=new ArrayList<Address>();
- //相应的getter和setter方法,构造方法
- }
Address.java
- package com.tonyj.pojo;
- public class Address {
- private String addressName;
- private String addressCode;
- private String user_id;
- //相应的getter和setter方法,构造方法
- }
4、ibatis配置文件的配置
SqlMapConfig.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE sqlMapConfig
- PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
- "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
- <sqlMapConfig>
- <properties resource="jdbc.properties"/>
- <settings
- cacheModelsEnabled="true"
- enhancementEnabled="true"
- lazyLoadingEnabled="true"
- maxRequests="32"
- maxSessions="10"
- maxTransactions="5"
- useStatementNamespaces="true"/>
- <transactionManager type="JDBC">
- <dataSource type="SIMPLE">
- <property name="JDBC.Driver" value="${jdbc.driverClassName}"/>
- <property name="JDBC.ConnectionURL" value="${jdbc.url}"/>
- <property name="JDBC.Username" value="${jdbc.userName}"/>
- <property name="JDBC.Password" value="${jdbc.password}"/>
- </dataSource>
- </transactionManager>
- <sqlMap resource="one2many.xml"/>
- </sqlMapConfig>
配置连接数据库的资源文件:
jdbc.properties
- jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/vin
- jdbc.userName=root
- jdbc.password=sa
一对多映射文件one2many.xml的配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE sqlMap
- PUBLIC "-//ibatis.apache.org//DTD SQL MAP 2.0//EN"
- "http://ibatis.apache.org/dtd/sql-map-2.dtd">
- <sqlMap>
- <typeAlias alias="User" type="com.tonyj.pojo.User"/>
- <typeAlias alias="Address" type="com.tonyj.pojo.Address"/>
- <resultMap class="User" id="get-user-result">
- <result property="id" column="id"/>
- <result property="userName" column="userName"/>
- <result property="passWord" column="passWord"/>
- <result property="expireDate" column="expiredate" />
- <result property="address" column="id" select="getAddressByUserId"/>
- </resultMap>
- <select id="getUser" parameterClass="int" resultMap="get-user-result">
- <![CDATA[
- select id,userName,passWord,expiredate
- from user
- where id=#id#
- ]]>
- </select>
- <select id="getAddressByUserId" parameterClass="int" resultClass="Address">
- select user_id,addressName,addressCode
- from address
- where user_id=#user_id#
- </select>
- </sqlMap>
5、测试类的编写
IbatisOne2Many.java
- package com.tonyj.test;
- import java.io.Reader;
- import java.util.List;
- import com.ibatis.common.resources.Resources;
- import com.ibatis.sqlmap.client.SqlMapClient;
- import com.ibatis.sqlmap.client.SqlMapClientBuilder;
- import com.tonyj.pojo.Address;
- import com.tonyj.pojo.User;
- public class IbatisOne2Many {
- public static void main(String[] args) throws Exception {
- Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
- SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
- // sqlMap系统初始化完毕
- User user=(User) sqlMap.queryForObject("getUser", new Integer(1));
- System.out.println("user_id:"+user.getId()+",userName:"
- +user.getUserName());
- List<Address> addresss=user.getAddress();
- for(int i=0;i<addresss.size();i++){
- System.out.println(addresss.get(i).getUser_id()+"-->"
- +addresss.get(i).getAddressName());
- }
- }
- }
6、执行结果如下:
- log4j:WARN No appenders could be found for logger (com.ibatis.common.jdbc.SimpleDataSource).
- log4j:WARN Please initialize the log4j system properly.
- user_id:1,userName:admin1
- 1-->addressName1
- 1-->addressName2