码迷,mamicode.com
首页 > 其他好文 > 详细

快讯项目 _1

时间:2018-01-08 16:45:11      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:public   where   div   java   dispatch   单元测试   water   www.   patch   

1 项目搭建以及环境配置

本项目是一个简单的新闻资讯的发布管理系统,新闻工作人员可以添加新闻,删除,修改新闻。也可以编辑发布新闻。

1.1 新建news_flash项目(web项目结构)maven配置,下图是项目的整体结构。

技术分享图片

1.2 领域对象的建立domain

package cn.czg.core.dao;

import java.io.Serializable;
import java.util.Date;

/**
 * 新闻领域对象
 */
public class News implements Serializable{

    //新闻id
    private Long id;
    //新闻题目
    private String title;
    //新闻内容
    private String context;
    //新闻图片路径
    private String imgUrl;
    //新闻录入时间
    private Date inputDate;
    //浏览次数(热点新闻)
    private Integer viewCount;
    //是否推荐新闻
    private Boolean isRecommend;
    //新闻类型
    private NewsType type;
    //新闻录入人(自动生成)
    private User user;

    public Long getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public Date getInputDate() {
        return inputDate;
    }

    public void setInputDate(Date inputDate) {
        this.inputDate = inputDate;
    }

    public Integer getViewCount() {
        return viewCount;
    }

    public void setViewCount(Integer viewCount) {
        this.viewCount = viewCount;
    }

    public Boolean getRecommend() {
        return isRecommend;
    }

    public void setRecommend(Boolean recommend) {
        isRecommend = recommend;
    }

    public NewsType getType() {
        return type;
    }

    public void setType(NewsType type) {
        this.type = type;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

上述的是News对象,下面以此建立NewsType、以及User对象

package cn.czg.core.dao;

import java.io.Serializable;

/**
 * 新闻类型领域对象
 */
public class NewsType implements Serializable{
    /** 新闻类型id*/
    private Long id;
    /** 新闻类型名称*/
    private String name;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package cn.czg.core.dao;

import java.io.Serializable;

/**
 * 用户领域对象
 */
public class User implements Serializable {

    /** 用户id*/
    private Long id;
    /** 用户名称*/
    private String name;
    /** 用户密码*/
    private String password;
    /** 0:代表管理员(管理后台)  1:普通用户(可以留言)*/
    private Integer type;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }
}

1.3 根据domain建立数据库mysql

第一张表:t_news,建表语句如下:

CREATE TABLE t_news ( 
id bigint(20) NOT NULL AUTO_INCREMENT, 
title varchar(50) DEFAULT NULL, 
context longtext, 
imgUrl varchar(255) DEFAULT NULL, 
viewCount int(11) DEFAULT NULL, 
isRecommend bit(1) DEFAULT NULL, 
inputDate datetime DEFAULT NULL, 
type_id bigint(20) DEFAULT NULL, 
user_id bigint(20) DEFAULT NULL, 
PRIMARY KEY (id) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

第二张表:t_newsType,建表语句如下:

CREATE TABLE t_newstype ( 
id bigint(20) NOT NULL AUTO_INCREMENT, 
name varchar(50) DEFAULT NULL, 
PRIMARY KEY (id) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

第三张表:t_user,建表语句如下:

CREATE TABLE t_user ( 
id bigint(20) NOT NULL AUTO_INCREMENT, 
name varchar(255) DEFAULT NULL, 
password varchar(255) DEFAULT NULL, 
type int(11) DEFAULT NULL, 
PRIMARY KEY (id) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

1.4 配置数据源,装配bean,根据bean创建dao层

jdbc.properties文件:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///news_flash
jdbc.username=root
jdbc.password=gosaint

applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- druid数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="newsDAO" class="cn.czg.core.dao.impl.NewsDAOImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

创建dao以及dao的实现类,这里使用JdbcDaoSupport

package cn.czg.core.dao;

import cn.czg.core.domain.News;

import java.util.List;

/**
 * 新闻咨询的CRUD接口
 */
public interface NewsDAO {
    void save(News news);
    void delete(Long id);
    void update(News news);
    News query(Long id);
    List<News> list();
}
package cn.czg.core.dao.impl;

import cn.czg.core.dao.NewsDAO;
import cn.czg.core.domain.News;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 继承JdbcDaoSupport,里面有数据源对象
 */
public class NewsDAOImpl extends JdbcDaoSupport implements NewsDAO{
    /**
     * 保存一个news对象
     * @param news
     */
    @Override
    public void save(News news) {
        super.getJdbcTemplate().update("insert into t_news(title,context,imgUrl,viewCount,isRecommend,inputDate) values (?,?,?,?,?,?)",
                news.getTitle(),news.getContext(),news.getImgUrl(),news.getViewCount(),news.getIsRecommend(),news.getInputDate());

    }

    @Override
    public void delete(Long id) {
        super.getJdbcTemplate().update("DELETE FROM t_news WHERE id",id);
    }

    @Override
    public void update(News news) {
        super.getJdbcTemplate().update("update t_news set title=?,context=?,imgUrl=?,viewCount=?,isRecommend=?,inputDate=? where id=?",
                news.getTitle(),news.getContext(),news.getImgUrl(),news.getViewCount(),news.getIsRecommend(),news.getInputDate(),news.getId()
        );

    }

    @Override
    public News query(Long id) {
        return super.getJdbcTemplate().queryForObject("select * from t_news where id=?", new NewsRowMapper()) ;

    }

    @Override
    public List<News> list() {
        return super.getJdbcTemplate().query("select * from t_news",new NewsRowMapper());
    }

    /**
     * 使用静态内部类封装了结果集对象
     */
    public static class NewsRowMapper implements RowMapper<News>{

        @Override
        public News mapRow(ResultSet resultSet, int i) throws SQLException {
            News news = new News();
            news.setId(resultSet.getLong("id"));
            news.setTitle(resultSet.getString("title"));
            news.setContext(resultSet.getString("context"));
            news.setImgUrl(resultSet.getString("imgUrl"));
            news.setViewCount(resultSet.getInt("viewCount"));
            news.setIsRecommend(resultSet.getBoolean("isRecommend"));
            news.setInputDate(resultSet.getDate("inputDate"));
            return news;
        }
    }
}

1.5 service层

package cn.czg.core.service.impl;

import cn.czg.core.dao.NewsDAO;
import cn.czg.core.domain.News;
import cn.czg.core.service.NewsService;

import java.util.List;

public class NewsServiceImpl implements NewsService{
    /**
     * 注入dao
     * @param news
     */
    private NewsDAO newsDAO;

    public void setNewsDAO(NewsDAO newsDAO) {
        this.newsDAO = newsDAO;
    }

    @Override
    public void save(News news) {
        newsDAO.save(news);
    }

    @Override
    public void delete(Long id) {
        newsDAO.delete(id);
    }

    @Override
    public void update(News news) {
        newsDAO.update(news);
    }

    @Override
    public News findNewsById(Long id) {
        return newsDAO.query(id);
    }

    @Override
    public List<News> list() {
        return newsDAO.list();
    }
}

1.6 单元测试

1 抽取BaseTest类(Spring测试的注解配置) 
2 CRUD的测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class BaseTest {
}

 

public class NewsTest extends BaseTest {
    @Autowired
    private NewsService newsService;
    @Test
    public void saveTest(){
        News news=new News();
        news.setTitle("刘强东:敢在京东卖假货,就敢让你破产");
        news.setViewCount(77);
        news.setIsRecommend(true);
        news.setInputDate(new Date());
        news.setImgUrl("E:\\就业资料\\14_aigou\\2017-07-31-B2C商城(爱购网)-DAY01\\code\\news_flash\\img\\f3624688c24ee77_w495_h277.jpg");
        news.setContext("刘强东:做供给链处事。互联网有两个情势,一个是轻情势,一个是重情势,轻情势就是做平台,然则多么没有价值,我们要做有价值的任务,所以,供给链路子是对的,我们是独一的一家重运营重资产的情势。");
        newsService.save(news);
    }
    @Test
    public void testQuery(){
        News newsById = newsService.findNewsById(16L);
        System.out.println(newsById);
    }
    @Test
    public void delete(){
        newsService.delete(10L);
    }
    @Test
    public void updateTest(){
        News news = newsService.findNewsById(16L);
        news.setTitle("寺库的危机");
        newsService.update(news);
    }
    @Test
    public void testList(){
        List<News> list = newsService.list();
        System.out.println(list);
    }
}

 

1.7 web.xml的配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--上下文的配置-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!--struts2的核心过滤器的配置-->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!--spring监听的配置-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
</web-app>

 

.启动tomcat,运行即可,发现程序正式启动,第二章会为大家带来相关的页面,以及分页和高级查询,以及图片上传等相关的技术。敬请期待。相关的代码我会在最后一章的时候发布到我的Github地址上,感兴趣的可以下载下来直接使用,当然相应的sql文件包括在内

代码请见最后一章,有Github地址

快讯项目 _1

标签:public   where   div   java   dispatch   单元测试   water   www.   patch   

原文地址:https://www.cnblogs.com/gosaint/p/8242979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!