Java框架SSM:mybatis之二 一对多的关系
程序员文章站
2022-04-21 23:51:28
...
接下来是一对多的关系!
sid | sname |
1 | 张三 |
2 | 李四 |
did | dname | sid |
1 | 泰迪 |
1 |
2 | 二哈 | 2 |
3 | 藏獒 | 1 |
可以看出张三有两只狗,分别是泰迪和藏獒。
而李四只有二哈一只狗。
接下来,上代码。
package com.hc.entity;
import java.util.List;
public class Son {
private int sonId;
private String sonName;
private List<Dog> dogs;
public Son() {
}
public Son(String sonName) {
this.sonName = sonName;
}
public int getSonId() {
return sonId;
}
public void setSonId(int sonId) {
this.sonId = sonId;
}
public String getSonName() {
return sonName;
}
public void setSonName(String sonName) {
this.sonName = sonName;
}
public List<Dog> getDogs() {
return dogs;
}
public void setDogs(List<Dog> dogs) {
this.dogs = dogs;
}
}
这是son的表。
son有一个属性类型为
List<Dog>
这是为了能够承装某son多条狗。
package com.hc.entity;
public class Dog {
private int dogId;
private String dogName;
private Son son;
public Dog() {
}
public Dog(String dogName) {
this.dogName = dogName;
}
public int getDogId() {
return dogId;
}
public void setDogId(int dogId) {
this.dogId = dogId;
}
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public Son getSon() {
return son;
}
public void setSon(Son son) {
this.son = son;
}
}
这是dog,dog有一个属性是主人son。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hc.dao.UserDao">
<resultMap id="SonDog" type="Son">
<id property="sonId" column="sonId"></id>
<result property="sonName" column="sonName"></result>
<collection property="dogs" ofType="Dog" select="selDog" column="sonId">
<id property="dogId" column="dogId"></id>
<result property="dogName" column="dogName"></result>
</collection>
</resultMap>
<select id="allSon" resultMap="SonDog">
select * from son
</select>
<select id="selDog" resultType="Dog" parameterType="int">
select * from dog where sonId=#{sonId}
</select>
</mapper>
这是关系的配置,Son.xml。
collection:相当于son对象中的
List<Dog>
collection select:在collection里调用了一个子查询
selDog
非常nice。
package com.hc.dao;
import com.hc.entity.Son;
import com.hc.entity.User;
import java.util.List;
public interface UserDao {
public List<User> allUser();
public List<Son> allSon();
}
这是dao接口。
package com.hc.test;
import com.hc.dao.UserDao;
import com.hc.entity.Dog;
import com.hc.entity.Son;
import com.hc.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
public class First {
private SqlSession session;
private UserDao userDao;
@Before
public void before(){
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(getClass().getClassLoader().getResourceAsStream("myBatis-config.xml"));
session = factory.openSession();
userDao = session.getMapper(UserDao.class);
}
@Test
public void testallSon(){
List<Son> sons= userDao.allSon();
for (Son son : sons) {
System.out.println(son.getSonId()+"\t"+son.getSonName());
for (Dog dog : son.getDogs()) {
System.out.println(dog.getDogId()+"\t"+dog.getDogName());
}
System.out.println("\n");
}
}
@Test
public void testallUser(){
List<User> users= userDao.allUser();
for (User user : users) {
System.out.println(user.getUserId()+"\t"+user.getUserName());
System.out.println(user.getCard().getCardId()+"\t"+user.getCard().getCardNum());
System.out.println("\n");
}
}
@After
public void after(){
session.commit();
session.close();
}
}
单元测试,运行
testallSon
,则可以获取到某son的多条狗。
非常好用!nice!
推荐阅读