Spring-Struts-Mybatis框架数据传递

新闻管理单模块为例,目录结构如下:
在这里插入图片描述
命名规范:包名小写,类名采用驼峰命名法(大驼峰)

po(模型层)

News.java

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import tk.mybatis.mapper.annotation.NameStyle;
import tk.mybatis.mapper.code.Style;
@NameStyle(Style.normal)
@Table(name = "tab_news_info") //用来命名 当前实体类 对应的数据库 表的名字
public class News {
	@Id //声明id字段
	private long id;
	private String title; //标题
	private String content;//内容
	private String newstime;//创建时间
	private String remark;//评论
	private String lcid;//流程id
	private String lcstatus;//流程状态
	private long releasePerson;//发布人id
	private long imageId;//图片id
	private long videoId;//视频id
	private String dept;//部门
	private int totalView;//访问次数
	private int status;//状态
	private String name;//信息录入者
	private long views;//浏览量
	private long type;//新闻类型

	//实体类中使用了@Table注解后,想要添加表中不存在字段,就要使用@Transient这个注解(关联查询用到)
	@Transient
	private String imgUrl;
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDept() {
		return dept;
	}
	public void setDept(String dept) {
		this.dept = dept;
	}
	public int getTotalView() {
		return totalView;
	}
	public void setTotalView(int totalView) {
		this.totalView = totalView;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}

	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getNewstime() {
		return newstime;
	}
	public void setNewstime(String newstime) {
		this.newstime = newstime;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public String getLcid() {
		return lcid;
	}
	public void setLcid(String lcid) {
		this.lcid = lcid;
	}
	public String getLcstatus() {
		return lcstatus;
	}
	public void setLcstatus(String lcstatus) {
		this.lcstatus = lcstatus;
	}
	public long getImageId() {
		return imageId;
	}
	public void setImageId(long imageId) {
		this.imageId = imageId;
	}
	public long getVideoId() {
		return videoId;
	}
	public void setVideoId(long videoId) {
		this.videoId = videoId;
	}
	public long getReleasePerson() {
		return releasePerson;
	}
	public void setReleasePerson(long releasePerson) {
		this.releasePerson = releasePerson;
	}
	public String getImgUrl() {
		return imgUrl;
	}
	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}
	public long getViews() {
		return views;
	}
	public void setViews(long views) {
		this.views = views;
	}
	public long getType() {
		return type;
	}
	public void setType(long type) {
		this.type = type;
	}
}

dao(数据操作)

NewsMapper.java
extends Mapper< News >,同时继承框架封装的方法:基础对象增删改查,无需具体实现

import java.util.List;
import org.apache.ibatis.annotations.Param;
import tk.mybatis.mapper.common.Mapper;
import com.linksaint.common.AjaxBeanData;
import com.linksaint.util.JSONObjectTool;
import com.linksaint.web.material.po.Material;
import com.linksaint.web.news.po.News;

public interface NewsMapper extends Mapper<News>{
/**
 * 根据begNum、endNum和type查询新闻
 * @return  List<News>
 */
	public List<News> getNewsByRowNumAndType(@Param("begNum")int begNum,@Param("endNum")int endNum,@Param("type")long type);
	/**
 * 查询各类型浏览量最多的一条新闻
 * @return  List<News>
 */
	public List<News> findNewstop1();

}

NewsMapper.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.linksaint.web.news.dao.NewsMapper">
	<resultMap type="com.linksaint.web.news.po.News" id="news"></resultMap>
	<resultMap type="java.util.Map" id="map"></resultMap>
<!--根据begNum、endNum和type查询新闻 -->
	<select id="getNewsByRowNumAndType" resultMap="news">
	  select *
			from 
			(
			    select a.*, SUBSTRING(b.filePath,4,len(b.filePath)) imgUrl,
			  row_number() over(order by newstime desc) as rownum
			    from tab_news_info a left join fileUpload b on a.imageId=b.fileId
			 where type=${type} 
			)tab
			where rownum between ${begNum} and ${endNum}
	</select>
<!-- 查询各类型浏览量最多的一条新闻-->
	<select  id="findNewstop1"  resultMap="news">
	      select top 1 id,title,newstime,1 as type from tab_news_info where id=(select MAX(id) from tab_news_info where type=1) 
			UNION
			select top 1 id,title,newstime,2 as type from tab_news_info where id=(select MAX(id) from tab_news_info where type=2) 
			UNION
			select top 1 id,title,newstime,3 as type from tab_news_info where  id=(select MAX(id) from tab_news_info where type=3) 
			UNION
			select  top 1 id,title,beginTime newstime,4 from tab_notice_info where id=(select MAX(id) from tab_notice_info ) 

	</select>

</mapper>

service(服务层)

NewsService.java

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.linksaint.system.service.SysMaxIdService;
import com.linksaint.util.StringUtils;
import com.linksaint.web.news.dao.NewsMapper;
import com.linksaint.web.news.po.News;

//@Service:自动注册到Spring容器,不需要再在applicationContext.xml文件定义bean了,类似的还包括@Component、@Repository、@Controller
@Service("newsService")
public class NewsService {

	//@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作
	@Autowired
	public NewsMapper newsMapper;
	@Autowired
	private SysMaxIdService sysMaxIdService;
	
	/**
	 * 基础方法:新增news
	 * @author ykp
	 */
	public void insertNews(News news) {
		long id = sysMaxIdService.selectSysMaxId("tab_news");//查询当前变量"tab_news"最大ID
		news.setId(id);
		newsMapper.insertSelective(news);
	};
	
	/**
	 * 基础方法:修改news
	 * @author ykp
	 */
	public void updateNews(News news) {
		newsMapper.updateByPrimaryKeySelective(news);
	};
	
	/**
	 * 基础方法:多选删除,根据Ids删除news
	 * @author ykp
	 */
	public void deleteNewsbyIds(String ids) {
		long[] idss = StringUtils.string2long(ids);
		for (int i = 0; i < idss.length; i++) {
			newsMapper.deleteByPrimaryKey(idss[i]);
		}

	};
	
	/**
	 * 基础方法:根据ID查询news对象
	 * @return  News
	 * @author ykp
	 */
	public News selectNewsbyId(long id) {
		News news = newsMapper.selectByPrimaryKey(id);
		return news;
	}
	
	/**
	 * 基础方法:查询所有news对象
	 * @return  List<News>
	 * @author ykp
	 */
	public List<News> selectAllNews() {
		List<News> newsList = newsMapper.selectAll();
		return newsList;
	}
	
	/**
	 * 根据begNum、endNum和type查询新闻
	 * @return  List<News>
	 * @author ykp
	 */
	public List<News> selectNewsByType(int begNum,int endNum,long type){
		return newsMapper.getNewsByRowNumAndType(begNum,endNum,type);
	}
	
	/**
	 * 查询各类型浏览量最多的一条新闻
	 * @return  List<News>
	 * @author ykp
	 */
	public List<News> findNewstop1(){
		return newsMapper.findNewstop1();
	}

}

action(控制层)

NewsAction.java
extends BaseAction,里面定义了常用的成员变量,以及forwordList,forwordInsert,可直接使用或自行定义

package com.linksaint.web.news.action;

import org.springframework.beans.factory.annotation.Autowired;
import com.linksaint.common.AjaxBeanData;
import com.linksaint.core.BaseAction;
import com.linksaint.system.po.SysUser;
import com.linksaint.system.service.SysMaxIdService;
import com.linksaint.system.service.SysUserService;
import com.linksaint.util.DateUtils;
import com.linksaint.util.JSONObjectTool;
import com.linksaint.util.StrutsUtils;
import com.linksaint.web.fileUpload.FileUploadModel;
import com.linksaint.web.fileUpload.FileUploadService;
import com.linksaint.web.news.po.News;
import com.linksaint.web.news.service.NewsService;

public class NewsAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	@Autowired
	private NewsService newsService;
	@Autowired
	private SysMaxIdService sysMaxIdService;
	@Autowired
	private FileUploadService fileUploadService;
	@Autowired
	private SysUserService sysUserService;
	private News news;
	private Long id;
	private FileUploadModel fileUpload;
	private FileUploadModel videoUpload;
	
	/**新闻发布
	 * @return
	 */
	public String forwordNews(){
	    return "forwordNews";
	}
	
	public String ajaxNewsList(){
		String sql=" select news.*,t.varsName from tab_news_info news  inner join (select * from sysVars where varsTypeId=2021) t on t.varsValue=news.type where 1=1 ";
	    json = customQueryService.getCustomQueryData("("+sql+") t");
	    return AJAX;
	}
	
	/**
	 * 跳转新增页面
	 * @return
	 */
	public String forwordInsert(){
	    return "forwordInsert";
	}
	
	public String ajaxInsert(){
		SysUser sysUser = (SysUser) StrutsUtils.getObjectFromSession("sysUser");
			news.setReleasePerson(sysUser.getUserId());
			news.setId(id);
			news.setName(sysUser.getUserName());
			news.setNewstime(DateUtils.getNowDate());
			news.setLcstatus("0,草稿");
			newsService.addNews(news);
			json = JSONObjectTool.getJson(AjaxBeanData.success());
		return AJAX;
	}
	
	/**
	 * 跳转修改页面
	 * @return
	 */
	public String forwordUpdate(){
		news = newsService.findNews(news.getId());
		fileUpload=fileUploadService.selectById(news.getImageId());
		videoUpload=fileUploadService.selectById(news.getVideoId());
		return "forwordUpdate";
	}
	
	public String ajaxUpdate(){
		News news2 = newsService.findNews(id);
		news.setId(id);
		news.setLcstatus(news2.getLcstatus());
		news.setNewstime(news2.getNewstime());
		news.setName(news2.getName());
		news.setReleasePerson(news2.getReleasePerson());
		newsService.updateNews(news);
		json = JSONObjectTool.getJson(AjaxBeanData.success());
		return AJAX;
	}
	
	/**
	 * 跳转删除数据
	 * @return
	 */
	public String ajaxDelete(){
		
		String[] split = ids.split(",");
		for (String s : split) {
			newsService.deleteNews(Long.parseLong(s));
		}
		json = JSONObjectTool.getJson(AjaxBeanData.success());
		return AJAX;
	}

	/**
	 * 跳转查看详情页面
	 * @return
	 */
	public String forwordView(){
		news = newsService.findNews(news.getId());
		fileUpload=fileUploadService.selectById(news.getImageId());
		videoUpload=fileUploadService.selectById(news.getVideoId());
		return "forwordView";
	}
	
	
			
	public String ajaxCheckStatus(){
		news=newsService.findNews(news.getId());
		AjaxBeanData ajaxBeanData = new AjaxBeanData();
		ajaxBeanData.setResult("success");
		ajaxBeanData.setResume("成功");
		ajaxBeanData.setData(news);
		json = JSONObjectTool.getJson(ajaxBeanData);
		return AJAX;
	}
//==========将依赖对象通过GET、SET方式注入======================
	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public FileUploadModel getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(FileUploadModel fileUpload) {
		this.fileUpload = fileUpload;
	}

	public FileUploadModel getVideoUpload() {
		return videoUpload;
	}

	public void setVideoUpload(FileUploadModel videoUpload) {
		this.videoUpload = videoUpload;
	}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值