32.myBatis实现一对一和一对多和多对多
来源:互联网 发布:网络防火墙导致不上网 编辑:程序博客网 时间:2024/05/16 01:08
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();
1 0
- Mybatis一对一和一对多
- 32.myBatis实现一对一和一对多和多对多
- Mybatis关联查询一对一和一对多的实现
- Mybatis关联查询一对一和一对多的实现
- Mybatis关联查询一对一和一对多的实现
- Mybatis关联查询一对一和一对多的实现
- Mybatis关联查询一对一和一对多的实现
- Mybatis关联查询一对一和一对多的实现
- Mybatis一对一和一对多配置
- Mybatis学习----一对一和一对多
- Mybatis的一对一和一对多
- myBatis 一对一 和一对多映射
- 转载MyBatis的一对一和一对多
- MyBatis 一对一、一对多
- mybatis 一对一,一对多
- ibatis 实现一对一和一对多
- ibatis 实现一对一和一对多(1)
- ibatis 实现一对一和一对多(2)
- kubernetes容器编排系统介绍
- 不同平台(mac、windows—wpf、web、ios、android)下实现页面的转场动画
- Android网络编程之使用HttpClient和MultipartEntityBuilder 批量同时上传文件和文字
- [SQL SERVER 2016]ABF文件的还原
- caffe 参数介绍
- 32.myBatis实现一对一和一对多和多对多
- SVGKit使用重要操作
- static关键字
- 第七周 oj 1030 求奖金总数
- View的绘制过程
- 安装pyqt4
- Android 程序框架设计
- linux子系统的初始化_subsys_initcall():那些入口函数
- Spring MVC controller间跳转 重定向 传参