myBatis实现一对一和一对多和多对多
数据库建表
一对一 外键声明在任何一方都行 丈夫和妻子 外键在丈夫表里
一对多 外键声明在对多的一方 组和用户 外键在用户表里
多对多 创建桥表,也就是关系表。引入两个表的主键作为外键 学生和老师
建立pojo类
一对一 妻子的对象作为丈夫类的成员变量
一对多 在一的pojo类里创建多的集合 在group类的创建Collection<User> users 作为成员变量
多对多 参考一对多。在两个pojo类里分别创建集合
修改mybatis-config.xml配置文件
重命名类。<typeAlises><package name="com.pojo"></typeAlises>
引入映射文件<mapper resource="com/briup/mapper/XXX.xml"/>
映射文件
<mapper namespace="com.briup.HusbandMapper"> //对应接口类
将从数据库里查询出来的数据封装成对象有两种方法,嵌套结果和嵌套查询(如果是嵌套结果记得给从数据库查出来的列起别名,如果select语句查出来的视图中有两列的列名一样的话,mybatis在封装对象的时候,不知道具体调用哪一个列的数据,会导致数据封装出错)
一对一使用association标签,一对多或多对多使用collection标签
嵌套结果(一条sql语句查询多张表,一个对象的封装的时候调用另一个对象的封装。给列起别名)
<resultMap id="HusbandResult" type="husband">
<id property="id" column="hid"/>
<result property="name" column="hname"/>
<result property="age" column="hage"/>
<association property="wife" resultMap="WifeResult"/> //如果是一对多的时候使用collection标签
</resultMap>
<resultMap id="WifeResult" type="wife">
<id property="id" column="wid"/>
<result property="name" column="wname"/>
<result property="age" column="wage"/>
</resultMap>
<select id="findHusbandWithWifeById" resultMap="HusbandResult" paramterType="int">
select h.id hid,h.name hname,h.age hage,w.id wid,w.name wname,w.age wage
from husband h,wife w
where h.id=#{id} and h.wife_id=w.id
</select>
嵌套查询(一条sql语句查询一张表,一张表查出来的某一列作为查询另一张表的条件。不用起别名,因为一张表对应一个结果集)
<resultMap id="WifeResult" type="wife">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
<select id="findWife" paramterType="int" resultMap="WifeResult">
select id,name,age
from wife
where id=#{id}
</select>
<resultMap id="HusbandResult" type="husband">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="wife" column="wife_id" select="findWife"/>
</resultMap>
<select id="findHusbandWithWifeById" resultMap="HusbandResult" paramterType="int">
select id,name,age,wife_id
from husband
where id=#{id}
</select>
插入数据到数据库中:
<insert id="insertWife" paramterType="wife">
isnert
into wife(id,name,age)
values(#{id},#{name},#{age})
</insert>
映射接口
public interface HusbandMapper{
public List<Husband> findHusbandWithWifeById(Integer id);
public void insertWife(Wife wife);
}
测试类
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
Mapper mapper = session.getMapper(HusbandMapper.class);//接口类
mapper.insertWife(wife);
List<Husband> list = mapper.findHusbandWithWifeById(1);
session.close();
数据库建表
一对一 外键声明在任何一方都行 丈夫和妻子 外键在丈夫表里
一对多 外键声明在对多的一方 组和用户 外键在用户表里
多对多 创建桥表,也就是关系表。引入两个表的主键作为外键 学生和老师
建立pojo类
一对一 妻子的对象作为丈夫类的成员变量
一对多 在一的pojo类里创建多的集合 在group类的创建Collection<User> users 作为成员变量
多对多 参考一对多。在两个pojo类里分别创建集合
修改mybatis-config.xml配置文件
重命名类。<typeAlises><package name="com.pojo"></typeAlises>
引入映射文件<mapper resource="com/briup/mapper/XXX.xml"/>
映射文件
<mapper namespace="com.briup.HusbandMapper"> //对应接口类
将从数据库里查询出来的数据封装成对象有两种方法,嵌套结果和嵌套查询(如果是嵌套结果记得给从数据库查出来的列起别名,如果select语句查出来的视图中有两列的列名一样的话,mybatis在封装对象的时候,不知道具体调用哪一个列的数据,会导致数据封装出错)
一对一使用association标签,一对多或多对多使用collection标签
嵌套结果(一条sql语句查询多张表,一个对象的封装的时候调用另一个对象的封装。给列起别名)
<resultMap id="HusbandResult" type="husband">
<id property="id" column="hid"/>
<result property="name" column="hname"/>
<result property="age" column="hage"/>
<association property="wife" resultMap="WifeResult"/> //如果是一对多的时候使用collection标签
</resultMap>
<resultMap id="WifeResult" type="wife">
<id property="id" column="wid"/>
<result property="name" column="wname"/>
<result property="age" column="wage"/>
</resultMap>
<select id="findHusbandWithWifeById" resultMap="HusbandResult" paramterType="int">
select h.id hid,h.name hname,h.age hage,w.id wid,w.name wname,w.age wage
from husband h,wife w
where h.id=#{id} and h.wife_id=w.id
</select>
嵌套查询(一条sql语句查询一张表,一张表查出来的某一列作为查询另一张表的条件。不用起别名,因为一张表对应一个结果集)
<resultMap id="WifeResult" type="wife">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
<select id="findWife" paramterType="int" resultMap="WifeResult">
select id,name,age
from wife
where id=#{id}
</select>
<resultMap id="HusbandResult" type="husband">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="wife" column="wife_id" select="findWife"/>
</resultMap>
<select id="findHusbandWithWifeById" resultMap="HusbandResult" paramterType="int">
select id,name,age,wife_id
from husband
where id=#{id}
</select>
插入数据到数据库中:
<insert id="insertWife" paramterType="wife">
isnert
into wife(id,name,age)
values(#{id},#{name},#{age})
</insert>
映射接口
public interface HusbandMapper{
public List<Husband> findHusbandWithWifeById(Integer id);
public void insertWife(Wife wife);
}
测试类
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
Mapper mapper = session.getMapper(HusbandMapper.class);//接口类
mapper.insertWife(wife);
List<Husband> list = mapper.findHusbandWithWifeById(1);
session.close();