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
}