eclipse搭建ssh框架(三)

1、之前2篇已经整合struts2+spring,这篇把hibernate整合进去。


先给大家看下ssh整合完成后整个项目架构

这里写图片描述

2、导入hibernate的相关包(已经导入全部包的忽略这一步)

在applicationContext.xml中添加hibernate的配置(有些地方改成自己的即可),这样写后,就不用再创建hibernate.properties文件了。

 <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <!-- 指定连接数据库的驱动 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <!-- 指定连接数据库的URL -->
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <!-- 指定连接数据库的用户名 -->
        <property name="username" value="root" />
        <!-- 指定连接数据库的密码 -->
        <property name="password" value="root" />
    </bean>
    <!-- 配置SessionFactory -->
    <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>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.autocommit">true </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="sql_format">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <!-- 指定hibernate映射文件 -->
            <list>
                <value>com/entity/User.hbm.xml</value>
            </list>
        </property>
    </bean>

4、接下来测试对数据库的操作,创建com.entity包,并在下面根据数据库对应的表创建User实体类

package com.entity;

public class User {
    private int id;
    private String name;
    private String age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
}

同目录下创建实体类映射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">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
    <class name="com.entity.User" table="user">
        <!-- 主键 -->
        <id name="id">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="36" />
        </property>
        <property name="age" type="java.lang.String">
            <column name="age" length="36"  />
        </property>
    </class>
</hibernate-mapping> 

5、创建持久层,新建com.dao包,并在下面创建接口UserDao(我已经在里面建立了add的方法)

package com.dao;

import com.entity.User;

public interface UserDao {
    public void add(User user);
}

然后同目录下创建实现类UserDaoImpl

package com.dao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.entity.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {



    @Override
    public void add(User user) {
        getHibernateTemplate().save(user);
    }

}

6、创建service层,新建com.service包,创建接口UserService(我也添加了add方法)

package com.service;

import com.entity.User;

public interface UserService {
    public void add(User user);
}

同目录下创建实现类UserServiceImpl

package com.service;

import com.entity.User;
import com.dao.UserDao;

public class UserServiceImpl implements UserService {

private UserDao userDao;


    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    @Override
    public void add(User user) {
        userDao.add(user);
    }

}

7、在action包下创建RegAction类

package com.action;

import java.util.ArrayList;
import java.util.List;

import com.entity.User;
import com.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class RegAction extends ActionSupport{

    private UserService userService;
    private String name;
    private String age;


//这里采用set注入
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }


    public String execute(){
        User user=new User();
        user.setName(name);
        user.setAge(age);
        userService.add(user);
        return "success";

    }

}

8、因为这里用的是set注入,所以在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-3.0.xsd">


       <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <!-- 指定连接数据库的驱动 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <!-- 指定连接数据库的URL -->
        <property name="url" value="jdbc:mysql://localhost:3306/test" />
        <!-- 指定连接数据库的用户名 -->
        <property name="username" value="root" />
        <!-- 指定连接数据库的密码 -->
        <property name="password" value="root" />
    </bean>
    <!-- 配置SessionFactory -->
    <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>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.autocommit">true </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="sql_format">true</prop>
            </props>
        </property>
        <property name="mappingResources">
            <!-- 指定hibernate映射文件 -->
            <list>
                <value>com/entity/User.hbm.xml</value>
            </list>
        </property>
    </bean>


        <bean id="LoginAction" class="com.action.LoginAction" scope="prototype">
        </bean>


        <!-- Dao配置 -->
    <bean id="userDao" class="com.dao.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- Service配置 -->
    <bean id="userService" class="com.service.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
    <bean id="reg" class="com.action.RegAction" scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>
    </beans>  

9、在struts.xml加入相应reg的action即可

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="false" />
   <package name="struts2" extends="struts-default">
        <!-- 此处的class的内容要与Spring配置文件中的bean的id相同 -->
        <action name="login" class="LoginAction">
            <result name="success">/result.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
        <!-- 注册,即测试ssh整合的action) -->
         <action name="reg" class="reg">
            <result name="success">/regsuccess.jsp</result>
            <result name="input">/reg.jsp</result>
        </action>
    </package>

</struts>

10、在WebContent下创建2个jsp,reg.jsp和regsuccess.jsp


reg.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户演示界面</title>
</head>
<body>
    <form action="reg" method="post" name="regForm">
        <table>
            <tr>
                <td>姓名</td>
                <td><input type="text" name="name" size="15" /></td>
            </tr>
            <tr>
                <td>年龄</td>
                <td><input type="text" name="age" size="15" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="注册"></td>
            </tr>


        </table>
    </form>

</body>
</html>

11、regsuccess.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>注册成功</h1>
</body>
</html>

最后重启tomcat,打开直接访问你的reg.jsp的网址
http://localhost:8080/SSH/reg.jsp

这里写图片描述

点击注册,这里我偷懒没写注册失败的页面。

这里写图片描述

再去数据库看下,成功添加。(你也可以再写一个select方法,直接在注册成功的页面中展示数据库的信息)

这里写图片描述

这样搭建ssh框架便完成了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我叫小八

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值