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">

    
    
    <filter>
        <filter-name>OpenSessionInViewfilter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>OpenSessionInViewfilter-name>


        <url-pattern>*.actionurl-pattern>
    filter-mapping>

    
    <filter>
        <filter-name>struts2filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
    filter>
    <filter-mapping>
        <filter-name>struts2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:bean*.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    <welcome-file-list>
        <welcome-file>index.jspwelcome-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">

    
    
    
    <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>

    
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>
        
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>
                <prop key="hibernate.show_sql">trueprop>
                <prop key="hibernate.hbm2ddl.auto">updateprop>
            props>
        property>
        
        <property name="mappingLocations">
            <list>
                <value>classpath:com/eggtwo/entity/*.hbm.xmlvalue>
            list>
        property>
    bean>
    
    
    
    <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: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 name="show" class="studentAction" method="execute">
            <result name="success">/index.jspresult>
        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 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 request = (Map) ActionContext.getContext().get("request");
        request.put("student", student);
        return SUCCESS;
    }
}

action