3.SSM整合_多表_一对多的增删改查
1.配置文件跟上一章一样,这里就不多写了,主要是Mapper映射文件,一对多反过来就是多对一
一
接口
public interface CategoryMapper {
public void addCategory(Category category);
public void delCategory(String id);
public void updateCategory(Category cateory);
//根据name模糊查询
public List<Category> selectCategory(String name);
//查询所有
public List<Category> selectAll();
//根据id查询
public Category selectById(String id);
//查询分类下的所有信息
public List<News> getNewsWithCate(String id);
}
映射文件
<mapper namespace="com.demo1.mapper.CategoryMapper">
<!-- 添加 -->
<insert id="addCategory" parameterType="Category">
insert into t_category(name, createtime)
value(#{name}, #{createtime});
</insert>
<!-- 删除 -->
<delete id="delCategory" parameterType="String">
delete from t_category
where id = #{id}
</delete>
<!-- 更新 -->
<update id="updateCategory" parameterType="Category">
update t_category
set name = #{name}, createtime = #{createtime}
where id = #{id}
</update>
<!-- 查询:一对多,多表查询 -->
<!-- 方式一:嵌套结果,一条SQL多表连表查询,所有的字段都做映射 -->
<resultMap type="Category" id="categoryResultMap">
<id column="Id" property="id"/>
<result column="Name" property="name"/>
<result column="Createtime" jdbcType="TIMESTAMP" property="createtime" javaType="String"/>
<!-- 没有外键字段 -->
<collection property="news" javaType="ArrayList" ofType="News">
<id column="NewId" property="id"/>
<result column="Title" property="title"/>
<result column="Author" property="author"/>
</collection>
</resultMap>
<!-- 根据name模糊查询 -->
<select id="selectCategory" parameterType="String" resultMap="categoryResultMap">
select t1.id Id, t1.name Name, t1.createtime Createtime,
t2.id NewId, t2.title Title, t2.author Author
from t_category t1
left join t_news t2
on t1.id = t2.category_id
where t1.name like "%"#{name}"%"
</select>
<!-- 查询所有 -->
<select id="selectAll" resultMap="categoryResultMap">
select t1.id Id, t1.name Name, t1.createtime Createtime,
t2.id NewId, t2.title Title, t2.author Author
from t_category t1
left join t_news t2
on t1.id = t2.category_id
</select>
<!-- 方式二:嵌套查询,两条SQL,单独查询 -->
<resultMap type="Category" id="categoryResultMap2">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="createtime" jdbcType="TIMESTAMP" property="createtime" javaType="String"/>
<!-- 这个column属性不是外键,是分类的主键,其值为id为selectById的select标签查询出的id字段 -->
<collection column="id" property="news" javaType="ArrayList" ofType="News" select="getNews">
</collection>
</resultMap>
<select id="getNews" parameterType="int" resultType="News">
select id, title, author
from t_news
where category_id = #{id}
</select>
<!-- 根据id查询 -->
<select id="selectById" parameterType="String" resultMap="categoryResultMap2">
select id, name ,createtime
from t_category
where id = #{id}
</select>
<!-- 查询分类下的所有信息 -->
<select id="getNewsWithCate" resultMap="categoryResultMap2">
select *
from t_news
where category_id = #{id}
</select>
</mapper>
实体类
public class Category {
private Integer id;
private String name;
private String createtime;
//一对多关联
private List<News> news;
//省略getter和setter
多
接口
public interface NewsMapper {
public void addNew(News news);
public void delNew(String id);
public void updateNew(News news);
//模糊查询
public List<News> selectNew(String name);
//查询所有
public List<News> selectAll();
//根据id查询
public News selectById(String id);
//获取分类的名称
public List<String> getCategoryName();
}
映射文件
<mapper namespace="com.demo1.mapper.NewsMapper">
<!-- 添加操作和单表添加差不多,唯一的区别就是外键字段 -->
<insert id="addNew" parameterType="News" >
insert into t_news(content, title, author, createtime, category_id)
value(#{content},#{title},#{author},#{createtime},#{category.id})
</insert>
<delete id="delNew" parameterType="String">
delete from t_news where id = #{id}
</delete>
<!-- 修改操作和单表修改差不多,唯一的区别就是外键字段 -->
<update id="updateNew" parameterType="News">
update t_news
set
content = #{content},
title = #{title},
author = #{author},
createtime = #{createtime},
category_id = #{category.id}
where
id = #{id}
</update>
<!-- 多对一(配置方式和一对一一样)多表查询 -->
<!-- 查询有两种方式, -->
<!--方式一嵌套结果: 就是把所有的字段都映射,一条SQL连表查询,其中外键使用 <association>标签映射-->
<resultMap type="News" id="newsResultMap">
<!-- property 表示bean中的属性; column 表示表中的列别名 -->
<id column="Id" property="id"/>
<!-- property 表示bean中的属性; column 表示表中的列别名 -->
<result column="Content" property="content"/>
<!-- property 表示bean中的属性; column 表示表中的列别名 -->
<result column="Title" property="title"/>
<!-- property 表示bean中的属性; column 表示表中的列别名 -->
<result column="Author" property="author"/>
<!-- javaType的属性值设置为String是为了显示成:2018-12-5 12:30:10这样 -->
<result column="Createtime" jdbcType="TIMESTAMP" property="createtime" javaType="String"/>
<!-- 映射外键字段,该标签的column属性值为表外键字段列名,而非列别名 -->
<association column="category_id" property="category" jdbcType="INTEGER" javaType="Category">
<!-- property 表示bean中的属性; column 表示表中的列别名 -->
<id column="Categoryid" property="id"/>
<!-- property 表示bean中的属性; column 表示表中的列别名 -->
<result column="Name" property="name"/>
<!-- 不管SQL语句中有没有查询某字段,如果别名同名,mySQL会自动在别名后加上序号,从1开始。
就会导致同时同名的字段会映射同一个数据,
比如:这里的column和上面的column中同名了,那这里的property属性值会和上面的property属性值一样
-->
<!-- <result column="Createtime" jdbcType="TIMESTAMP" property="createtime" javaType="String"/> -->
</association>
</resultMap>
<!-- 根据name模糊查询,左链接查询,若外键无值,则字段为空 -->
<select id="selectNew" parameterType="String" resultMap="newsResultMap">
select t1.id Id, t1.content Content, t1.title Title, t1.author Author, t1.createtime Createtime,
t2.id Categoryid, t2.name Name
from t_news t1
left join t_category t2
on t1.category_id = t2.id
where t1.title like "%"#{name}"%"
</select>
<!-- 查询所有 -->
<select id="selectAll" resultMap="newsResultMap">
<!-- 连表查询:若外键无值,则整条记录去掉 -->
<!-- select t1.id, t1.content, t1.title, t1.author, t1.createtime, t2.name from t_news t1, t_category t2 where t1.category_id = t2.id -->
<!-- 左链接查询,若外键无值,则字段为空 -->
select t1.id Id, t1.content Content, t1.title Title, t1.author Author, t1.createtime Createtime,
t2.id Categoryid, t2.name Name
from t_news t1
left join t_category t2
on t1.category_id = t2.id
</select>
<!-- 方式二:嵌套查询,使用两条SQL执行查询,两条SQL单独查询 -->
<resultMap type="News" id="newsResultMap2">
<!-- property 表示bean中的属性; column 表示表中的列别名 -->
<id column="Id" property="id"/>
<!-- property 表示bean中的属性; column 表示表中的列别名 -->
<result column="Content" property="content"/>
<!-- property 表示bean中的属性; column 表示表中的列别名 -->
<result column="Title" property="title"/>
<!-- property 表示bean中的属性; column 表示表中的列别名 -->
<result column="Author" property="author"/>
<!-- javaType的属性值设置为String是为了显示成:2018-12-5 12:30:10这样 -->
<result column="Createtime" jdbcType="TIMESTAMP" property="createtime" javaType="String"/>
<!-- 映射外键字段,该标签的column属性值为表外键字段列名,而非列别名,其值为ID为selectById的select标签查询出的category_id字段 -->
<association column="category_id" property="category" jdbcType="INTEGER" javaType="Category" select="getCategory">
</association>
</resultMap>
<select id="getCategory" parameterType="int" resultType="Category">
select id, name
from t_category
where id = #{id}
</select>
<!-- 根据id查询 :修改时调用
查询字段:t_news表:id,content,title,author,createtime
t_category表:id,name
-->
<select id="selectById" parameterType="String" resultMap="newsResultMap2">
select t1.id Id, t1.content Content, t1.title Title, t1.author Author, t1.createtime Createtime, t1.category_id category_id
from t_news t1
where t1.id = #{id}
</select>
<!-- 查询分类的name -->
<select id="getCategoryName" resultType="Category">
select id, name
from t_category
</select>
</mapper>
实体类
public class News {
private Integer id;
private String content;
private String title;
private String createtime;
private Category category;//外键
private String author;
//省略getter和setter
}
3.SSM整合_多表_一对多的增删改查的更多相关文章
- 第二百七十七节,MySQL数据库-数据表、以及列的增删改查
MySQL数据库-数据表.以及列的增删改查 1.创建一个表 CREATE(创建) TABLE(表) ENGINE(引擎) ENGINE=INNODB(引擎)还有很多类引擎,这里只是简单的提一下INNO ...
- Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查
之前介绍了Spring Boot 整合mybatis 使用xml配置的方式实现增删改查,还介绍了自定义mapper 实现复杂多表关联查询.虽然目前 mybatis 使用xml 配置的方式 已经极大减轻 ...
- Django中ORM表的创建以及基本增删改查
Django作为重量级的Python web框架,在做项目时肯定少不了与数据库打交道,编程人员对数据库的语法简单的还行,但过多的数据库语句不是编程人员的重点对象.因此用ORM来操作数据库相当快捷.今天 ...
- SSMybatis整合 --详细解读Mybatis对oracle数据库进行增删改查(一)
Mybatis是现在主流的持久化层框架,与Hibernate不同的是,它鼓励程序员使用原声SQL语句对数据库进行操作.因此提供了非常灵活的功能.特别是当数据库同时访问数过多,需要进行优化时,使用sql ...
- IDEA对数据库、表、记录的(增删改查可视化操作)、数据库安全性问题的演示
对数据库的增删改查 新增数据库 修改数据库 删除数据库 对表的增删改查 新增表 修改表 删除表 对记录的增删改查 数据库安全性问题的演示 演示脏读 一个事物里面读到了另外一个事物没有提交的数据: ...
- 【hbase】——Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...
- (转)Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...
- Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...
- MongoDB 数据库创建删除、表创建删除、数据增删改查
一.管理 mongodb 数据库:mongo 查看所有数据库列 表 show dbs 二. 创建数据库 创建 数据库 use student 如果真的想把这个数据库创建成功,(collections) ...
随机推荐
- 2018-2019-2 网络对抗技术 20165337 Exp3 免杀原理与实践
基础问题回答 (1)杀软是如何检测出恶意代码的? 基于特征码的检测:特征码就是一段数据.如果一个可执行文件(或其他运行的库.脚本等)包含特定的数据则被认为是恶意代码.AV软件厂商要做的就是尽量搜集最全 ...
- mybatis一对多查询之collection的用法
首先看一下返回的数据的格式: //获取端子信息List<Map<String, Object>> portList = doneTaskDao.queryTroubleTask ...
- iView -- TimePicker 自定义修改时间选择器选择时间面板样式
iView官方组件展示效果: 期望的最终效果: 为什么要修改期望效果? 项目需要只选择小时,分钟跟秒的不需要,而官方并没有直接相关的小时组件或者是设置显示成小时或分钟或秒的时间选择器,因为自己直接修改 ...
- Laravel-google-authenticator--Google验证码
开发前的准备 安装Laravel 安装二维码生成器QrCode,没有安装也可以,接下来会安装 安装拓展 1.运行如下代码安装拓展包: composer require "earnp/lara ...
- 实验1:C++简单程序设计(1)
实验目的 1. 掌握c++中类c部分的编程知识: 数据类型,常量,变量,运算符,表达式,分支结构,循环结构 2. 掌握C++中数据输入和输出的基本方法 3. 熟练使用c++程序开发环境,掌握c++程序 ...
- C# 解压缩工具类GZip
using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using ...
- spring+myBatis 配置多数据源,切换数据源
注:本文来源于 tianzhiwuqis <spring+myBatis 配置多数据源,切换数据源> 一个项目里一般情况下只会使用到一个数据库,但有的需求是要显示其他数据库的内容,像这样 ...
- CF1029A Many Equal Substrings
题目描述 你有一个字符串t,它由n个字母组成. 定义一个字符串s的子串为s[l...r],表示从位置l到r构成的一个新的串. 你的目标是构造一个字符串s,使得它的可能长度最小,要求s中存在k个位置i, ...
- 依赖背包——cf855C好题
比较裸的依赖背包,但是想状态还是想了好久 转移时由于边界问题,虽然可以倒序转移,但当容量为0|1的时候,由于有初始值的存在 很难再原dp数组上进行修改,所以额外用tmp数组来保存修改后的值 #incl ...
- Python之MySQL基础
一.存储引擎 1.1 什么是存储引擎 MySQL中的数据通过不同的技术存储再文件或者内存中,每种技术有不同的存储机制,索引技巧,锁定水平,并且提供不同的能力,而实现这些技术的我们就称之为存储引擎 1 ...