发表于: 2019-12-30 21:03:59

2 1169


啥也不说就是干!!!

今天完成的事情:

1、开发剩余接口
因为基本上都是些增删改查的类似的东西,这里就挑一些代表性的接口来说一下

查询目录分类,因为涉及到二级分类,所以需要联表查询(自关联)

先看 sql 语句

<select id="queryAllCategory" resultMap="BaseResultMap">
select
c1.id,c1.category_name,c1.parent_id,
c2.id se_id,c2.category_name se_category_name,c2.parent_id c2_parent_id,c2.create_time se_create_time,c2.update_time se_update_time
from category c1
left join category c2 on c2.parent_id=c1.id;
</select>

需要注意这里要给关联表查出来的字段起别名,以便 ResultMap 中映射到实体类中

mybatis 嵌套查询 <collection>标签

<mapper namespace="com.gerry.jnshu.mapper.CategoryInfoMapper">
 <resultMap id="BaseResultMap" type="com.gerry.jnshu.pojo.CategoryInfo">
   <!--
     WARNING - @mbg.generated
   -->
   <id column="id" javaType="INTEGER" property="id"/>
   <result column="category_name" jdbcType="VARCHAR" property="categoryName" />
   <result column="parent_id" jdbcType="INTEGER" property="parentId" />
   <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
   <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
   <collection property="seCategories" ofType="com.gerry.jnshu.pojo.CategoryInfo" javaType="java.util.List">
     <id column="se_id" property="id"/>
     <result column="se_category_name" property="categoryName"/>
     <result column="se_parent_id" property="parentId"/>
     <result column="se_create_time" property="createTime"/>
     <result column="se_update_time" property="updateTime"/>
   </collection>

 </resultMap>

当删除目录分类时候,需要判断该目录分类下的子分类,递归删除

@Override
public int deleteCategoryInfo(Integer id) {
List<CategoryInfo> categoryInfo = categoryInfoMapper.queryCategoryById(id).getSeCategories();
   int result= -1;
   if(categoryInfo.size()>0){
for (CategoryInfo info : categoryInfo) {
deleteCategoryInfo(info.getId());
       }
result = categoryInfoMapper.deleteByPrimaryKey(id);
   }
else{
result = categoryInfoMapper.deleteByPrimaryKey(id);
   }

return result;
}

删除评论的方法

public int deleteCommentInfo(Integer id) {
commentMapper.deleteByPrimaryKey(id);

   Example example = new Example(Reply.class);

   Example.Criteria criteria = example.createCriteria();
   criteria.andEqualTo("commentId",id);
   int rowId = replyMapper.deleteByExample(example);
   return rowId;
}

由于删除评论也要级联删除评论下的回复,所以需要用到事务相关的知识

2、事务相关知识学习

Spring 事务管理分为编程式事务管理和声明式事务管理两种方式,编程式事务管理指通过编码的方式实现事务管理,声明式事务基于 AOP ,将业务逻辑与事务处理解耦,声明式事务对代码侵入少,在实际应用中比较广泛

Spring 声明式事务,其本质是在目标方法执行前进行拦截,在方法开始前创建一个事务,在执行完方法后根据执行情况提交或回滚事务。实现方式有四种,最常用的是 


1、基于 tx 标签的 XML 文件配置

<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource"/>
</bean>

<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">

   <tx:attributes>

       <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
   </tx:attributes>
</tx:advice>
<!--配置切面-->
<aop:config>
   <!--配置切入点-->
   <aop:pointcut id="pointcut" expression="execution (* com.gerry.jnshu.service.*Impl.*(..))"/>
   <!--配置切面-->
   <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>

2、基于注解 @Transactional

除了基于命名空间的事务配置方式外,Spring 还有基于注解的方式,@Transactional 可以作用于接口、接口方法、类和类方法上,当用作类时候,该类的所有 public 方法都会事务属性,可被方法级事务覆盖

<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

@Transactional注解应该被应用到public方法上,这是由AOP的本质决定的,如果应用在protected、private的方法上,事务将被忽略

@Transactional(propagation = Propagation.REQUIRED)
public int deleteArticle(Integer id) {
int rowIds= articleMapper.deleteByPrimaryKey(id);
   Example example = new Example(ImageInfo.class);
   Example.Criteria criteria = example.createCriteria();
   criteria.andEqualTo("relationId",id);
   imageInfoMapper.deleteByExample(example);
   return rowIds;
}

@Transaction 注解的完整属性信息如下:

两种配置方式各有优缺点:基于命名空间的方式一个配置可以匹配多个方法,但配置较注解方式复杂;基于注解的方式需要在每个需要使用事务的方法或类上标注,但基于标注的方法学习成本更低。

明天计划的事情:

1、Nginx 配置图片访问

2、填充数据

3、深度思考的相关问题


遇到的问题:

收获:

1、springmvc HTTP PUT请求接收不到 Form表单中的参数,添加  HttpPutFormContentFilter 过滤器

<filter>
   <filter-name>httpPutFormFilter</filter-name>
   <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
   <filter-name>httpPutFormFilter</filter-name>
   <servlet-name>springmvc</servlet-name>
</filter-mapping>



返回列表 返回列表
评论

    分享到