经过css布局后
hibernate3中文文档http://download.csdn.net/detail/wsk1103/9657379
使用struts2.1+hibernate4.1+spring4.1,实现一个拥有简单的注册和登录的小程序
步骤
1. 使用myeclipce2016CI3创建一个名为ssh2的ssh2项目
2. 为myeclipce创建一个hibernate到MySQL的连接
3. 使用myeclipce自带的向导功能创建struts2.1框架
4. 创建spring4.1框架
5. 创建hibernate4.1框架
6. 利用hibernate的反射生成User.java(POJO)和User.hbm.xml
7. 修改applicationContext.xml文件
8. 编写DAO层代码(用于与数据库进行操作)
9. 编写service层代码(用于连接DAO层)
10. 编写action层(沟通jsp和service层)
11. 继续添加代码到applicationContext.xml中(注入)
12. 修改struts2.xml(响应jsp操作)
13. 编写jsp层
项目结构图
实现:
1. 使用myeclipce创建一个名为ssh2的项目
2. 为myeclipce创建一个hibernate连接MySQL的连接,
2.1 创建方法为 Windows->perspective->Open Perspective->MyEclipse Hibernate
2.2 在图中空白处右键->new一个新的数据库连接
2.3 然后创建连接
2.4 使用navicat forMySQL创建名为hibernate的数据库并创建user的表
3. 利用向导自动导入struts2.1包
URL要改成如图所示
4. 利用向导直接创建spring4.1,使用默认设置
5. 利用向导直接创建hibernate4.1
5.1使用刚才创建的hibernate-MySQL的连接,然后点击finish
6. 在刚才创建的user表反射生成userPOJO和user.hbm.xml(自动生成后自动注入到applicationContext.xml中)
6.1 反射操作:在user表右键->hibernate reverse engineering ,信息填写后finish
7. 修改applicationContext.xml文件,文中红字部分
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
property>//这个必须写上去,使用驱动,要不然连接不了数据库
<property name="url"
value="jdbc:mysql://localhost:3306/hibernate">
property>
<property name="username" value="root">property>
<property name="password" value="123456">property>
bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
prop>
<prop key="hibernate.show_sql">trueprop>//显示sql,便于调试,也可以不写
<prop key="hibernate.hbm2ddl.auto">updateprop>//更新数据库数据,可以不写
props>
property>
<property name="mappingResources">
<list>
<value>com/wsk/bean/User.hbm.xmlvalue>list>
property>bean>
8.编写DAO层,目的是通过hibernate操作数据库
8.1 编写接口
package com.wsk.dao;
public interface UserDAO{
public void register(T t);//注册
public boolean login(String user);//登录
}
8.2 实现接口
package com.wsk.dao;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class UserDAOImpl implements UserDAO {
private SessionFactory sessionFactory;
//登录,通过连接数据进行判断该用户是否存在或者密码是否正确
public boolean login(String user) {
//连接数据库进行判断
List list=this.getSession().createQuery(user).list();
//如果存在
if (!list.isEmpty()){
//转化形式
Iterator iterator=list.iterator();
String getString=(String) iterator.next();
if (getString!=null||getString!="")
return true;
}
return false;
}
//注册功能
public void register(T t) {
// 保存到数据库
this.getSession().save(t);
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public Session getSession(){
//hibernate4必须用openSession,不能用getCurrentSession
return sessionFactory.openSession();
}
}
9.编写service层,用于连接DAO层
package com.wsk.service;
import com.wsk.dao.UserDAOImpl;
public class Service {
private UserDAOImpl userDAOImpl;
public boolean doLogin(String name,String password){
if (name==null||password==null)
return false;
String sqlString="select u.password from "
+ "com.wsk.bean.User u where u.name='"+name
+"' and password='"+password+"'";
if (userDAOImpl.login(sqlString)){
return true;
}
return false;
}
public void register(T user){
userDAOImpl.register(user);
}
public UserDAOImpl getUserDAOImpl() {
return userDAOImpl;
}
public void setUserDAOImpl(UserDAOImpl userDAOImpl) {
this.userDAOImpl = userDAOImpl;
}
}
10.编写action层,用于连接jsp和service层
package com.wsk.action;
import com.opensymphony.xwork2.ActionSupport;
import com.wsk.bean.User;
import com.wsk.service.Service;
public class UserAction extends ActionSupport{
private String name;
private String password;
User user=new User();
Service service=new Service();
public String login(){
if (service.doLogin(name, password)){
return SUCCESS;
}
return ERROR;
}
public String register(){
if (name.isEmpty()||name==""||password.isEmpty()||password=="")
return ERROR;
user.setName(name);
user.setPassword(password);
service.register(user);
return SUCCESS;
}
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 Service getService() {
return service;
}
public void setService(Service service) {
this.service = service;
}
}
11.修改applicationContext.xml文件,实现注入,
id="dao" class="com.wsk.dao.UserDAOImpl">
<property name="sessionFactory" ref="sessionFactory">property>
id="service" class="com.wsk.service.Service">
<property name="userDAOImpl" ref="dao">property>
id="useraction" class="com.wsk.action.UserAction">
<property name="service" ref="service">property>
总:applicationContext.xml
version="1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx">
id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
property>
<property name="url"
value="jdbc:mysql://localhost:3306/hibernate">
property>
<property name="username" value="root">property>
<property name="password" value="123456">property>
id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
property>
<property name="hibernateProperties">
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
prop>
<prop key="hibernate.show_sql">trueprop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
property>
<property name="mappingResources">
<list>
com/wsk/bean/User.hbm.xml list>
property>
id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
transaction-manager="transactionManager" />
id="dao" class="com.wsk.dao.UserDAOImpl">
<property name="sessionFactory" ref="sessionFactory">property>
id="service" class="com.wsk.service.Service">
<property name="userDAOImpl" ref="dao">property>
id="useraction" class="com.wsk.action.UserAction">
<property name="service" ref="service">property>
12.修改struts2.xml,文中红色部分
<struts>
<package name="1" extends="struts-default">
<action name="useraction" class="com.wsk.action.UserAction">
<result>success.jspresult>
<result name="error">error.jspresult>
action>
package>
struts>
13.编写JSP
13.1 编写登录界面login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>logintitle>
head>
<body>
<form action="useraction!login" method="post">
用户名:<input type="text" name="name"><br>
密 码:<input type="password" name="password"><br>
<input type="submit" value="登录"><br>
form>
<a href="register.jsp">注册a>
bod1y>
html>
13.2 编写注册界面 register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>registertitle>
head>
<body>
<form action="useraction!register" method="post">
用户名:<input type="text" name="name"><br>
密 码:<input type="password" name="password"><br>
<input type="submit" value="确定">
form>
<a href="index.jsp">返回a>
body>
html>
13.3 编写登录成功页面 success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>successtitle>
head>
<body>
<s:property value="name"/> welcom.<br>
<a href="index.jsp">返回a>
body>
html>
13.4 编写登录失败页面error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>errortitle>
head>
<body>
登录错误<br>
<a href="index.jsp">返回a>
body>
html>
总结:
1. 第一次配置好的时候,忘记在applicationContext.xml中添加
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
property>
使得连接数据库的时候找不到驱动,报错
org.hibernate.exception.GenericJDBCException: Could not open connection
2. spring整合hibernate4.1的时候,要获取session的时候,使用getCurrentSession,别用openSession. (原因和如何使用getCurrentSession 见连接 http://blog.csdn.net/wsk1103/article/details/53222352)
public Session getSession(){
return sessionFactory.getCurrentSession();
}
该会话会自动开启事务和关闭事务
3. applicationContext.xml注入的时候,property 中的name必须存在于该类中,而且要拥有get和set方法
4. 项目下载地址 http://download.csdn.net/detail/wsk1103/9633990
5. 暂时版本http://blog.csdn.net/wsk1103/article/details/52783346