MAVEN SSH的框架整合

  • SSH 是 struts,spring和hibernate 合成的一个集成框架,是目前目前比较流行的web应用程序开源框架,(这是官方的介绍,个人认为SSM要比它好一点了)

一.hibernate

1.引入hibernate

<!-- 引入Servlet依赖 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.0</version>
        <scope>provided</scope>

<!-- 引入Hibernate依赖 -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.12.Final</version>
    </dependency>

    <!-- 引入Mysql依赖 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.43</version>
    </dependency>

2.写个hibernate.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>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>

        <!-- 显示sql语句 -->
        <property name="show_sql">true</property>
        <!-- 格式化sql语句 -->
        <property name="format_sql">true</property>

        <!-- 关联映射文件 -->
        <mapping resource="com/guard/entity/StudentEntity.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
  • 3.写个实体类 并 配置一个映射文件
  • 这里写图片描述

映射文件:

<?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-12-18 0:26:15 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.guard.entity.StudentEntity" table="student">
        <id name="sid" type="int">
            <column name="SID" />
            <generator class="increment" />
        </id>
        <property name="sname" type="java.lang.String">
            <column name="SNAME" />
        </property>
        <property name="age" type="int">
            <column name="AGE" />
        </property>
    </class>
</hibernate-mapping>
  • 5.进行测试
public class TestHibernate {

    @Test
    public void test(){
        //测试Hibernate框架,对数据进行CRUD
        //读取hibernate配置文件
        Configuration configuration = new Configuration().configure();
        //通过configuration获取sessionfactory
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        //通过sessionfactory获取session
        Session session = sessionFactory.openSession();
        //开启事务
        Transaction transaction = session.beginTransaction();
        //操作
        session.save(new StudentEntity("admin", 18));
        //提交事务
        transaction.commit();
        //关闭
        session.close();
        sessionFactory.close();
    }
}

这里写图片描述
这里写图片描述
成功了

二.Spring

  • 1.引入依赖
<!-- 引入Spring依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>

    <!-- 引入c3p0数据库连接池 -->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.1</version>
    </dependency>

    <!-- 引入Hibernate整合Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>

    <!-- 引入spring-aspects:解析事务的表达式 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>
  • 2.写个db.properties
  • 注释hibernate

db.properties 把hibernate的数据库连接拿出来

uname=root
upass=1234
url=jdbc:mysql://localhost:3306/lt
driverClass=com.mysql.jdbc.Driver

initPoolSize=10
maxPoolSize=20

注释一部分hibernate 拿出来就注释

<?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>
       <!--  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property> -->

        <!-- 显示SQL语句 -->
        <property name="show_sql">true</property>
        <!-- 格式化SQL语句 -->
        <property name="format_sql">true</property>

        <!-- 关联映射文件 -->
       <!--  <mapping resource="com/guard/entity/StudentEntity.hbm.xml"/> -->

    </session-factory>
</hibernate-configuration>
  • 3.写applicationContext-public.xml配置sessionfactory,数据源,连接池,和事务
<?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"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context"
    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
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 引入db.properties -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置数据源:配置数据库连接池c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${uname}"></property>
        <property name="password" value="${upass}"></property>
        <property name="jdbcUrl" value="${url}"></property>
        <property name="driverClass" value="${driverClass}"></property>

        <property name="initialPoolSize" value="${initPoolSize}"></property>
        <property name="maxPoolSize" value="${maxPoolSize}"></property>
    </bean>

    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 引入数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 加载hibernate配置文件 -->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <!-- 加载映射文件 -->
        <property name="mappingLocations" value="classpath:com/guard/entity/*.hbm.xml"></property>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>    
    </bean>

    <!-- 配置事务的属性 -->
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- 配置事务的切点 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.guard.dao.*.*(..))" id="myPoint"/>
        <aop:advisor advice-ref="myAdvice" pointcut-ref="myPoint"/>
    </aop:config>
</beans>
  • 4 写applicationContext-biz.xml和applicationContext-dao的路径
<?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.xsd">

    <bean id="studentBizImpl" class="com.guard.biz.StudentBizImpl">
        <property name="studentDao" ref="studentDaoImpl"></property>
    </bean>
</beans>


<!-- dao--!>
<?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.xsd">

    <bean id="studentDaoImpl" class="com.guard.dao.StudentDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>
  • 写dao 和 biz
    这里写图片描述
package com.guard.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.guard.entity.StudentEntity;

public class StudentDaoImpl implements StudentDao{
    private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public Session getSession(){
        return sessionFactory.getCurrentSession();
    }
    public void add(StudentEntity studentEntity) {
        getSession().save(studentEntity);
    }

}

<!--biz --!>
package com.guard.biz;

import com.guard.dao.StudentDao;
import com.guard.entity.StudentEntity;

public class StudentBizImpl implements StudentBiz{

    private StudentDao studentDao;
    public StudentDao getStudentDao() {
        return studentDao;
    }
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
    public void add(StudentEntity studentEntity) {
        // TODO Auto-generated method stub
        studentDao.add(studentEntity);
    }

}
  • 5.测试
    这里写图片描述

    Struts

  • 1.引入依赖

<!-- 引入Struts2依赖 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.33</version>
        </dependency>

        <!-- struts2整合Spring的 插件包 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.5.12</version>
        </dependency>

        <!-- log4J -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
  • 2写个action
package com.guard.action;

import com.opensymphony.xwork2.ActionSupport;
import com.guard.biz.StudentBiz;
import com.guard.entity.StudentEntity;

public class StudentAction extends ActionSupport{
    private StudentEntity studentEntity;
    private StudentBiz studentBiz;
    public StudentEntity getStudentEntity() {
        return studentEntity;
    }
    public void setStudentEntity(StudentEntity studentEntity) {
        this.studentEntity = studentEntity;
    }
    public StudentBiz getStudentBiz() {
        return studentBiz;
    }
    public void setStudentBiz(StudentBiz studentBiz) {
        this.studentBiz = studentBiz;
    }
    public String add()throws Exception{
        studentBiz.add(studentEntity);
        return this.SUCCESS;
    }
}
  • applicationContext-action路径
<?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.xsd">

    <bean id="StudentAction" class="com.guard.action.StudentAction" scope="prototype">
        <property name="studentBiz" ref="studentBizImpl"></property>
    </bean>

</beans>
  • 3.配置struts.xml
<?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.3.dtd">


    <struts>
        <package name="myPackage" extends="struts-default">
            <action name="student*" class="studentAction" method="{1}">
                <result>/success.jsp</result>
            </action>
        </package>

    </struts>
  • 4.写个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>
    <form action="studentadd.action" method="post">
        姓名:<input type="text" name="student.sname"/><br/>
        年龄:<input type="text" name="student.age"/><br/>
        <input type="submit" value="添加"/><br/>
    </form>
</body>
</html>
  • 这个jsp看看上一个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>
success
</body>
</html>
  • 最后web.xml的配置
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-*.xml</param-value>
    </context-param>

  <!-- 加载struts2 -->
  <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>*.action</url-pattern>
  </filter-mapping>

  <!-- 加载spring -->


    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

这是一个路径的 还有一个是注解的 !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值