java web开发入门五(ssh整合)基于intellig idea

SSH整合

 

 

 

1.引入jar包

Struts 核心jar

Hibernate 核心jar

Spring

      Core  核心功能

      Web  对web模块支持

      Aop   aop支持

       Orm   对hibernate支持

        Jdbc/tx  jdbc支持包、事务相关包

2.配置xml

 *.hbm.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.eggtwo.entity">

    <class name="Student" table="t_student">
        <id name="id" column="id">
            <generator class="native"></generator>
        </id>
        <!-- 外键映射:多对一 -->
        <many-to-one name="grade" column="gradeId" class="Grade"></many-to-one>
        <property name="name"></property>
        <property name="age"></property>
        <property name="birthday"></property>

    </class>

</hibernate-mapping>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!-- 配置spring的OpenSessionInView模式 【目的:JSp页面访问懒加载数据】 -->
    <!-- 注意:访问struts时候需要带上*.action后缀,这样才能访问懒加载数据 -->
    <filter>
        <filter-name>OpenSessionInView</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInView</filter-name>

<!--url请求设置-->
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

    <!-- 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>/*</url-pattern>
    </filter-mapping>

    <!-- Spring配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:bean*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

bean.xml

每一个包中单独一个bean

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd">

    
    <!-- 所有配置的公共部分 -->
    <!-- 1) 数据源对象: C3P0连接池实例 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="6"></property>
    </bean>

    <!-- 2) SessionFactory实例创建 -->
    <!-- 所有的配置都由spring维护(项目中不需要hibernate.cfg.xml啦) -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- a. 连接池 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- b. hibernate常用配置: 方言、显示sql、自动建表等 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!-- c. 映射配置 -->
        <property name="mappingLocations">
            <list>
                <value>classpath:com/eggtwo/entity/*.hbm.xml</value>
            </list>
        </property>
    </bean>
    
    <!-- 3) 事务配置 -->
    <!-- # 事务管理器 -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- # 事务增强 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    <!-- # AOP配置 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.eggtwo.service.*.*(..))" id="pt"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
    
</beans>

 

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="myAction" extends="struts-default">
        <!-- action实例交给spring容器创建 -->
        <!--注意:action的class不能写全称类,要写bean-action中的 bean id-->
        <!-- 这种写法是把action交给tomcat创建,所以类要写全称
        <action name="show" class="com.eggtwo.action.StudentAction" method="execute">
                    <result name="success">/index.jsp</result>
        </action>
        -->
        <action name="show" class="studentAction" method="execute">
            <result name="success">/index.jsp</result>
        </action>

    </package>

</struts>

 

3.开发 

Entity/Dao/service/action

 Entity

package com.eggtwo.entity;

import java.util.Date;

public class Student {
    private int id;
   // private int gradeId;
    private String name;
    private int age;
    private Date birthday;
    private boolean isMan;

    public Grade getGrade() {
        return grade;
    }

    public void setGrade(Grade grade) {
        this.grade = grade;
    }

    private  Grade grade;
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

//    public int getGradeId() {
//        return gradeId;
//    }
//
//    public void setGradeId(int gradeId) {
//        this.gradeId = gradeId;
//    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public boolean isMan() {
        return isMan;
    }

    public void setMan(boolean man) {
        isMan = man;
    }
}

entity实体对象

实体对象映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.eggtwo.entity">

    <class name="Student" table="t_student">
        <id name="id" column="id">
            <generator class="native"></generator>
        </id>
        <!-- 外键映射:多对一 -->
        <many-to-one name="grade" column="gradeId" class="Grade"></many-to-one>
        <property name="name"></property>
        <property name="age"></property>
        <property name="birthday"></property>

    </class>

</hibernate-mapping>

entity实体对象映射数据库XML文件

 

Dao

package com.eggtwo.dao;

import com.eggtwo.entity.Student;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.orm.hibernate4.HibernateTemplate;

import java.io.Serializable;
import java.util.List;

public class StudentDao {
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public  void  save(Student student){
        //HibernateTemplate hibernateTemplate=new HibernateTemplate(sessionFactory);
        sessionFactory.getCurrentSession().save(student);
    }
    public  Student findById(Serializable id){
        Student o = (Student) sessionFactory.getCurrentSession().get(Student.class, id);
        return o;
    }
//    public List<Student> findList(){
//        Session currentSession = sessionFactory.getCurrentSession();
//        currentSession.find()
//    }
}

数据访问

 

Service:事务处理在此包中

package com.eggtwo.service;

import com.eggtwo.dao.StudentDao;
import com.eggtwo.entity.Student;

import java.io.Serializable;

public class StudentService {
    private StudentDao studentDao;

    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
    public  void  save(Student student){
         studentDao.save(student);
    }
    public  Student findById(Serializable id){
        return  studentDao.findById(id);
    }
}

服务

 

action

package com.eggtwo.action;

import com.eggtwo.entity.Student;
import com.eggtwo.service.StudentService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.omg.PortableServer.REQUEST_PROCESSING_POLICY_ID;

import java.util.Map;

public class StudentAction extends ActionSupport {
    //IOC注入
    private StudentService studentService;

    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    @Override
    public String execute() throws Exception {
        int studentId = 2;
        Student student = studentService.findById(studentId);
        Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
        request.put("student", student);
        return SUCCESS;
    }
}

action

 

posted @ 2019-11-23 11:28  梦亦晓  阅读(359)  评论(0编辑  收藏  举报