USE mybatis; CREATE TABLE person ( personId VARCHAR(36) PRIMARY KEY, personName VARCHAR(64), personAddress VARCHAR(128), personTel VARCHAR(11) ); -- order 在MySQL中是关键字 CREATE TABLE orders ( orderId VARCHAR(36) PRIMARY KEY, orderNumber VARCHAR(20), orderPrice INT, pid VARCHAR(36) ); INSERT INTO person VALUES('001', 'Jack', 'Wuhan', '1234567'); INSERT INTO orders VALUES('O_00001', '00001', 100, '001'); INSERT INTO orders VALUES('O_00002', '00002', 200, '001'); SELECT p.*, o.* FROM person p JOIN orders o ON (p.personId=o.pid) WHERE p.personId = '001'
public class Person { private String id; private String name; private String address; private String tel; List<Order> orders; // get / set 已省略 } public class Order { private String id; private String number; private Integer price; // get / set 已省略 }
<configuration> <typeAliases> <typeAlias type="com.hehe.mybatis.domain.Person" alias="Person"/> <typeAlias type="com.hehe.mybatis.domain.Order" alias="Order"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/mybatis" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <!-- 映射文件的位置 --> <mapper resource="com/hehe/mybatis/domain/Person.xml" /> <mapper resource="com/hehe/mybatis/domain/Order.xml" /> </mappers> </configuration>
<mapper namespace="com.hehe.mybatis.domain.Person"> <resultMap type="Person" id="personMap"> <id column="personId" property="id"/> <result column="personName" property="name"/> <result column="personAddress" property="address"/> <result column="personTel" property="tel"/> <!-- 一对多 --> <!-- List<Order> orders; property : 实体属性名(集合名) ofType : 集合 中元素的类型 --> <collection property="orders" ofType="Order"> <id column="orderId" property="id"/> <result column="orderNumber" property="number"/> <result column="orderPrice" property="price"/> </collection> </resultMap> <select id="selectPersonById" parameterType="string" resultMap="personMap"> SELECT p.*, o.* FROM person p JOIN orders o ON (p.personId=o.pid) WHERE p.personId = #{id} </select> </mapper>
<mapper namespace="com.hehe.mybatis.domain.Order"> <resultMap type="Order" id="orderMap"> <id column="orderId" property="id"/> <result column="orderNumber" property="number"/> <result column="orderPrice" property="price"/> </resultMap> </mapper>
/* <select id="selectPersonById" parameterType="string" resultMap="orderMap"> SELECT p.*, o.* FROM person p JOIN orders o ON (p.personId=o.pid); where p.id = #{id} </select> */ @Test public void testSelectPersonById() throws Exception { SqlSession session = sqlSessionFactory.openSession(); List<Person> persons = session.selectList("com.hehe.mybatis.domain.Person.selectPersonById", "001"); // 在此处打断点查看 for (Person p : persons) { System.out.println( p.getId() + "\t" + p.getName() + "\t" + p.getAddress() + "\t" + p.getTel() ); for (Order o : p.getOrders()) { System.out.println( o.getId() + "\t" + o.getNumber() + "\t" + o.getPrice() ); } } session.close(); }