hibernate中有关系,在mybatis里面也有关系的配置。
一对一
用户和卡,一个用户对应一张卡,一张卡对应一个用户
User实体类中
public class User {
private Integer uid;
private String uname;
private String pass;
//用户里面需要一个card的对象
private Card card;
//get set方法,构造方法,tostrring方法
}
Card实体类中
public class Card {
private Integer uid;
private String number;
//需要一个用户对象
private User user;
//get set方法,构造方法,tostrring方法
}
用户的接口中UserDao
public interface UserDao {
public List<User> findAllUser ();//普通查询全部
public User findUserAndCard(int id);//查询用户的同时查询出卡
public List<User> findAllUserAndCard();
public void deleteUser(int uid);
}
卡的接口中CardDao
public interface CityDao {
public List<City> findAllCity();
public City findIDCityAndProvince(int pid);
public List<City> findAllCityAndProvince();
}
User对应的映射类User.xml
<?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.ywy.dao.UserDao">
<select id="findAllUser" resultType="User">
select * from user;
</select>
<delete id="deleteUser" parameterType="int">
delete from user where uid=#{uid};
</delete>
<!--自定义的返回类型-->
<resultMap id="userAndCard" type="User"><!--主要查询的是用户所以是User-->
<id property="uid" column="UID"></id><!--主键-->
<result property="uname" column="UNAME"></result><!--普通列-->
<result property="pass" column="PASS"></result>
<!--用户里面对应的卡-->
<association property="card" javaType="Card">
<result property="uid" column="UID"></result>
<result property="number" column="NUMBER"></result>
</association>
</resultMap>
<!--查询指定用户里面对应的卡号-->
<!--返回类型就是自定义类型的id名-->
<select id="findUserAndCard" parameterType="int" resultMap="userAndCard">
select * from user u inner join card c on u.uid=c.uid where u.uid=#{uid};
</select>
<!--查询全部用户里面的卡-->
<select id="findAllUserAndCard" resultMap="userAndCard">
select * from user u inner join card c on u.uid=c.uid;
</select>
Card对应的映射类Card.xml
<?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.ywy.dao.CityDao">
<select id="findAllCity" resultType="City">
select * from city;
</select>
<!--自定义类型-->
<resultMap id="findCityAndProvince" type="City"><!--主要查询的是卡,所有返回card对象-->
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
<result property="pid" column="pid"></result>
<association property="province" javaType="Province">
<id property="pid" column="pid"></id>
<result property="pname" column="pname"></result>
</association>
</resultMap>
<!--查询城市对应的省份-->
<select id="findIDCityAndProvince" resultMap="findCityAndProvince" parameterType="int">
select * from city c inner join province p on c.pid=p.pid where c.cid=#{cid};
</select>
<!--查询全部-->
<select id="findAllCityAndProvince" resultMap="findCityAndProvince">
select * from city c inner join province p on c.pid=p.pid;
</select>
</mapper>
测试类中
public class TestMangToMany {
private SqlSession session;
@Before
public void before(){
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(getClass().getClassLoader().getResourceAsStream("mybatis-config.xml"));
session = factory.openSession();
}
@Test
public void test(){
UserDao ud=session.getMapper(UserDao.class);
//接口方式
//普通查询
List<User> list = ud.findAllUser();
System.out.println(list);
//查询全部的用户和对应的卡
User user = ud.findUserAndCard(2);
System.out.println(user.getCard());
System.out.println(user);
}
@After
public void after(){
session.commit();
session.close();
}
一对多
省份和城市,一个省份中有多个城市,一个城市对应一个省份
省份实体类:Province
public class Province {
private Integer pid;
private String pname;
//省份里面有多个城市
private List<City> cities;
//get set方法,构造方法,tostrring方法
}
城市实体类:Ctiy.xml
public class City {
private Integer cid;
private String cname;
private Integer pid;
//一个省份对象
private Province province;
//get set方法,构造方法,tostrring方法
}
省份配置文件Province.xml
<?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.ywy.dao.ProvinceDao">
<select id="findAllProvince" resultType="Province">
select * from province;
</select>
<!--自定义返回类型-->
<resultMap id="findProvinceAndCity" type="Province"><!--返回类型是省份,主要查询的是省份-->
<id property="pid" column="pid"></id>
<result property="pname" column="pname" ></result>
<!--省份里面有多个城市,所以是集合-->
<!--ofType:集合里面是什么类型,就返回什么类型-->
<!--column:城市通过什么查询省份,通过pid-->
<collection property="cities" ofType="City" column="pid">
<id column="cid" property="cid"></id>
<result column="cname" property="cname"></result>
</collection>
</resultMap>
<!--查询指定省份中的城市-->
<select id="findIDProvinceAndCity" resultMap="findProvinceAndCity" parameterType="int">
select * from province p inner join city c on p.pid=c.pid where p.pid=#{pid};
</select>
<!--查询全部省份中的城市-->
<select id="findAllProvinceAndCity" resultMap="findProvinceAndCity" >
select * from province p inner join city c on p.pid=c.pid;
</select>
</mapper>
城市的配置文件City.xml
<?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.ywy.dao.CityDao">
<select id="findAllCity" resultType="City">
select * from city;
</select>
<!--自定义类型-->
<resultMap id="findCityAndProvince" type="City">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
<result property="pid" column="pid"></result>
<association property="province" javaType="Province">
<id property="pid" column="pid"></id>
<result property="pname" column="pname"></result>
</association>
</resultMap>
<!--查询城市对应的省份-->
<select id="findIDCityAndProvince" resultMap="findCityAndProvince" parameterType="int">
select * from city c inner join province p on c.pid=p.pid where c.cid=#{cid};
</select>
<!--查询全部-->
<select id="findAllCityAndProvince" resultMap="findCityAndProvince">
select * from city c inner join province p on c.pid=p.pid;
</select>
</mapper>
测试类
//查询城市,和对应的省份
CityDao cd=session.getMapper(CityDao.class);
List<City> list = cd.findAllCity();
for (City city : list) {
System.out.println(city);
}
//查询省份和对应的城市
ProvinceDao pd=session.getMapper(ProvinceDao.class);
List<Province> list = pd.findAllProvince();
for (Province province : list) {
System.out.println(province.getPname());
System.out.println(province.getCities());
}
多对多
老师和学生 多个老师有多个学生,多个学生有多个老师
老师的实体类Teacher
public class Teacher {
private Integer tid;
private String tname;
//需要一个学生对象
private List<Student> students;
//get set方法,构造方法,tostrring方法
}
学生的实体类Student
public class Student {
private Integer sid;
private String name;
private String sex;
private Integer age;
private Date time;
//需要一个老师对象
private List<Teacher> teachers;
//get set方法,构造方法,tostrring方法
}
老师的映射类Teacher.xml
<?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.ywy.dao.TeacherDao">
<select id="findTeacherById" resultMap="findTeacherAndStudent" parameterType="int">
select * from teacher where tid=#{tid};
</select>
<select id="findAllTeacherAndStudent" resultMap="findTeacherAndStudent">
select * from teacher;
</select>
<resultMap id="findTeacherAndStudent" type="Teacher">
<id column="tid" property="tid"></id>
<result column="tname" property="tname"></result>
<!--查询老师的学生-->
<!--select :引入一个查询,查询老师对应的学生-->
<collection property="students" ofType="Student" select="findTeacherToStuById" column="tid">
<id column="sid" property="sid"></id>
<result column="name" property="name"></result>
<result column="sex" property="sex"></result>
<result column="age" property="age"></result>
<result column="time" property="time"></result>
</collection>
</resultMap>
<!--查询老师的学生-->
<select id="findTeacherToStuById" resultType="Student" parameterType="int">
select * from student s inner join stu_tea st on s.sid=st.sid where st.tid=#{tid};
</select>
</mapper>
学生的映射类Student.xml
<?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.ywy.dao.StudentDao">
<select id="findStudentById" resultMap="findStudentAndStudent" parameterType="int">
select * from student where sid=#{sid};
</select>
<select id="findAllStudent" resultMap="findStudentAndStudent" >
select * from student;
</select>
<!--自定义类型-->
<resultMap id="findStudentAndStudent" type="Student">
<id column="sid" property="sid"></id>
<result column="name" property="name"></result>
<result column="sex" property="sex"></result>
<result column="age" property="age"></result>
<result column="time" property="time"></result>
<!--查询该学生的所有老师-->
<collection property="teachers" ofType="Teacher" select="findStudentByIdTeacher" column="sid">
<!--select :引用一个查询,查询指定学生的老师-->
<id column="tid" property="tid"></id>
<result column="tname" property="tname"></result>
</collection>
</resultMap>
<!--查询学生的老师-->
<select id="findStudentByIdTeacher" resultType="Teacher" parameterType="int">
select * from teacher t inner join stu_tea st on t.tid=st.tid where st.sid=#{sid};
</select>
</mapper>
测试类
StudentDao td=session.getMapper(StudentDao.class);
//查询全部学生的老师
List<Student> list = td.findAllStudent();
for (Student student : list) {
System.out.println(student.getName());
for (Teacher teacher : student.getTeachers()) {
System.out.println(teacher.getTname());
}
}
//查询指定老师的学生
TeacherDao td2=session.getMapper(TeacherDao.class);
//查询全部的老师的学生
List<Teacher> teachers = td2.findAllTeacherAndStudent();
for (Teacher teacher : teachers) {
System.out.println(teacher.getTname());
for (Student student : teacher.getStudents()) {
System.out.println(student.getName());
}
}