ssh框架的搭建和整合(struts,spring,hibernate)
1.首先建包
File--->new--->dwp
2.导包
3.在配置文件web.xml配置一个struts2的过滤器和spring监听器。
<!-- listener监听 -->
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.在Java Resources下的src目录下创建四个包(package)进行分层。
5.根据数据库表的字段编写(实体类)。
package com.third.bean;
/**
- @author 登录变量
-
*/
public class User {private Integer id;
private String username;
private String password;public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
6.创建和hibernate.hbm.xml(映射文件)
点击com.hp.bean--->new--->other--->hibernate.hbm.xml.
<hibernate-mapping>
<class name="com.third.bean.User" table="USER">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
</class>
</hibernate-mapping>
7.在com.hp.service;com.hp.servic.impl包里编写com.hp.service(接口类)和com.hp.service.impl(实现类)。
接口类
public interface LoginService {
/** -
service层接口
*/
public List login(String username,String password);
public List login(String username);
public void addUser(User user);
public User findUser(String username,String password);
}
实现类
public class LoginServiceimpl implements LoginService{
private LoginDao logindao;public LoginDao getLogindao() {
return logindao;
}public void setLogindao(LoginDao logindao) {
this.logindao = logindao;
}@Override
public List login(String username, String password) {return logindao.login(username, password);
}
@Override
public void addUser(User user) {
logindao.addUser(user);}
@Override
public User findUser(String username, String password) {
return logindao.findUser(username, password);
}@Override
public List login(String username) {
return logindao.login(username);
}
}
8.在com.ho.dao;com.ho.dao.impl包里编写com.ho.dao(接口类)和com.ho.dao.impl(实现类)。
接口类
/**
- @author 登录Dao层
-
*/
public interface LoginDao {public List<User> login(String username,String password);
public List<User> login(String username);
public void addUser(User user);
public User findUser(String username,String password);
}
实现类
/** - @author 登录dao层
-
*/
public class LoginDaoimpl extends HibernateDaoSupport implements LoginDao{
private HibernateTemplate hibernatetemplate;public HibernateTemplate getHibernatetemplate() {
return hibernatetemplate;
}public void setHibernatetemplate(HibernateTemplate hibernatetemplate) {
this.hibernatetemplate = hibernatetemplate;
}@Override
public List<User> login(String username, String password) {List list=hibernatetemplate.find("from User where username=? and password=?", username,password);
if(list.size()>0){
return list;
}else{
return null;
}
}@Override
public void addUser(User user) {
hibernatetemplate.saveOrUpdate(user);}
@Override
public User findUser(String username, String password) {
String hql="from User where username=? and password=?";
List list= hibernatetemplate.find(hql, username,password);
//查询不到 list 空
if(list.size()>0){
return (User) list.get(0);
}else{
return null;
}
}
@Override
public List<User> login(String username) {
List list=hibernatetemplate.find("from User where username=?", username);
if(list.size()>0){
return list;
}else{
return null;
}
}
}
9.编写action类
/**
- @author 登录功能
-
*/
public class LoginAction extends ActionSupport {private String username;
private String password;
private LoginService loginservice;
private List<User> list;
private User user;
private Map map;
private List<User> users;public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public LoginService getLoginservice() {
return loginservice;
}
public void setLoginservice(LoginService loginservice) {
this.loginservice = loginservice;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//登录
public String login(){list=loginservice.login(username, password); boolean userflag; if (null==list) { userflag=false; }else { ActionContext.getContext().getSession().put("username", username); userflag=true; } map=new HashMap(); map.put("userflag", userflag); map.put("status", "ok"); return SUCCESS;
}
//注册
public String reg(){users=loginservice.login(username); boolean userflag; if (null==users) { userflag=false; loginservice.addUser(user); }else { //ActionContext.getContext().getSession().put("username", username); userflag=true; } map=new HashMap(); map.put("userflag", userflag); map.put("status", "ok"); return SUCCESS;
}
}
10:编写struts.xml(struts配置文件)、applicationContext.xml(spring配置文件)、hibernate.cfg.xml(hibernate配置文件)。
struts.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4 "http://struts.apache.org/dtds/struts-2.3.dtd">
5 <!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml -->
7 <struts>
8 <!-- 告知Struts2运行时使用Spring来创建对象 -->
9 <constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="UTF-8"/>
11 <!-- 第1步:先定义一个包 -->
12 <package name="struts" namespace="/" extends="struts-default">
13 <!-- 第2步:定义一个action,配置跳转信息 name 类似于Servlet @WebServlet("/IndexServlet")
14 http://xxxx/xxx/Index.action http://xxxx/xxx/Index class 对应于自己写的Action类 当不写method属性时,默认调用的是execute
15 class="ssh.action.IndexAction" ** new ssh.action.IndexAction()
16 设计思想:关心了具体的实现类必须改为不要关注那个实现类 加入spring后,struts的action节点的class属性意义发生变化,直接引用spring帮忙创建的实例
17 -->
18 <action name="login" class="login" method="login">
19 <!-- 跳转是forward/WEB-INF/是防止jsp不经过action就可以访问-->
20 <!-- result接收返回的字符串,然后做对应的事情 -->
21 <result name="success">/index.jsp</result>
22 <result name="input">/login.jsp</result>
</action>
23 </package>
24 </struts>
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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 登录的bean -->
<bean id="logindao" class="com.third.dao.impl.LoginDaoimpl" autowire="byType"></bean>
<bean id="loginservice" class="com.third.service.impl.LoginServiceimpl" autowire="byType"></bean>
<bean id="login" class="com.third.action.LoginAction" autowire="byType"></bean>
<!--创建 hiber模板类 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate" autowire="byType">
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="user" value="root"></property>
<property name="password" value=""></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost/item?characterEncoding=UTF-8"></property>
</bean>
<!-- 创建Sessio 工厂类 -->
<bean id="sessionfactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 创建实物管理对象 -->
<bean id="tx" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionfactory"> </property>
</bean>
<aop:config>
<aop:pointcut expression="execution( com.third.service.impl..*(..))" id="pointcut"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
<!-- 创建实物通知 -->
<tx:advice id="advice" transaction-manager="tx">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
</beans>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="configlocation">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/item?characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/third/bean/User.hbm.xml"/>
<mapping resource="com/third/bean/ComputerType.hbm.xml"/>
<mapping resource="com/third/bean/Computer.hbm.xml"/>
<mapping resource="com/third/bean/ComputerUser.hbm.xml"/>
<mapping resource="com/third/bean/Tree.hbm.xml"/>
<mapping resource="com/third/bean/OrderBean.hbm.xml"/>
<mapping resource="com/third/bean/OrderDetail.hbm.xml"/>
</session-factory>
</hibernate-configuration>
11:创建一个index.jsp页面将所有数据取出来显示到页面上。
<s:form action="login" method="post">
<s:textfield name="username" label="账号"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit value="登录"></s:submit>
</s:form>
总结:在SSH中使用Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持,Spring做支持控制反转,spring在struts基础是控制业务跳转,支持struts和hibernate。具体做法是:用面向对象的分析方法根据需求提出一些模型,将这些模型实现为基本的Java对象,然后编写基本的DAO(Data Access Objects)接口,并给出Hibernate的DAO实现,采用Hibernate架构实现的DAO类来实现Java类与数据库之间的转换和访问,最后由Spring做支持,支持struts和hibernate。其实ssh框架最主要的本质是:“高内聚、低耦合”。
共同学习,写下你的评论
评论加载中...
作者其他优质文章