引述
SSH,是最简单的Struts2+Spring3+Hibernate3 数据库使用的是[MySQL。分别列出整合需要的jar 包.
** struts2.5**:
asm-5.1.jar
asm-commons-5.1.jar
asm-tree-5.1.jar
commons-fileupload-1.3.2.jar
commons-io-2.4.jar
commons-lang-2.4.jar
commons-lang3-3.4.jar
commons-logging-1.1.3.jar
freemarker-2.3.23.jar
javassist-3.20.0-GA.jar
json-lib-2.3-jdk15.jar
list.txt
log4j-api-2.7.jar
log4j-core-2.3.jar
ognl-3.1.12.jar
slf4j-api-1.7.12.jar
struts2-convention-plugin-2.5.10.1.jar
struts2-core-2.5.10.1.jar
struts2-json-plugin-2.5.10.1.jar
struts2-spring-plugin-2.5.10.1.jar
Hibernate5:
antlr-2.7.7.jar
cdi-api-1.1.jar
classmate-1.3.0.jar
dom4j-1.6.1.jar
el-api-2.2.jar
geronimo-jta_1.1_spec-1.1.1.jar
hibernate-commons-annotations-5.0.1.Final.jar
hibernate-core-5.2.2.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-2.0.0.Final.jar
javassist-3.20.0-GA.jar
javax.inject-1.jar
jboss-interceptors-api_1.1_spec-1.0.0.Beta1.jar
jboss-logging-3.3.0.Final.jar
jsr250-api-1.0.jar
list.txt
mysql-connector-java-5.1.7-bin.jar
spring 4.3.8
aopalliance.jar
aspectjweaver.jar
commons-logging-1.1.1.jar
list.txt
spring-aop-4.3.8.RELEASE.jar
spring-aspects-4.3.8.RELEASE.jar
spring-beans-4.3.8.RELEASE.jar
spring-context-4.3.8.RELEASE.jar
spring-context-support-4.3.8.RELEASE.jar
spring-core-4.3.8.RELEASE.jar
spring-expression-4.3.8.RELEASE.jar
spring-instrument-4.3.8.RELEASE.jar
spring-instrument-tomcat-4.3.8.RELEASE.jar
spring-jdbc-4.3.8.RELEASE.jar
spring-jms-4.3.8.RELEASE.jar
spring-messaging-4.3.8.RELEASE.jar
spring-orm-4.3.8.RELEASE.jar
spring-oxm-4.3.8.RELEASE.jar
spring-test-4.3.8.RELEASE.jar
spring-tx-4.3.8.RELEASE.jar
spring-web-4.3.8.RELEASE.jar
spring-webmvc-4.3.8.RELEASE.jar
spring-webmvc-portlet-4.3.8.RELEASE.jar
spring-websocket-4.3.8.RELEASE.jar
配置web.xml
SSH2
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
action2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
action2
/*
contextConfigLocation
classpath:ApplicationContext.xml
org.springframework.web.context.ContextLoaderListener
配置struts2.xml
/index.jsp
配置ApplicationContext.xml
org.hibernate.dialect.MySQL5Dialect
true
true
false
update
ssh/entity/User.hbm.xml
采用最新的SSH配置需要配置log,避免输出警告
- log4j.properties
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
- log4j2.xml
在环境搭建完成以后编写测试文件
package test;
import org.hibernate.SessionFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testdemo {
@SuppressWarnings("resource")
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
SessionFactory bean = (SessionFactory) context.getBean("mysessionFactory");
System.out.println(bean);
}
}
进行项目分层
- 实体User
// default package
// Generated 2017-6-3 22:03:50 by Hibernate Tools 5.2.3.Final
package ssh.entity;
import java.util.Date;
/**
* User generated by hbm2java
*/
public class User implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String password;
private Integer qqnumber;
private String username;
private Date time;
public User() {
}
public User(String password, Integer qqnumber, String username, Date time) {
this.password = password;
this.qqnumber = qqnumber;
this.username = username;
this.time = time;
}
@Override
public String toString() {
return "User [id=" + id + ", password=" + password + ", qqnumber="
+ qqnumber + ", username=" + username + ", time=" + time + "]";
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getQqnumber() {
return this.qqnumber;
}
public void setQqnumber(Integer qqnumber) {
this.qqnumber = qqnumber;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getTime() {
return this.time;
}
public void setTime(Date time) {
this.time = time;
}
}
- 实体映射
- DAO层实现类
package ssh.daoimpl;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
import ssh.dao.UserDao;
import ssh.entity.User;
//创建IndexDaoImpl(实现类)实现IndexDao接口
@Transactional
public class UserDaoimpl implements UserDao {
// 在SSH的设计理念:要使用某个实例,那么就定义声明一个对象,然后
// 给它添加set方法(用于spring注入进来)
// 实现不要关注这个实例来自于那里,以及怎么创建,或者它是谁
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@SuppressWarnings("deprecation")
@Override
public List getAllUser() {
// sessionFactory这个实例可以自己按常规的hibernate传统写法创建
// 也可以交给spring去托管
/*
* onfiguration cfg = new Configuration().configure(); sessionFactory =
* cfg.buildSessionFactory();
*/
// 获取session
Session session = sessionFactory.getCurrentSession();
// 后面当使用JPA的时候,EntityManager 类似于 Session
List list = session.createQuery("from User").list();
for (User user : list) {
System.out.println(user.toString());
}
return list;
}
}
- Sevices实现类
package ssh.serviceimpl;
import java.util.List;
import ssh.dao.UserDao;
import ssh.entity.User;
import ssh.service.IndexService;
//创建IndexServiceImpl(实现类)实现IndexService接口
public class IndexServiceImpl implements IndexService {
// dao实例使用注入方式
private UserDao dao;
// 用于注入使用
public void setDao(UserDao dao) {
this.dao = dao;
}
// 本类应该编写业务逻辑的代码,
// 但本例没有业务逻辑,就不用写。
// 访问数据库的代码,不会出现在service这一层
// 交给dao来操作数据库
@Override
public List getAllUser() {
// 进行其它的业务逻辑操作,比如增加多一个选项,是否过期
// 本例不需要
// ....
List list = dao.getAllUser();
return list;
}
}
- Action 实现类 这里采用的spring来管理的action的实例,主要可以进行aop加强操作
package action;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import ssh.entity.User;
import ssh.service.IndexService;
public class registerAction extends ActionSupport{
//声明service,但不给它创建具体的实现类的实例,
private IndexService indexService;
//添加set()方法
public void setIndexService(IndexService indexService) {
this.indexService = indexService;
}
/**
* //编写execute()方法
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
List list = indexService.getAllUser();
ActionContext context = ActionContext.getContext();
context.put("userlist", list);
return ActionSupport.SUCCESS;
}
}
- jsp显示层
<%@ page language="Java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
??