【MyEclipse】:SSH快速搭建
【MyEclipse】:SSH快速搭建
目录ssh搭建的前提,需要掌握 spring,struts2,hibernate
这是一种快速搭建的方式,框架基本上都是自动生成好。
如有不足之处,请指教!
- 【MyEclipse】:SSH快速搭建
- 1、数据库表
- 2、新建项目
- 2.1、创建一个web project项目
- 2.2、导入spring
- 2.3、添加 struts2
- 2.4、添加 Hibernate
- 2.5、逆向工程,生成实体类
- 2.6、配置 web.xml
- 2.7、数据编写
- 3、视图层
- index.jsp
- list.jsp
- add.jsp
- update.jsp
- 执行效果:
- 最终目录结构:
1、数据库表
create table users(
uid INT auto_increment PRIMARY KEY,
uname VARCHAR(20) NOT NULL,
usex VARCHAR(2) NOT NULL,
ubirth TIMESTAMP DEFAULT CURRENT_TIMESTAMP not null
)DEFAULT CHARSET=utf8;
insert into users values(null,"张珊",'女',DEFAULT);
insert into users values(null,"李四",'男',DEFAULT);
insert into users values(null,"王五",'男',DEFAULT);
insert into users values(null,"赵柳",'女',DEFAULT);
2、新建项目
2.1、创建一个web project项目

2.2、导入spring


spring 添加玩以后,会多出很多的jar 包和一个配置文件 applicationContext.xml

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
2.3、添加 struts2
导入 struts2



项目结构

struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
2.4、添加 Hibernate
- 导入hibernate






- Hibernate 的配置自动添加到 applicationContext.xml 文件中
org.hibernate.dialect.MySQLDialect
2.5、逆向工程,生成实体类
- 连接 mysql 数据库



- 导入数据库表




项目结构

- 在 applicationContext.xml 中配置事务和支持注解开发
<?xml version="1.0" encoding="UTF-8"?>
org.hibernate.dialect.MySQLDialect
com/ssh/pojo/Users.hbm.xml
注意:

2.6、配置 web.xml
- 配置 Context Params

- Filters 过滤器








-
Listeners 监听器




-
保存,web.xml就配置完了
-
部署到Tomcat服务器上,无报错,即搭建成功!
2.7、数据编写
-
建立基本结构
- com.ssh.action
- com.ssh.dao
- com.ssh.pojo
- com.ssh.service
-
实体类和dao层
实体类
public class Users implements java.io.Serializable {
private Integer uid;
private String uname;
private String usex;
private Timestamp ubirth;
//get/set方法,有参无参构造函数,toString();
}
? dao层
package com.ssh.dao;
import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.ssh.pojo.Users;
public class UsersDAO extends HibernateDaoSupport {
private static final Logger log = LoggerFactory.getLogger(UsersDAO.class);
// property constants
public static final String UNAME = "uname";
public static final String USEX = "usex";
protected void initDao() {
// do nothing
}
public void save(Users transientInstance) {
log.debug("saving Users instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Users persistentInstance) {
log.debug("deleting Users instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Users findById(java.lang.Integer id) {
log.debug("getting Users instance with id: " + id);
try {
Users instance = (Users) getHibernateTemplate().get(
"com.ssh.pojo.Users", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Users instance) {
log.debug("finding Users instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding Users instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Users as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByUname(Object uname) {
return findByProperty(UNAME, uname);
}
public List findByUsex(Object usex) {
return findByProperty(USEX, usex);
}
public List findAll() {
log.debug("finding all Users instances");
try {
String queryString = "from Users";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public Users merge(Users detachedInstance) {
log.debug("merging Users instance");
try {
Users result = (Users) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Users instance) {
log.debug("attaching dirty Users instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Users instance) {
log.debug("attaching clean Users instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static UsersDAO getFromApplicationContext(ApplicationContext ctx) {
return (UsersDAO) ctx.getBean("UsersDAO");
}
}
-
Users.hbm.xml
注意:里面的
not null = ‘true’需要删除,不然做曾删改时会报错!如果没删传对象时所有属性都不能为空!
<?xml version="1.0" encoding="utf-8"?>
-
编写Service层的接口和实现类
接口
package com.ssh.service;
import java.util.List;
import com.ssh.pojo.Users;
public interface UsersService {
//查询所有用户
List queryUsersList();
//单值查询
Users queryUsers();
//添加
void addUsers(Users users);
//修改
void updateUsers(Users users);
//删除
void delUsers(Users users);
}
? 实现类
package com.ssh.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.ssh.dao.UsersDAO;
import com.ssh.pojo.Users;
public class UsersServiceImpl implements UsersService {
private UsersDAO usersDAO;
public void setUsersDAO(UsersDAO usersDAO) {
this.usersDAO = usersDAO;
}
//添加
@Autowired
@Transactional
public void addUsers(Users users) {
usersDAO.save(users);
}
//删除
@Autowired
@Transactional
public void delUsers(Users users) {
usersDAO.delete(users);
}
//单值查询
public Users queryUsers(int uid) {
return (Users) usersDAO.findById(uid);
}
//查询所有
public List queryUsersList() {
return usersDAO.findAll();
}
//修改
@Autowired //自动装配
@Transactional //开启事务行为,曾删改
public void updateUsers(Users users) {
usersDAO.merge(users);
}
}
- 编写action层
package com.ssh.action;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ssh.pojo.Users;
import com.ssh.service.UsersService;
public class UsersAction extends ActionSupport {
private UsersService usersService;
private Users users;
private List list;
private int uid;
//get/set方法
public void setUsersService(UsersService usersService) {
this.usersService = usersService;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return super.execute();
}
//查询所有用户
public String list() throws Exception {
list = usersService.queryUsersList();
return "list";
}
//单值查询
public String query() throws Exception {
Users user = usersService.queryUsers(uid);
//获取session会话
HttpSession session = ServletActionContext.getRequest().getSession();
//给session赋值
session.setAttribute("user", user);
//返回
return "query";
}
//添加
public String add() throws Exception {
usersService.addUsers(users);
return list();
}
//删除
public String del() throws Exception {
usersService.delUsers(users);
return list();
}
//修改
public String update() throws Exception {
usersService.updateUsers(users);
return list();
}
}
- applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
org.hibernate.dialect.MySQLDialect
com/ssh/pojo/Users.hbm.xml
- struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
/list.jsp
/update.jsp
3、视图层
index.jsp
去list.jsp
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'list.jsp' starting page
用户信息
添加用户
编号
姓名
性别
生日
操作
${user.uid }
${user.uname }
${user.usex }
${user.ubirth }
修改
删除
<script>
function delUsers(id){
if(confirm("确认删除吗?")){
location.href="users/usersAction_del?users.uid="+id;
}
}
</script>
add.jsp
update.jsp
执行效果:

最终目录结构:
