zoukankan      html  css  js  c++  java
  • SSH简单项目

    这是我学习SSH整合时的一个测试项目,代码比较简单

    整个项目实现从数据库中取数据,在页面上显示。项目的结构如下:

    (1)数据库设计

    数据库使用的是student数据库中的一个数据库表grade,表的内容如下:

    (2)建立一个web项目—>导包。

    (3)新建配置文件,新建每一层的类。代码如下:

    配置文件: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"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <context:component-scan base-package="dao.impl,service.impl,controller"></context:component-scan>
    <!-- 连接数据库的配置 -->
    <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/student"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123"></property>
        <property name="maxActive" value="100"></property>
        <property name="maxWait" value="10000"></property>
    </bean>
    <!-- 连接数据库的配置 -->
    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mappingResources">
            <list>
                <value>entity/Grade.hbm.xml</value>
            </list>
        </property>
    </bean>
    <!-- 配置sessionFactory -->
    
    <!-- 配置GradeDaoImpl的bean -->
    <!-- <bean id="gradeDao" class="dao.impl.GradeDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>    配置dao.Impl的依赖,它依赖于sessionFactory,配置注解后就不需要配置这个了
    </bean>
    <bean id="gradeService" class="service.impl.GradeServiceImpl">
        <property name="gradeDao" ref="gradeDao"></property>
    </bean>
    配置GradeDaoImpl的bean
    
    定义事务管理器
    <bean name="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"
    >
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    定义事务管理器
    
    配置事务增强
        <tx:advice id="txAdvice" transaction-manager="txManager">
        声明事务的规则——具体看我的另一篇博客SSH的整合
        <tx:attributes>
            <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
            <tx:method name="query*" read-only="true" propagation="SUPPORTS"/>
            <tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="del*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
        </tx:advice>
    配置事务增强
    
    定义切面
    <aop:config>
    定义切入点
    <aop:pointcut expression="execution(* service.impl.*.*(..))" id="pointCut"/>  <!-- execution(* 包名.类名.方法名(..)) -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/> </aop:config> --> </beans>

    entity层:Grade.java

    package entity;
    
    public class Grade {
    
        private int gradeId;
        private String gradeName;
        public int getGradeId() {
            return gradeId;
        }
        public void setGradeId(int gradeId) {
            this.gradeId = gradeId;
        }
        public String getGradeName() {
            return gradeName;
        }
        public void setGradeName(String gradeName) {
            this.gradeName = gradeName;
        }
    }

    entity层:Grade.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-12-18 15:42:30 by Hibernate Tools 3.4.0.CR1 -->
    <hibernate-mapping>
        <class name="entity.Grade" table="GRADE">
            <id name="gradeId" type="int">
                <column name="GRADEID" />
                <generator class="assigned" />
            </id>
            <property name="gradeName" type="java.lang.String">
                <column name="GRADENAME" />
            </property>
        </class> 
    </hibernate-mapping>

    dao层:GradeDao.java

    package dao;
    
    import java.util.List;
    
    
    import entity.Grade;
    
    public interface GradeDao {
    
        /**
         * 查询所有年级的方法
         * @return
         */
        List<Grade> query_Grade();
        
    }

    dao.Impl:GradeDaoImpl.java

    package dao.impl;
    
    import java.util.List;
    
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    
    import dao.GradeDao;
    import entity.Grade;
    
    @Repository
    public class GradeDaoImpl implements GradeDao {
    
        @Autowired
        private SessionFactory sessionFactory;
        
        public List<Grade> query_Grade() {
        
            return sessionFactory.openSession().createQuery("from Grade").list();
        }
    
    }

    service:GradeService.java

    package service;
    
    import java.util.List;
    
    import entity.Grade;
    
    public interface GradeService {
    
        /**
         * 查询所有年级
         * @return
         */
        List<Grade> All_Grade();
    }

    service.impl:GradeServiceImpl.java

    package service.impl;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import service.GradeService;
    import dao.GradeDao;
    import entity.Grade;
    
    @Service
    public class GradeServiceImpl implements GradeService {
    
        @Autowired
        private GradeDao gradeDao;
        
        public List<Grade> All_Grade() {
            
            return gradeDao.query_Grade();
        }
    
    }

    test:GradeTest.java(这是一个中间的测试类,可以不要)

    package test;
    
    import java.util.List;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import service.GradeService;
    import dao.GradeDao;
    import entity.Grade;
    
    public class GradeTest {
        
        public static void main(String[] args) {
            System.out.println(111);
            ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
            GradeService gradeDao = (GradeService) act.getBean("gradeService");
            List<Grade> list = gradeDao.All_Grade();
            System.out.println(list.size());
        }
    }

    controller层:MyAction.java

    package controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    import service.GradeService;
    import entity.Grade;
    
    @Controller
    public class MyAction extends ActionSupport {
        
        @Autowired  //自动装配,配置这个注解后,就不用在application设置依赖,spring会自动帮你配置依赖关系,但需要在web.xml中配置,具体看web.xml
        private GradeService gradeService;
        private List<Grade> list;
        
        public GradeService getGradeService() {
            return gradeService;
        }
    
        public void setGradeService(GradeService gradeService) {
            this.gradeService = gradeService;
        }
    
        public String show(){
            System.out.println(111);
            list=gradeService.All_Grade();
            for (Grade grade : list) {
                System.out.println(grade.getGradeName());
            }
            return "success";
        }
    
        public List<Grade> getList() {
            return list;
        }
    
        public void setList(List<Grade> list) {
            this.list = list;
        }
    
    }

    界面显示:NewFile.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!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>
    <script type="text/javascript"></script>
    </head>
    <body>
        <c:forEach items="${list}" var="l">
            <p>${l.gradeId}&nbsp&nbsp${l.gradeName}</p>
        </c:forEach>
    </body>
    </html>

    struts配置文件: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="default" namespace="/" extends="struts-default">
            <action name="show" class="controller.MyAction" method="show">
                <result name="success">/NewFile.jsp</result>
            </action>
        </package>
    </struts>

    web.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
      <display-name>SSH_Framework</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
        <listener> <!-- 配置监听器 -->
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        
        <context-param>  <!-- 配置一组键值对,与<listener>对应,当服务器启动时,服务器会读取web.xml配置,当读到<listener></listener>和<context-param></context-param>这两个节点的时候,容器会将这两个节点set到ServletContext(上下文对象)中,这样我们在程序中就能通过这个上下文对象去取得我们这个配置值。 -->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
      
      <filter>   <!-- 配置一个过滤器 -->
          <filter-name>struts</filter-name>
          <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  <!-- struts给出的一个过滤器,或自动把匹配到的地址信息交由struts的配置文件处理 -->
      </filter>
      <filter-mapping>  <!-- 配置过滤映射,当页面的访问地址为.action结尾时,调用交由filter-name为struts的过滤器处理 -->
          <filter-name>struts</filter-name>
          <url-pattern>*.action</url-pattern>
      </filter-mapping>
    </web-app>
  • 相关阅读:
    1.saltstack入门
    构造函数
    gitlab
    mysql常见的问题
    python数据运算
    BZOJ 1691 usaco2007 挑剔的美食家
    BZOJ 1708 usaco2007 Money奶牛的硬币
    BZOJ 1050 HAOI2006 旅行comf
    BZOJ 2427 HAOI2010 软件安装
    BZOJ 1901 Dynamic Rankings
  • 原文地址:https://www.cnblogs.com/365txrw/p/SSH_SimpleJava.html
Copyright © 2011-2022 走看看