基于SSM的简易博客项目02

这篇博客详细记录了使用Spring、SpringMVC和MyBatis搭建简易博客项目的步骤,包括创建DAO、Service、Controller层,配置文件的编写,以及前端资源的导入和Spring配置。在Service层中添加了事务管理,并通过接口注入实现业务逻辑。最后,概述了项目启动流程和配置注意事项。
摘要由CSDN通过智能技术生成

这是小菜鸟做的课后笔记,如有不足,望指点,接着01往下的步骤

创建与dao建立联系的配置文件

上一篇博客介绍到使用map映射文件实现BlogDao接口中的两个查询方法,BlogDao接口中还有一个插入博客的方法,在blogMapper.xml中添加以下语句实现添加博客的操作:

 <!--添加博客    public void insertBlog(Blog blog)
    输入参数类型 ———— Blog实体bean
    输出类型:无-->
    <insert id="insertBlog" parameterType="com.zte.blog.entity.Blog" >
        insert
        into
                t_blog
                (title,content,create_date)
        values
        <!--占位符(?,?,?),在mybaits中是以下表现方式-->
                (#{title},#{content},#{create_date})

    </insert>

现在BlogDao接口中的方法就都映射完成了,还有一个CommentDao接口,我们也需要创建一个Map配置文件与其连接,实现接口中的方法。
在资源目录下创建一个commentMapper.xml文件来与CommentDao接口连接,实现接口 中的方法
在这里插入图片描述
在commentmapper中写的内容与blogMapper.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.zte.blog.dao.CommentDao">
<!--       添加评论
    public void insertComment(Comment comment);-->
    <insert id="insertComment" parameterType="com.zte.blog.entity.Comment">
        insert
        into
             t_comment
            (content,comment_date,blog_id)
        values
        <!--#{这里的值对应Comment实体bean中的值}-->
        <!--注意#{}中只能是基本类型,这里的blog_id是外键对应的是blog表中的id值
        因此,blog_id对应的是blog.id (找到当前对象blog,在找到对象的属性id)-->
            (#{content},#{comment_date},#{blog.id})

    </insert>

<!--          查询前num 条评论
    public List<Comment> selectTop(Integer num);-->
    <select id="selectTop" parameterType="int" resultType="com.zte.blog.entity.Comment" >
        select
        c.id,c.content
        from
        t_comment c
        order by c.comment_date desc       -- 根据时间倒排
        limit 0,#{num};             -- 显示前num条评论
    </select>
    


</mapper>

此时Map配置文件就完全配置好了。
注意:
1.保证命名空间、方法id、输入参数、输出参数能够与相关类、方法、数据类型相对应
2.保证#{}中的值与实体类相对应

创建Service层

在com.zte.blog下创建一个service包,用来存放service层的代码。
创建两个service接口类:BlogService.java和CommentService.java,用来存放Service层对实体层的操作。在service包下创建impl包用来存放接口的实现类。
在这里插入图片描述

由于这个项目比较简单实现的操作与Dao层对实体层的操作相同,为了区分两类的操作,Dao中的查询语句使用Select,Service层中查询语句使用find;Dao中的添加语句使用insert,Service层中使用add。
BlogService.java

public interface BlogService {
   
//    查询所有博客
    public List<BlogVO> findAll();

    //    根据id查找某个博客
    public Blog findBlogById(Integer id);

    //    添加博客
    public void addBlog(Blog blog);
}

CommentService.java:

public interface CommentService {
   
    //    添加评论 注意:service中添加使用addXXX
    public void addComment(Comment comment);

    //    查询前num 条评论 注意:service中查找记录使用find
    public List<Comment> findTop(Integer num);
}

创建接口的实现类,在实现类中与dao不同的是,service层中的实现类要加上事务,由于功能与dao中的相同,就通过注入dao来实现service层接口。
BlogServiceImpl.java:

/*service 层绑定了事务调用Dao层*/
//service注解
@Service
//添加事务注解,该类中的每一个方法都适用于这个事务
//propagation ---> 添加默认隔离级别
//rollbackFor ---> 回滚机制:报错才回滚
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class BlogServiceImpl implements BlogService {
   
//    注入BlogDao
    @Autowired
    private BlogDao blogDao;
    @Override
//    查询的时候不使用上面定义的事务,使用只读事务
    @Transactional(readOnly = true)
    public List<BlogVO> findAll() {
   
        return blogDao.selectAll();
    }

    @Override
    public Blog findBlogById(Integer id) {
   
        return blogDao.selectBlogById(id);
    }

    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值