ssm(springmvc4+spring4+mybatis3)整合实战-个人博客系统-dao与service层等组件整合

ssm(springmvc4+spring4+mybatis3)整合实战-个人博客系统-dao与service层等组件整合

    这一篇博客,我把mybatis逆向工程生成的dao与service层分享出来,另外,我在第一篇博客时贴出的web.xml中配置了一个InitComponent,即初始化组件:

 

<!-- 监听器:初始化组件 把博主信息、根据博客类别分类信息、根据日期归档分类信息 存放到application中,用以提供页面请求性能 -->
	<listener>
		<listener-class>com.steadyjack.service.impl.InitComponent</listener-class>
	</listener>

    这个组件的作用在于启动时获取系统首页所需要的各个model的信息,加快首页index.html的访问速度。下面先贴出这个组件的源码吧:

 

    在package com.steadyjack.service.impl 下建立 InitComponent.java类:

 

package com.steadyjack.service.impl;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.steadyjack.entity.Blog;
import com.steadyjack.entity.BlogType;
import com.steadyjack.entity.Blogger;
import com.steadyjack.entity.Link;
import com.steadyjack.service.BlogService;
import com.steadyjack.service.BlogTypeService;
import com.steadyjack.service.BloggerService;
import com.steadyjack.service.LinkService;

/**
 * title:InitComponent.java
 * description:初始化组件 把博主信息 根据博客类别分类信息 根据日期归档分类信息 
 * 		                   存放到application中,用以提供页面请求性能
 * time:2017年1月16日 下午10:36:50
 * author:debug-steadyjack
 */
@Component
public class InitComponent implements ServletContextListener,ApplicationContextAware{

	//其实这个就是spring的IOC容器-spring上下文应用程序
	private static ApplicationContext applicationContext;
	
	@SuppressWarnings("static-access")
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
	}

	public void contextInitialized(ServletContextEvent servletContextEvent) {
		//application:整个应用的上下文应用程序,可以往此对象中绑定一些经常需要访问的对象
		ServletContext application=servletContextEvent.getServletContext();
		
		//Spring IOC容器中获取各个bean
		BloggerService bloggerService=(BloggerService) applicationContext.getBean("bloggerService");
		Blogger blogger=bloggerService.find(); // 查询博主信息
		blogger.setPassword(null);
		application.setAttribute("blogger", blogger);
		
		BlogTypeService blogTypeService=(BlogTypeService) applicationContext.getBean("blogTypeService");
		List<BlogType> blogTypeCountList=blogTypeService.countList(); // 查询博客类别以及博客的数量
		application.setAttribute("blogTypeCountList", blogTypeCountList);
		
		BlogService blogService=(BlogService) applicationContext.getBean("blogService");
		List<Blog> blogCountList=blogService.countList(); // 根据日期分组查询博客
		application.setAttribute("blogCountList", blogCountList);
		
		LinkService linkService=(LinkService) applicationContext.getBean("linkService");
		List<Link> linkList=linkService.list(null); // 查询所有的友情链接信息
		application.setAttribute("linkList", linkList);
		
		String ctx=application.getRealPath("/");
		System.out.println("根路径: "+ctx);
		application.setAttribute("ctx", ctx);
	}

	public void contextDestroyed(ServletContextEvent sce) {
	}

}


     然后,会发现这个初始化组件InitComponent.java用到了ioc容器的各个service组件,故而,下面建立service层各个组件:

 

     在package com.steadyjack.service下建立BloggerService.java,BlogService.java,BlogTypeService.java,CommentService.java,LinkService.java各个interface(接口类),源码如下:

     BloggerService.java接口类:

 

package com.steadyjack.service;

import com.steadyjack.entity.Blogger;

/**
 * title:BloggerService.java
 * description:博主Service接口
 * time:2017年1月16日 下午10:00:20
 * author:debug-steadyjack
 */
public interface BloggerService {

	/**
	 * 查询博主信息
	 * @return
	 */
	public Blogger find();
	
	/**
	 * 通过用户名查询用户
	 * @param userName
	 * @return
	 */
	public Blogger getByUserName(String userName);
	
	/**
	 * 更新博主信息
	 * @param blogger
	 * @return
	 */
	public Integer update(Blogger blogger);
}

    BlogService.jave接口类:

 

 

package com.steadyjack.service;

import java.util.List;
import java.util.Map;

import com.steadyjack.entity.Blog;

/**
 * title:BlogService.java
 * description:博客Service接口
 * time:2017年1月16日 下午10:35:32
 * author:debug-steadyjack
 */
public interface BlogService {

	/**
	 * 根据日期月份分组查询
	 * @return
	 */
	public List<Blog> countList(); 
	
	/**
	 * 分页查询博客
	 * @return
	 */
	public List<Blog> list(Map<String,Object> map);
	
	/**
	 * 获取总记录数
	 * @param map
	 * @return
	 */
	public Long getTotal(Map<String,Object> map);
	
	/**
	 * 通过Id查找实体
	 * @param id
	 * @return
	 */
	public Blog findById(Integer id);
	
	/**
	 * 更新博客信息
	 * @param blog
	 * @return
	 */
	public Integer update(Blog blog); 
	
	/**
	 * 获取上一个博客
	 * @param id
	 * @return
	 */
	public Blog getLastBlog(Integer id);
	
	/**
	 * 获取下一个博客
	 * @param id
	 * @return
	 */
	public Blog getNextBlog(Integer id);
	
	/**
	 * 添加博客信息
	 * @param blog
	 * @return
	 */
	public Integer add(Blog blog);
	
	/**
	 * 删除博客信息
	 * @param id
	 * @return
	 */
	public Integer delete(Integer id);
	
	/**
	 * 查询指定的博客类别下的博客数量
	 * @param typeId
	 * @return
	 */
	public Integer getBlogByTypeId(Integer typeId);
}


    BlogTypeService.java接口类:

 

 

package com.steadyjack.service;

import java.util.List;
import java.util.Map;

import com.steadyjack.entity.BlogType;

/**
 * title:BlogTypeService.java
 * description:博客类型Service接口
 * time:2017年1月16日 下午10:03:06
 * author:debug-steadyjack
 */
public interface BlogTypeService {

	/**
	 * 查询所有博客类型 以及对应的博客数量
	 * @return
	 */
	public List<BlogType> countList();
	
	/**
	 * 分页查询博客类别信息
	 * @param map
	 * @return
	 */
	public List<BlogType> list(Map<String,Object> map);
	
	/**
	 * 获取总记录数
	 * @param map
	 * @return
	 */
	public Long getTotal(Map<String,Object> map);
	
	/**
	 * 添加博客类别信息
	 * @param blogType
	 * @return
	 */
	public Integer add(BlogType blogType);
	
	/**
	 * 修改博客类别信息
	 * @param blogType
	 * @return
	 */
	public Integer update(BlogType blogType);
	
	/**
	 * 删除博客类别信息
	 * @param id
	 * @return
	 */
	public Integer delete(Integer id);
}


    CommentService.java接口类:

 

 

package com.steadyjack.service;

import java.util.List;
import java.util.Map;

import com.steadyjack.entity.Comment;

/**
 * title:CommentService.java
 * description:评论Service接口
 * time:2017年1月16日 下午10:35:42
 * author:debug-steadyjack
 */
public interface CommentService {

	/**
	 * 添加评论
	 * @param comment
	 * @return
	 */
	public int add(Comment comment);
	
	/**
	 * 修改评论
	 * @param comment
	 * @return
	 */
	public int update(Comment comment);
	
	/**
	 * 查找用户评论信息
	 * @param map
	 * @return
	 */
	public List<Comment> list(Map<String,Object> map);
	
	
	/**
	 * 获取总记录数
	 * @param map
	 * @return
	 */
	public Long getTotal(Map<String,Object> map);
	
	/**
	 * 删除评论信息
	 * @param id
	 * @return
	 */
	public Integer delete(Integer id);
}


     LinkService.java接口类:

 

 

package com.steadyjack.service;

import java.util.List;
import java.util.Map;

import com.steadyjack.entity.Link;

/**
 * title:LinkService.java
 * description:友情链接Service接口
 * time:2017年1月16日 下午10:35:55
 * author:debug-steadyjack
 */
public interface LinkService {

	/**
	 * 添加友情链接
	 * @param link
	 * @return
	 */
	public int add(Link link);
	
	/**
	 * 修改友情链接
	 * @param link
	 * @return
	 */
	public int update(Link link);
	
	/**
	 * 查找友情链接信息
	 * @param map
	 * @return
	 */
	public List<Link> list(Map<String,Object> map);	
	
	/**
	 * 获取总记录数
	 * @param map
	 * @return
	 */
	public Long getTotal(Map<String,Object> map);
	
	/**
	 * 删除友情链接
	 * @param id
	 * @return
	 */
	public Integer delete(Integer id);
}


     各个service的实现类建立在package com.steadyjack.service.impl目录下,各个实现类源码如下:

 

     BloggerServiceImpl.java实现类:

 

package com.steadyjack.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.steadyjack.dao.BloggerDao;
import com.steadyjack.entity.Blogger;
import com.steadyjack.service.BloggerService;

/**
 * title:BloggerServiceImpl.java
 * description:博主Service实现类
 * time:2017年1月16日 下午10:36:10
 * author:debug-steadyjack
 */
@Service("bloggerService")
public class BloggerServiceImpl implements BloggerService{

	@Resource
	private BloggerDao bloggerDao;

	public Blogger find() {
		return bloggerDao.find();
	}

	public Blogger getByUserName(String userName) {
		return bloggerDao.getByUserName(userName);
	}

	public Integer update(Blogger blogger) {
		return bloggerDao.update(blogger);
	}
	
	
}


    BlogServiceImpl.java实现类如下:

 

 

package com.steadyjack.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.steadyjack.dao.BlogDao;
import com.steadyjack.entity.Blog;
import com.steadyjack.service.BlogService;

/**
 * title:BlogServiceImpl.java
 * description:博客Service实现类
 * time:2017年1月16日 下午10:36:23
 * author:debug-steadyjack
 */
@Service("blogService")
public class BlogServiceImpl implements BlogService{

	@Resource
	private BlogDao blogDao;
	
	public List<Blog> countList() {
		return blogDao.countList();
	}

	public List<Blog> list(Map<String, Object> map) {
		return blogDao.list(map);
	}

	public Long getTotal(Map<String, Object> map) {
		return blogDao.getTotal(map);
	}

	public Blog findById(Integer id) {
		return blogDao.findById(id);
	}

	public Integer update(Blog blog) {
		return blogDao.update(blog);
	}

	public Blog getLastBlog(Integer id) {
		return blogDao.getLastBlog(id);
	}

	public Blog getNextBlog(Integer id) {
		return blogDao.getNextBlog(id);
	}

	public Integer add(Blog blog) {
		return blogDao.add(blog);
	}

	public Integer delete(Integer id) {
		return blogDao.delete(id);
	}

	public Integer getBlogByTypeId(Integer typeId) {
		return blogDao.getBlogByTypeId(typeId);
	}
}


     BlogTypeServiceImpl.java实现类:

 

 

package com.steadyjack.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.steadyjack.dao.BlogTypeDao;
import com.steadyjack.entity.BlogType;
import com.steadyjack.service.BlogTypeService;

/**
 * title:BlogTypeServiceImpl.java
 * description:博客类型Service实现类
 * time:2017年1月16日 下午10:36:32
 * author:debug-steadyjack
 */
@Service("blogTypeService")
public class BlogTypeServiceImpl implements BlogTypeService{

	@Resource
	private BlogTypeDao blogTypeDao;
	
	public List<BlogType> countList() {
		return blogTypeDao.countList();
	}

	public List<BlogType> list(Map<String, Object> map) {
		return blogTypeDao.list(map);
	}

	public Long getTotal(Map<String, Object> map) {
		return blogTypeDao.getTotal(map);
	}

	public Integer add(BlogType blogType) {
		return blogTypeDao.add(blogType);
	}

	public Integer update(BlogType blogType) {
		return blogTypeDao.update(blogType);
	}

	public Integer delete(Integer id) {
		return blogTypeDao.delete(id);
	}

}


     CommentServiceImpl.java实现类:

 

 

package com.steadyjack.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.steadyjack.dao.CommentDao;
import com.steadyjack.entity.Comment;
import com.steadyjack.service.CommentService;

/**
 * title:CommentServiceImpl.java
 * description:评论Service实现类
 * time:2017年1月16日 下午10:36:41
 * author:debug-steadyjack
 */
@Service("commentService")
public class CommentServiceImpl implements CommentService{

	@Resource
	private CommentDao commentDao;
	
	public int add(Comment comment) {
		return commentDao.add(comment);
	}

	public List<Comment> list(Map<String, Object> map) {
		return commentDao.list(map);
	}

	public Long getTotal(Map<String, Object> map) {
		return commentDao.getTotal(map);
	}

	public Integer delete(Integer id) {
		return commentDao.delete(id);
	}

	public int update(Comment comment) {
		return commentDao.update(comment);
	}

}


     LinkServiceImpl.java实现类:

 

 

package com.steadyjack.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.steadyjack.dao.LinkDao;
import com.steadyjack.entity.Link;
import com.steadyjack.service.LinkService;

/**
 * title:LinkServiceImpl.java
 * description:友情链接Service实现类
 * time:2017年1月16日 下午10:37:15
 * author:debug-steadyjack
 */
@Service("linkService")
public class LinkServiceImpl implements LinkService{

	@Resource
	private LinkDao linkDao;
	
	public int add(Link link) {
		return linkDao.add(link);
	}

	public int update(Link link) {
		return linkDao.update(link);
	}

	public List<Link> list(Map<String, Object> map) {
		return linkDao.list(map);
	}

	public Long getTotal(Map<String, Object> map) {
		return linkDao.getTotal(map);
	}

	public Integer delete(Integer id) {
		return linkDao.delete(id);
	}

}


     同样的道理,在service.impl中的各个实现类中,自然用到了mapper类(即dao层的类):包括dao与xml配置文件,下面也贴出来。

 

     首先是package com.steadyjack.dao的各个dao层类:

     BlogDao.java:

 

package com.steadyjack.dao;

import java.util.List;
import java.util.Map;

import com.steadyjack.entity.Blog;

/**
 * title:BlogDao.java
 * description: 博客Dao接口
 * time:2017年1月16日 下午10:34:19
 * author:debug-steadyjack
 */
public interface BlogDao {

	/**
	 * 根据日期月份分组查询 - 月份-当年博客的数量
	 * @return
	 */
	public List<Blog> countList();
	
	/**
	 * 分页查询博客
	 * @return
	 */
	public List<Blog> list(Map<String,Object> map);
	
	/**
	 * 获取总记录数
	 * @param map
	 * @return
	 */
	public Long getTotal(Map<String,Object> map);
	
	/**
	 * 通过Id查找实体
	 * @param id
	 * @return
	 */
	public Blog findById(Integer id);
	
	/**
	 * 更新博客信息
	 * @param blog
	 * @return
	 */
	public Integer update(Blog blog); 
	
	/**
	 * 获取上一个博客
	 * @param id
	 * @return
	 */
	public Blog getLastBlog(Integer id);
	
	/**
	 * 获取下一个博客
	 * @param id
	 * @return
	 */
	public Blog getNextBlog(Integer id);
	
	/**
	 * 添加博客信息
	 * @param blog
	 * @return
	 */
	public Integer add(Blog blog);
	
	/**
	 * 删除博客信息
	 * @param id
	 * @return
	 */
	public Integer delete(Integer id);
	
	/**
	 * 查询指定的博客类别下的博客数量
	 * @param typeId
	 * @return
	 */
	public Integer getBlogByTypeId(Integer typeId);
	
	
}


    BloggerDao.java:

 

 

package com.steadyjack.dao;

import com.steadyjack.entity.Blogger;

/**
 * title:BloggerDao.java
 * description:博主Dao接口
 * time:2017年1月16日 下午10:16:44
 * author:debug-steadyjack
 */
public interface BloggerDao {

	/**
	 * 查询博主信息
	 * @return
	 */
	public Blogger find();
	
	/**
	 * 通过用户名查询用户
	 * @param userName
	 * @return
	 */
	public Blogger getByUserName(String userName);
	
	/**
	 * 更新博主信息
	 * @param blogger
	 * @return
	 */
	public Integer update(Blogger blogger);
}


    BlogTypeDao.java:

 

 

package com.steadyjack.dao;

import java.util.List;
import java.util.Map;

import com.steadyjack.entity.BlogType;


/**
 * title:BlogTypeDao.java
 * description:博客类型Dao接口
 * time:2017年1月16日 下午10:11:14
 * author:debug-steadyjack
 */
public interface BlogTypeDao {

	/**
	 * 查询所有博客类型 以及对应的博客数量
	 * @return
	 */
	public List<BlogType> countList();
	
	/**
	 * 通过id查询博客类型
	 * @param id
	 * @return
	 */
	public BlogType findById(Integer id);
	
	/**
	 * 分页查询博客类别信息
	 * @param map
	 * @return
	 */
	public List<BlogType> list(Map<String,Object> map);
	
	/**
	 * 获取总记录数
	 * @param map
	 * @return
	 */
	public Long getTotal(Map<String,Object> map);
	
	/**
	 * 添加博客类别信息
	 * @param blogType
	 * @return
	 */
	public Integer add(BlogType blogType);
	
	/**
	 * 修改博客类别信息
	 * @param blogType
	 * @return
	 */
	public Integer update(BlogType blogType);
	
	/**
	 * 删除博客类别信息
	 * @param id
	 * @return
	 */
	public Integer delete(Integer id);
}


    CommentDao.java:

 

 

package com.steadyjack.dao;

import java.util.List;
import java.util.Map;

import com.steadyjack.entity.Comment;

/**
 * title:CommentDao.java
 * description:评论Dao接口
 * time:2017年1月16日 下午10:28:17
 * author:debug-steadyjack
 */
public interface CommentDao {

	/**
	 * 添加评论
	 * @param comment
	 * @return
	 */
	public int add(Comment comment);
	
	/**
	 * 修改评论
	 * @param comment
	 * @return
	 */
	public int update(Comment comment);
	
	/**
	 * 查找用户评论信息
	 * @param map
	 * @return
	 */
	public List<Comment> list(Map<String,Object> map);
	
	/**
	 * 获取总记录数
	 * @param map
	 * @return
	 */
	public Long getTotal(Map<String,Object> map);
	
	/**
	 * 删除评论信息
	 * @param id
	 * @return
	 */
	public Integer delete(Integer id);
	
	
}


    LinkDao.java:

 

 

package com.steadyjack.dao;

import java.util.List;
import java.util.Map;

import com.steadyjack.entity.Link;

/**
 * title:LinkDao.java
 * description:友情链接Dao接口
 * time:2017年1月16日 下午10:31:28
 * author:debug-steadyjack
 */
public interface LinkDao {

	/**
	 * 添加友情链接
	 * @param link
	 * @return
	 */
	public int add(Link link);
	
	/**
	 * 修改友情链接
	 * @param link
	 * @return
	 */
	public int update(Link link);
	
	/**
	 * 查找友情链接信息
	 * @param map
	 * @return
	 */
	public List<Link> list(Map<String,Object> map);	
	
	/**
	 * 获取总记录数
	 * @param map
	 * @return
	 */
	public Long getTotal(Map<String,Object> map);
	
	/**
	 * 删除友情链接
	 * @param id
	 * @return
	 */
	public Integer delete(Integer id);
}


    最后是mapper配置文件,包目录为com.steadyjack.mappers。

 

    BloggerMapper.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.steadyjack.dao.BloggerDao">

	<resultMap type="Blogger" id="BloggerResult">
		<result property="id" column="id"/>
		<result property="userName" column="userName"/>
		<result property="password" column="password"/>
		<result property="nickName" column="nickName"/>
		<result property="sign" column="sign"/>
		<result property="proFile" column="proFile"/>
		<result property="imageName" column="imageName"/>
	</resultMap>
	
	<select id="find"  resultMap="BloggerResult">
		select * from tb_blogger where id=1;
	</select>
	
	<select id="getByUserName" parameterType="String" resultMap="BloggerResult">
		select * from tb_blogger where userName=#{userName}
	</select>
	
	<update id="update" parameterType="Blogger">
		update tb_blogger
		<set>
			<if test="password!=null and password!='' ">
				password=#{password},
			</if>
			<if test="nickName!=null and nickName!='' ">
				nickName=#{nickName},
			</if>
			<if test="sign!=null and sign!='' ">
				sign=#{sign},
			</if>
			<if test="proFile!=null and proFile!='' ">
				proFile=#{proFile},
			</if>
			<if test="imageName!=null and imageName!='' ">
				imageName=#{imageName},
			</if>
		</set>
		where id=1
	</update>
	
</mapper> 


    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.steadyjack.dao.BlogDao">

	<resultMap type="Blog" id="BlogResult">
		<result property="id" column="id"/>
		<result property="title" column="title"/>
		<result property="summary" column="summary"/>
		<result property="releaseDate" column="releaseDate"/>
		<result property="clickHit" column="clickHit"/>
		<result property="replyHit" column="replyHit"/>
		<result property="content" column="content"/>
		<result property="keyWord" column="keyWord"/>
		<association property="blogType" column="typeId" select="com.steadyjack.dao.BlogTypeDao.findById"></association>
	</resultMap>
	
	<select id="countList"  resultMap="BlogResult">
		SELECT DATE_FORMAT(releaseDate,'%Y年%m月') AS releaseDateStr ,COUNT(*) AS blogCount  FROM tb_blog GROUP BY DATE_FORMAT(releaseDate,'%Y年%m月') ORDER BY DATE_FORMAT(releaseDate,'%Y年%m月') DESC;
	</select>
	
	<select id="list" parameterType="Map" resultMap="BlogResult">
		select * from tb_blog
		<where>
		    <if test="title!=null and title!='' ">
				and title like #{title}
			</if>
			<if test="typeId!=null and typeId!='' ">
				and typeId = #{typeId}
			</if>
			<if test="releaseDateStr!=null and releaseDateStr!='' ">
				and DATE_FORMAT(releaseDate,'%Y年%m月') = #{releaseDateStr}
			</if>
		</where>
		order by releaseDate desc
		<if test="start!=null and size!=null">
			limit #{start},#{size}
		</if>
	</select>
	
	<select id="getTotal" parameterType="Map" resultType="Long">
		select count(*) from tb_blog
		<where>
		    <if test="title!=null and title!='' ">
				and title like #{title}
			</if>
			<if test="typeId!=null and typeId!='' ">
				and typeId = #{typeId}
			</if>
			<if test="releaseDateStr!=null and releaseDateStr!='' ">
				and DATE_FORMAT(releaseDate,'%Y年%m月') = #{releaseDateStr}
			</if>
		</where>
	</select>
	
	<select id="findById" parameterType="Integer" resultMap="BlogResult">
		select * from tb_blog where id=#{id}
	</select>
	
	<update id="update" parameterType="Blog">
		update tb_blog
		<set>
			<if test="title!=null and title!='' ">
				title=#{title},
			</if>
			<if test="summary!=null and summary!='' ">
				summary=#{summary},
			</if>
			<if test="content!=null and content!='' ">
				content=#{content},
			</if>
			<if test="blogType.id!=null ">
				typeId=#{blogType.id},
			</if>
			<if test="clickHit!=null ">
				clickHit=#{clickHit},
			</if>
			<if test="replyHit!=null ">
				replyHit=#{replyHit},
			</if>
			<if test="keyWord!=null and keyWord!='' ">
				keyWord=#{keyWord},
			</if>
		</set>
		where id=#{id}
	</update>
	
	<select id="getLastBlog" parameterType="Integer" resultMap="BlogResult">
		SELECT * FROM tb_blog WHERE id<#{id} ORDER BY id DESC LIMIT 1
	</select>
	
	<select id="getNextBlog" parameterType="Integer" resultMap="BlogResult">
		SELECT * FROM tb_blog WHERE id>#{id} ORDER BY id ASC LIMIT 1
	</select>
	
	
	<insert id="add" useGeneratedKeys="true" keyProperty="id" parameterType="Blog">
		insert into tb_blog values(null,#{title},#{summary},now(),0,0,#{content},#{blogType.id},#{keyWord})
	</insert>
	
	<delete id="delete" parameterType="Integer">
		delete from tb_blog where id=#{id}
	</delete>
	
	<select id="getBlogByTypeId" parameterType="Integer" resultType="Integer">
		select count(*) from tb_blog where typeId=#{typeId}
	</select>
</mapper> 


    BlogTypeMapper.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.steadyjack.dao.BlogTypeDao">

	<resultMap type="BlogType" id="BlogTypeResult">
		<result property="id" column="id"/>
		<result property="typeName" column="typeName"/>
		<result property="blogCount" column="blogCount"/>
		<result property="orderNo" column="orderNo"/>
	</resultMap>
	
	<select id="countList" resultMap="BlogTypeResult">
		SELECT t2.id,t2.typeName,COUNT(t1.id) AS blogCount FROM tb_blog t1 RIGHT JOIN tb_blogType t2 ON t1.typeId=t2.id GROUP BY t2.typeName order by t2.orderNo;
	</select>
	
	<select id="findById" parameterType="Integer" resultMap="BlogTypeResult">
		select * from tb_blogType where id=#{id}
	</select>
	
	<select id="list" parameterType="Map" resultMap="BlogTypeResult">
		select * from tb_blogType
		<if test="start!=null and size!=null">
			limit #{start},#{size}
		</if>
	</select>
	
	<select id="getTotal" parameterType="Map" resultType="Long">
		select count(*) from tb_blogType
	</select>
	
	<insert id="add" parameterType="BlogType">
		insert into tb_blogType values(null,#{typeName},#{orderNo});
	</insert>
	
	<update id="update" parameterType="BlogType">
		update tb_blogType
		<set>
			<if test="typeName!=null and typeName!='' ">
				typeName=#{typeName},
			</if>
			<if test="orderNo!=null ">
				orderNo=#{orderNo},
			</if>
		</set>
		where id=#{id}
	</update>
	
	<delete id="delete" parameterType="Integer">
		delete from tb_blogType where id=#{id}
	</delete>
</mapper> 


    CommentMapper.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.steadyjack.dao.CommentDao">

	<resultMap type="Comment" id="CommentResult">
		<result property="id" column="id"/>
		<result property="userIp" column="userIp"/>
		<result property="content" column="content"/>
		<result property="commentDate" column="commentDate"/>
		<result property="state" column="state"/>
		<association property="blog" column="blogId" select="com.steadyjack.dao.BlogDao.findById"></association>
	</resultMap>
	
	<insert id="add" parameterType="Comment">
		insert into tb_comment values(null,#{userIp},#{blog.id},#{content},now(),0)
	</insert>
	
	<update id="update" parameterType="Comment">
		update tb_comment
		<set>
			<if test="state!=null">
				state=#{state},
			</if>
		</set>
		where id=#{id}
	</update>
	
	<select id="list" parameterType="Map" resultMap="CommentResult">
		select * from tb_comment
		<where>
			<if test="blogId!=null">
				and blogId=#{blogId}
			</if>
			<if test="state!=null">
				and state=#{state}
			</if>
		</where>
		order by commentDate 
		<if test="start!=null and size!=null">
			limit #{start},#{size}
		</if>
	</select>
	
	<select id="getTotal" parameterType="Map" resultType="Long">
		select count(*) from tb_comment
		<where>
			<if test="state!=null">
				and state=#{state}
			</if>
		</where>
	</select>
	
	
	<delete id="delete" parameterType="Integer">
		delete from tb_comment where id=#{id}
	</delete>
</mapper> 


    LinkMapper.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.steadyjack.dao.LinkDao">

	<resultMap type="Link" id="LinkResult">
		<result property="id" column="id"/>
		<result property="linkName" column="linkName"/>
		<result property="linkUrl" column="linkUrl"/>
		<result property="orderNo" column="orderNo"/>
	</resultMap>
	
	<insert id="add" parameterType="Link">
		insert into tb_link values(null,#{linkName},#{linkUrl},#{orderNo})
	</insert>
	
	<update id="update" parameterType="Link">
		update tb_link
		<set>
			<if test="linkName!=null and linkName!='' ">
				linkName=#{linkName},
			</if>
			<if test="linkUrl!=null and linkUrl!='' ">
				linkUrl=#{linkUrl},
			</if>
			<if test="orderNo!=null">
				orderNo=#{orderNo},
			</if>
		</set>
		where id=#{id}
	</update>
	
	<select id="list" parameterType="Map" resultMap="LinkResult">
		select * from tb_link order by orderNo
		<if test="start!=null and size!=null">
			limit #{start},#{size}
		</if>
	</select>
	
	<select id="getTotal" parameterType="Map" resultType="Long">
		select count(*) from tb_link
	</select>
	
	
	<delete id="delete" parameterType="Integer">
		delete from tb_link where id=#{id}
	</delete>
</mapper> 


    然后把shiro配置文件中自定义的realm也贴出来吧,用于用户权限认证授权。包package com.steadyjack.realm,建立MyRealm.java:

 

 

package com.steadyjack.realm;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import com.steadyjack.entity.Blogger;
import com.steadyjack.service.BloggerService;

/**
 * title:MyRealm.java
 * description:自定义Realm(域)
 * time:2017年1月22日 下午10:50:57
 * author:debug-steadyjack
 */
public class MyRealm extends AuthorizingRealm{

	@Resource
	private BloggerService bloggerService;
	
	/**
	 * 为当限前登录的用户授予角色和权
	 * (由于目前系统没有啥资源且只有admin超级用户,故而没写授予角色、权限(资源)逻辑)
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		return null;
	}

	/**
	 * 验证当前登录的用户
	 * (成功时,将登陆用户绑定到会话中;失败时,其实会报各种exception,理当抛出用于前端页面展示(后期实现))
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//获取身份(在这里指 用户名)-凭证(在这里指 密码)
		String userName=(String)token.getPrincipal();
		Blogger blogger=bloggerService.getByUserName(userName);
		if(blogger!=null){
			//当前用户信息存到session中
			SecurityUtils.getSubject().getSession().setAttribute("currentUser", blogger); 
			AuthenticationInfo authcInfo=new SimpleAuthenticationInfo(blogger.getUserName(),blogger.getPassword(),getName());
			return authcInfo;
		}else{
			return null;				
		}
	}

}


     至此,基本上后端mvc的dao层与service层跟model都已经分享完毕。可能缺一些util(下篇贴出),加入util之后,再开发controller与页面解析,基本上系统慢慢就起来了。。。。

 

     如果有相关问题:如想找我付费开发其他功能,讨论其中相关问题等等,可以来以下两群找我,我叫debug!

     Java开源技术交流:583522159     个人QQ:1948831260

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

修罗debug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值