创建数据库表及ORM映射文件
数据库SQL
drop database if exists TEST;
create database TEST;
use TEST;
drop table if exists USERS;
create table USERS (
UID INT AUTO_INCREMENT PRIMARY KEY,
USERNAME VARCHAR(20) NOT NULL UNIQUE,
PASSWORD VARCHAR(32) NOT NULL) ENGINE=MyISAM;
insert into USERS(USERNAME, PASSWORD) VALUES('admin', '21232f297a57a5a743894a0e4a801fc3');
insert into USERS(USERNAME, PASSWORD) VALUES('test', '098f6bcd4621d373cade4e832627b4f6');
User.java:
package test.bean;
public class User {
private int uid;
private String username;
private String password;
public int getUid() {
return this.uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
User.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="test.bean.User" table="USERS">
<id name="uid" type="int">
<column name="UID" />
<generator class="increment" />
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="20" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="32" />
</property>
</class>
</hibernate-mapping>
Struts2 Spring2 Hibernate3配置文件
web.xml:
<?xml version="1.0"?>
<web-app>
<display-name>SSH Test</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="maxPoolSize" value="40" />
<property name="minPoolSize" value="1" />
<property name="initialPoolSize" value="1" />
<property name="maxIdleTime" value="20" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>test/bean/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userDao" class="test.dao.impl.UserDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userService" class="test.service.impl.UserServiceImpl">
<property name="userDao">
<ref bean="userDao" />
</property>
</bean>
<bean id="Login" class="test.action.Login" scope="prototype">
<property name="userService">
<ref bean="userService" />
</property>
</bean>
</beans>
struts.xml:
<?xml version="1.0" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="test" extends="struts-default">
<action name="Login" class="Login">
<result>/success.jsp</result>
<result name="input">/index.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
</struts>
struts.properties:
struts.objectFactory=spring
struts.locale=en_GB
struts.i18n.encoding=GBK
log4j.properties:
log4j.rootLogger=info,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%p - %d{HH:mm:ss} %m%n
接下来就是程序部分
UserDAO.java:
package test.dao;
import test.bean.User;
public interface UserDAO {
public User find(String username);
}
UserDAOImpl.java:
package test.dao.impl;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import test.bean.User;
import test.dao.UserDAO;
public class UserDAOImpl implements UserDAO {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@SuppressWarnings("unchecked")
public User find(String username) {
Session session = null;
try {
session = sessionFactory.openSession();
} catch(HibernateException e) {
e.printStackTrace();
return null;
}
if(session != null) {
List<User> list;
try {
String hql = "from User u where u.username=?";
Query query = session.createQuery(hql);
query.setString(0, username);
list = query.list();
if(list.size() == 1)
return list.get(0);
else
return null;
} catch(HibernateException e) {
return null;
} finally {
session.close();
}
} else
return null;
}
}
UserService.java:
package test.service;
public interface UserService {
public boolean validate(String username, String password);
}
UserServiceImpl.java:
package test.service.impl;
import test.bean.User;
import test.dao.UserDAO;
import test.service.UserService;
public class UserServiceImpl implements UserService {
private UserDAO userDAO;
public UserDAO getUserDao() {
return this.userDAO;
}
public void setUserDao(UserDAO userDAO) {
this.userDAO = userDAO;
}
public boolean validate(String username, String password) {
User user = null;
user = userDAO.find(username);
if(user != null) {
if(password.equals(user.getPassword()))
return true;
else
return false;
} else
return false;
}
}
Login.java:
package test.action;
import test.bean.User;
import test.service.UserService;
import test.util.Encryption;
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport {
private static final long serialVersionUID = 1L;
private User user;
private UserService userService;
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getUserService() {
return this.userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public String execute() {
String username = user.getUsername();
String password = new Encryption(user.getPassword()).getCryptograph();
if(userService.validate(username, password))
return SUCCESS;
else
return ERROR;
}
}
Encryption.java:
package test.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Encryption {
private String s;
public Encryption(String s) {
this.s = s;
}
public String getCryptograph() {
String cryptograph = null;
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
if(md != null) {
byte[] b = md.digest(s.getBytes());
StringBuffer buf = new StringBuffer(b.length * 2);
for(int i = 0; i < b.length; i++) {
if((b[i] & 0xff) < 16)
buf.append("0");
buf.append(Long.toString(b[i] & 0xff, 16));
}
cryptograph = buf.toString();
return cryptograph;
} else
return null;
}
}
分享到:
相关推荐
论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts...
6. **文件结构**:尽管没有提供具体的文件列表,但一个标准的Struts2+Spring2+Hibernate3项目通常包含以下部分:源代码(src目录),配置文件(如struts.xml、spring配置文件、hibernate配置文件),Web资源(Web-...
struts2 + spring2 +hibernate3 struts2 + spring2 +hibernate3 struts2 + spring2 +hibernate3 struts2 + spring2 +hibernate3 struts2 + spring2 +hibernate3
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
Struts2、Spring2和Hibernate3是Java Web开发中的经典技术栈,被广泛应用于企业级应用的构建。这个实例结合了这三大框架,提供了一个完整的功能模块,包括用户注册、数据查询、搜索以及分页显示,非常适合初学者进行...
Struts2+Spring3+HibernateBBS源码.rar请同时下载Struts2+Spring3+HibernateBBS源码lib1.rar ,Struts2+Spring3+HibernateBBS源码.rar lib2.rar此为jar包。
Struts2+Spring+Hibernate 中的Action单元测试环境搭建 在软件开发中,单元测试是一种非常重要的测试方法,可以帮助我们确保代码的可靠性和稳定性。在 Struts2+Spring+Hibernate 框架中,对 Action 的单元测试环境...
总的来说,基于Struts2+Spring+Hibernate+MySql的注册登录系统是利用这些技术协同工作,实现了用户注册、登录的基本功能。Struts2处理请求,Spring管理组件和事务,Hibernate负责数据持久化,而MySql存储数据。...
Struts2+Spring+Hibernate和Struts2+Spring+Ibatis是两种常见的Java Web应用程序集成框架,它们分别基于ORM框架Hibernate和轻量级数据访问框架Ibatis。这两种框架结合Spring,旨在提供一个强大的、可扩展的、易于...
sshplus项目 Struts2+Spring2+Hibernate3整合演示part2 sshplus项目 Struts2+Spring2+Hibernate3整合演示part2 sshplus项目 Struts2+Spring2+Hibernate3整合演示part2
Struts2+Spring+Hibernate+JSP+MySQL5是一个经典的Java Web开发框架组合,也被称为SSH框架。这个项目申报系统是基于这些技术构建的,它提供了高效、灵活且可扩展的后端架构来处理复杂的业务逻辑和数据管理。以下是...
sshplus项目 Struts2+Spring2+Hibernate3整合演示part3 sshplus项目 Struts2+Spring2+Hibernate3整合演示part3 sshplus项目 Struts2+Spring2+Hibernate3整合演示part3
sshplus项目 Struts2+Spring2+Hibernate3整合演示 part1 sshplus项目 Struts2+Spring2+Hibernate3整合演示 part2 sshplus项目 Struts2+Spring2+Hibernate3整合演示 part3
Struts2+Spring2+Hibernate3 是一种经典的Java Web开发技术栈,用于构建高效、松耦合的企业级应用。这三个框架分别负责不同的职责:Struts2 提供MVC(Model-View-Controller)架构,Spring 提供依赖注入(DI)和面向...
《JavaEE实用开发指南:基于Weblogic+EJB3+Struts2+Hibernate+Spring》-- part2/3
Java Web整合开发实战--基于Struts 2+Hibernate+Spring.pdf 1章 Web的工作机制 2章 搭建Java Web开发环境 3章 JSP及其相关技术 2篇 表现层框架Struts技术 4章 Struts快速上手 5章 解密Struts之核心文件 6章 ...