一个简单的S2SH的整合新闻案例,今天给大家分享一下。该项目使用的ORACLE数据库
+Hibernate+Struts2+Spring 。希望可以对大家起到帮助
1.oracle创建用户和表
--创建表空间
create tablespace superhang
logging
datafile 'd:/superhang.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
--创建临时表空间
create temporary tablespace bocodbtempdbs
tempfile 'd:/bocodbtempdbs01.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
create user xuhang identified by svse
default tablespace superhang
temporary tablespace bocodbtempdbs;
--授权
grant connect,resource to xuhang;
--查看所有表空间
select dbf.tablespace_name,
dbf.totalspace "总量(M)",
dbf.totalblocks as 总块数,
dfs.freespace "剩余总量(M)",
dfs.freeblocks "剩余块数",
(dfs.freespace / dbf.totalspace) * 100 "空闲比例"
from (select t.tablespace_name,
sum(t.bytes) / 1024 / 1024 totalspace,
sum(t.blocks) totalblocks
from dba_data_files t
group by t.tablespace_name) dbf,
(select tt.tablespace_name,
sum(tt.bytes) / 1024 / 1024 freespace,
sum(tt.blocks) freeblocks
from dba_free_space tt
group by tt.tablespace_name) dfs
where trim(dbf.tablespace_name) = trim(dfs.tablespace_name)
--创建news表
create table news
(
nid number primary key ,
kid number references userinfo(id),
ntitle varchar2(50) not null,
ncontext varchar2(500) not null,
ntime varchar2(50) not null
)
--创建发布人表
create table userinfo
(
id number primary key,
uname varchar2(50) not null,
upsw varchar2(50) not null,
urealname varchar2(50) not null
)
--创建序列
create sequence user_seq
minvalue 1
maxvalue 999999999
start with 1
increment by 1
cache 20;
--创建序列
create sequence news_seq
minvalue 1
maxvalue 999999999
start with 1
increment by 1
cache 20;
insert into userinfo values (user_seq.Nextval,'admin','123456','张三');
insert into news values(news_seq.Nextval,2,'张三成功的登上了月球','月球,俗称月亮,古时又称太阴、玄兔,是地球唯一的天然卫星,并且是太阳系中第五大的卫星','2014-5-21');
insert into news values(news_seq.Nextval,2,'世界末日什么时候来','xxxxx','2014-5-22');
insert into news values(news_seq.Nextval,2,'amazeUI很不错','xxxxxxxx','2014-5-23');
select * from userinfo;
select * from news;
导入Struts时勾选这2个包
导入hibernate勾选这2个包
3.项目搭建好了。我们就可以编写业务逻辑了
web.xml文件加入2(listener和filter)个配置
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
--dao层,使用接口的编写方式,定义2个接口
package xander.dao;
import java.util.List;
import xander.entity.News;
public interface NewsDAO {
public List getAllNews();
public void addNews(News news);
}
package xander.dao;
import xander.entity.Userinfo;
public interface UserDAO {
public Userinfo getUser(String uname,String upsw);
}
--2接口的实现类DAOImpl
package xander.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import xander.dao.NewsDAO;
import xander.entity.News;
@Repository("newsDAO")
public class NewsDAOImpl implements NewsDAO{
@Resource
private HibernateTemplate hibernateTemplate;
public void addNews(News news) {
hibernateTemplate.save(news);
}
public List getAllNews() {
List news =hibernateTemplate.find("from News");
return news;
}
}
package xander.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import xander.dao.UserDAO;
import xander.entity.Userinfo;
@Repository("userDAO")
public class UserDAOImpl implements UserDAO{
@Resource
private HibernateTemplate hibernateTemplate;
public Userinfo getUser(String uname, String upsw) {
String hql = "from Userinfo u where u.uname = ? and u.upsw = ?";
List userinfos = hibernateTemplate.find(hql, uname,upsw);
if(userinfos==null || userinfos.size()==0){
return null;
}
return userinfos.get(0);
}
}
package xander.service;
import java.util.List;
import xander.entity.News;
public interface NewsService {
public List getAllNews();
public void addNews(News news);
}
package xander.service;
import xander.entity.Userinfo;
public interface UserService {
public Userinfo getUser(String uname,String upsw);
}
package xander.service;
import xander.entity.Userinfo;
public interface UserService {
public Userinfo getUser(String uname,String upsw);
}
package xander.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import xander.dao.UserDAO;
import xander.entity.Userinfo;
import xander.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService{
@Resource
private UserDAO userDAO;
public Userinfo getUser(String uname, String upsw) {
// TODO Auto-generated method stub
return userDAO.getUser(uname, upsw);
}
}
--接下里我们就可以编写action类
一个UserAction处理登录
package xander.action;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.xml.crypto.Data;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import xander.entity.News;
import xander.service.NewsService;
import com.opensymphony.xwork2.ActionSupport;
@Controller("newsAction")
@Scope("prototype")
public class NewsAction extends ActionSupport {
private News snews;
@Resource
private NewsService newsService;
private List news;
public News getSnews() {
return snews;
}
public void setSnews(News snews) {
this.snews = snews;
}
public List getNews() {
return news;
}
public void setNews(List news) {
this.news = news;
}
public String addUI() throws Exception {
return "addUI";
}
public String add() throws Exception {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String ntime =sdf.format(date);
snews.setNtime(ntime);
newsService.addNews(snews);
// 获取新闻列表
news = newsService.getAllNews();
return "list";
}
}
--NewsAction处理新闻的显示
package xander.action;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import xander.entity.News;
import xander.entity.Userinfo;
import xander.service.NewsService;
import xander.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport{
@Resource
private UserService userService;
@Resource
private NewsService newsService;
private List news;
public List getNews() {
return news;
}
public void setNews(List news) {
this.news = news;
}
private Userinfo userinfo;
public Userinfo getUserinfo() {
return userinfo;
}
public void setUserinfo(Userinfo userinfo) {
this.userinfo = userinfo;
}
//登录
public String login() throws Exception {
Userinfo user = userService.getUser(userinfo.getUname(), userinfo.getUpsw());
if(user !=null){
Map map = ActionContext.getContext().getSession();
map.put("user", user);
news = newsService.getAllNews();
return "list";
}else{
ActionContext.getContext().put("msg", "你输入的用户名或密码有误!");
return INPUT;
}
}
}
--struts的配置文件如下
/show_news.jsp
/index.jsp
/news_addUI.jsp
/show_news.jsp
--applicationContext配置文件:
org.hibernate.dialect.Oracle9Dialect
xander/entity/News.hbm.xml
xander/entity/Userinfo.hbm.xml
--3个JSP页面如下
1.登录页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
登录页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
显示新闻信息
编号
标题
发布时间
发布人
${sessionScope.user.urealname}
发布新闻
--新闻添加页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
添加新闻页面
到这里我们的项目就做完了,一个简易新闻SSH整合就结束了,我们使用了部分注解的方式