SSH框架搭建

来源:互联网 发布:js随机数1到22 编辑:程序博客网 时间:2024/06/08 05:21

SSH框架的搭建(使用XML)

博主书写此博客主要是为了给大家提供SSH搭建的干货,博主也走了很多弯路,在此给大家提供SSH搭建的完整流程
博主在此处使用的版本是:Spring5.0.0;struts2-2.5.12;Hibernate5

编写流程

  • SSH框架的搭建使用XML
    • 编写流程
    • 引入所需要的Jar包
    • hibernate配置
    • hibernatecfgxml
    • 编写strutsxml
    • 编写applicationContextxml
    • 编写实体类User以及Userhbmxml
    • 编写BaseDao接口以及UserDao实现接口
    • 编写UserAction
    • 编写测试类
    • 博主在此分享经验仅供参考有BUG可以在下方评论留言


引入所需要的Jar包

这里使用C3P0数据源,在此不多做介绍与往常的jdbc配置并无不同

hibernate配置

hibernate.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!-- 设置打印mysql语句 -->        <property name="hibernate.show_sql">true</property>        <!-- 设置数据库方言,inno支持外键 -->        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>        <!-- 设置编译的时候对数据库是进行的操作,博主在此使用更新 -->        <property name="hibernate.hbm2ddl.auto">update</property>    </session-factory></hibernate-configuration>

编写struts.xml

当设置了通配符访问的时候,博主采用<a href="User_queryUserall.do">来访问查询

 <!-- 这句话一定要加,把Struts交给Springioc容器--><?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"    "http://struts.apache.org/dtds/struts-2.5.dtd"><struts><!-- 这句话一定要加,是吧Struts交给SpringIOC容器 -->    <constant name="struts.objectFactory" value="spring" />    <!-- 设置开发者模式 -->    <constant name="struts.devMode" value="true" />    <!-- 设置访问名为.do -->    <constant name="struts.action.extension" value="do" />    <!-- 打开自动编译 -->    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>    <package name="struts" extends="struts-default">      <global-allowed-methods>regex:.*</global-allowed-methods>        <!-- 使用*_*通配符访问 -->        <action name="*_*" class="userAction" method="{2}">            <result name="{2}">/{1}{2}.jsp</result>        </action>    </package></struts>

编写applicationContext.xml

<!---配置扫描的包名,ioc会自动初始化这些包名-->     <context:component-scan base-package="com.bean"></context:component-scan>   <context:component-scan base-package="com.dao"></context:component-scan>   <context:annotation-config></context:annotation-config>   <!-----------------注释事务的开始-->   <tx:annotation-driven  transaction-manager="txManager"/>    <!------------------配置数据源-->   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springc3p0"></property>        <property name="user" value="root"></property>        <property name="password" value="root"></property>    </bean>    <!----------------配置session工厂-->        <bean id="sessionFactory"        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">        <property name="dataSource" ref="dataSource"></property>        <!---------设置扫描的配置文件路径-->        <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>        <!----------设置映射文件路径-->        <property name="mappingLocations" value="classpath:com/bean/*.hbm.xml"></property>    </bean>    <!--------------配置事务管理器-->    <bean name="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">           <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!--------------实例化aop的bean-->    <bean id="time" class="com.aop.Time"></bean>    <!-------------配置aop-->     <aop:config>       <aop:aspect id="aspect" ref="time">    <!-----------------------------------------------  *是返回值,dao.*代表所有方法,..代表形参-->                    <aop:pointcut expression="execution(* com.dao.BaseDao.*(..))" id="pointcut"/>            <aop:before method="init" pointcut-ref="pointcut"/>            <aop:after method="end"  pointcut-ref="pointcut"/>        </aop:aspect>    </aop:config></beans>

编写实体类User以及User.hbm.xml

实体类User(使用注解来注入User)

@Entitypublic class User {    private int id;    private String username;    private String money;    @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getMoney() {        return money;    }    public void setMoney(String money) {        this.money = money;    }}

映射文件User.hbm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2017-10-17 13:45:55 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping>    <class name="com.bean.User" table="USER">        <id name="id" type="int">            <column name="ID" />            <generator class="assigned" />        </id>        <property name="username" type="java.lang.String">            <column name="USERNAME" />        </property>        <property name="money" type="java.lang.String">            <column name="MONEY" />        </property>    </class></hibernate-mapping>

编写BaseDao接口以及UserDao实现接口

BaseDao:

public interface BaseDao {    List<User> queryUserall();}

UserDao

package com.dao;import java.util.List;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.query.Query;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Component;import org.springframework.stereotype.Repository;import org.springframework.stereotype.Service;import com.bean.User;public class UserDao implements BaseDao{    private SessionFactory factory;    @Override    public List<User> queryUserall() {        // TODO Auto-generated method stub        Session session=factory.openSession();        Query<User> query=session.createQuery("From User");        List<User> list=query.list();        for (User user : list) {            System.out.println(user.getUsername());        }        return list;    }    public SessionFactory getFactory() {        return factory;    }    public void setFactory(SessionFactory factory) {        this.factory = factory;    }}

编写UserAction

UserAction:
`public class UserAction extends ActionSupport{
private BaseDao basedao;
private List list;
public String queryUserall() {
this.basedao=(BaseDao)new ClassPathXmlApplicationContext(“applicationContext.xml”).getBean(“baseDao”);
list=basedao.queryUserall();

return "queryUserall";}public List<User> getList() {    return list;}public void setList(List<User> list) {    this.list = list;}public BaseDao getBasedao() {    return basedao;}public void setBasedao(BaseDao basedao) {    this.basedao = basedao;}}

编写测试类

Test:

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");        BaseDao baseDao=(BaseDao)context.getBean("baseDao");        System.out.println(baseDao.queryUserall().get(0).getId());

博主在此分享经验,仅供参考,有BUG可以在下方评论留言

1:生命不息,学习不止,博主在此对自己学习经验做一个记录,也给广大其他码友们提供一点微薄的经验,如有不足之处,请在下方批评指正,Thanks


原创粉丝点击