一对一和多对一配置一样,这里就放到一起。

1.配置文件跟上一章一样,这里就不多写了,主要是Mapper映射文件

接口

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
}

接口

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

2.SSM整合_多表_一对一或多对一的增删改查的更多相关文章

  1. 4.SSM整合_多表_多对多的增删改查

    多对多关系,课程和学生 接口 public interface CourseMapper { /** * 获取所有课程 * @return * @throws Exception */ public ...

  2. Python全栈开发记录_第二篇(文件操作及三级菜单栏增删改查)

    python3文件读写操作(本篇代码大约100行) f = open(xxx.txt, "r", encoding="utf-8") 不写“r”(只读)默认是只 ...

  3. MongoDB 表(集合) 创建删除、数据增删改查

    MongoDB 表(集合) 创建删除和增删改查数据 创建一个集合(emp) 在创建集合之前先使用use xxx,选择数据库,如果没有会创建(并不是真正的创建,只有在数据库里面保存集合数据之后才能够真正 ...

  4. 1.SSM整合_单表的增删改查

    目标:增删改查 环境:Maven+Eclipse+Tomcat7+JDK7 思维导图: 表结构 目录结构 依赖 <dependencies> <dependency> < ...

  5. SSM整合_年轻人的第一个增删改查_基础环境搭建

    写在前面 SSM整合_年轻人的第一个增删改查_基础环境搭建 SSM整合_年轻人的第一个增删改查_查找 SSM整合_年轻人的第一个增删改查_新增 SSM整合_年轻人的第一个增删改查_修改 SSM整合_年 ...

  6. SSM整合_年轻人的第一个增删改查_查找

    写在前面 SSM整合_年轻人的第一个增删改查_基础环境搭建 SSM整合_年轻人的第一个增删改查_查找 SSM整合_年轻人的第一个增删改查_新增 SSM整合_年轻人的第一个增删改查_修改 SSM整合_年 ...

  7. SSM整合_年轻人的第一个增删改查_新增

    写在前面 SSM整合_年轻人的第一个增删改查_基础环境搭建 SSM整合_年轻人的第一个增删改查_查找 SSM整合_年轻人的第一个增删改查_新增 SSM整合_年轻人的第一个增删改查_修改 SSM整合_年 ...

  8. Oracle学习总结_day01_day02_表的创建_增删改查_约束

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! 更新: SELECT * FROM (SELECT R ...

  9. 论坛模块_版块管理_增删改查&实现上下移动

    论坛模块_版块管理1_增删改查 设计实体Forum.java public class Forum { private Long id; private String name; private St ...

随机推荐

  1. 读spring源码(三)-ClassPathXmlApplicationContext-getBean

    这次主要看了下bean的生成过程,发现个画时序图很好用的软件plantuml,充分发挥程序员的能力,能用代码解决的别叨叨别的

  2. 51nod 2512

    看错题目!!啊啊啊,都说了不能有前导,我怎么这么想当然呢!!另外1也是2的幂次方 代码: #include<iostream> #include<cstdio> #includ ...

  3. selenium-webdriver循环点击百度搜索结果以及获取新页面的handler

    webdriver还是很有意思的,之前用过Ruby的watir的自动化测试框架,感觉selenium的这套框架更好一些,很容易就可以上手.我虽然不做自动化这块,不过先玩玩再说,多学点东西总之还是好一些 ...

  4. Unity AssetBundle的生成、加载和热更新

    当前使用的是unity2018.2.6版本. 生成AssetBundle 这个版本生成AssetBundle有两种方式,一种是在资源的Inspector面板下边配置AssetBundle名称,然后调用 ...

  5. nginx的location配置详解

    语法规则: location [=|~|~*|^~] /uri/ { … } =开头表示精确匹配 ^~开头表示uri以某个常规字符串开头,理解为匹配url路径即可.nginx不对url做编码,因此请求 ...

  6. [Linux]标准IO全缓冲和行缓冲

    概述 标准IO中,标准错误是不带缓冲的.若是指向终端设备的流才是行缓冲的,否则是全缓冲的. 行缓冲也可以分配缓冲区,当遇到超大行(超过缓冲区的行),缓冲区内容也会优先刷出. 示例 #include & ...

  7. computed计算属性

    在computed中,可以定义一些属性,这些属性 叫做计算属性.计算属性的本质是一个方法,只不过我们在使用的时候,把他们的名称当做属性来使用,并不会吧计算属性当做方法去调用.与methods平级. / ...

  8. (详细)华为P8 GRA-UL00的Usb调试模式在哪里开启的方法

    经常我们使用Pc通过数据线连接上安卓手机的时候,如果手机没有开启usb开发者调试模式,Pc则没能够成功检测到我们的手机,有时候我们使用的一些功能较强的应用软件好比之前我们使用的一个应用软件引号精灵,老 ...

  9. 解决svn更新项目目录时“Error:svn: E155037: Previous operation has not finished; run 'cleanup' if it was interrupted”的报错问题

    今天在IDEA更新项目目录时,发现报错“Error:svn: E155037: Previous operation has not finished; run 'cleanup' if it was ...

  10. java web项目最简单的结构

    为了解“徒手”建立一个web应用,此博客建立简单过程 1.在任意一个目录下,建立一个文件夹,取名字 webDemo .这个应用名字. 2.在 webDemo 内建立一个 WEB-INF 文件夹,此处大 ...