一 .编写项目流程:
1.确定需求-----确定要实现的功能
2.画E-R图,设计数据库
3.确定项目框架:定义接口,定义工具类
4.编写代码
4.1.级联删除步骤:
(1) .在bean包下新建一个文件夹ex包,作为某一个bean对象的扩展类.
(2) .在src/main/resource/mapper包新建一个文件夹ex,为自定义的扩展类添加对应的XXXEXMapper.xml文件,
(3) com.example.demo.mapper包中新建一个ex扩展包,在包中定义好所有自定义的扩展类所对应的接口文件
注:为了在重建mybatis逆向工程的时候可以保存好自定义的类,所以一般情况下都把自定义的类放到ex包下,防止其被覆盖.
CategoryExMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.ex.CategoryExMapper">
<resultMap id="BaseResultMap" type="com.example.demo.bean.ex.CategoryEx">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="code" jdbcType="BIGINT" property="code" />
<result column="name" jdbcType="VARCHAR" property="name" />
<collection property="articles" ofType="com.example.demo.bean.Article"
column="id" select="selectAllArticleByCategoryId" >
</collection>
</resultMap>
<select id="findAllCategoryEx" resultMap="BaseResultMap">
select id,name,code from cms_category
</select>
<select id="findCategoryExById" resultMap="BaseResultMap" >
select * from cms_category where id=#{id}
</select>
<select id="selectAllArticleByCategoryId" resultType="com.example.demo.bean.Article">
select * from cms_article where category_id=#{id}
</select>
</mapper>
CategoryExMapper.java
public interface CategoryExMapper {
/**
* 实现查询所有栏目及其包含的文章信息
* */
List<CategoryEx> findAllCategoryEx() throws CustomerException;
/**
* 实现根据id查询对应的栏目及文章信息
* */
CategoryEx findCategoryExById(Integer id) throws CustomerException;
}