zoukankan      html  css  js  c++  java
  • JavaWeb学习:Struts和Hibernate案例

    一、开发环境搭建

      ①、创建web项目,引入jar包

      • Struts2
      • Hibernate

      ②、配置文件

      • Struts2
        • struts.xml
        • web.xml(配置过滤器)
      • Hibernate
        • 核心配置(hibernate.cfg.xml)
        • 映射文件(类名.hbm.xml)
        • 日志文件(log4j.properties)

      ③、搭建项目框架

        

     二、编写index.html、持久化类、Action、Service、DAO、list.jsp

      ①、index.html

    <A class=style2 href="customer_find.action" target=main> 客户列表</A>

      ②、Customer持久化类

      • Customer
      • Customer.hbm.xml

        Customer

    package com.xxx.domain;
    
    /**
    * @Title: Customer
    * @Description: 
    * @author: marw
    * @date 2020/11/03 10:56:17
    */
    public class Customer {
        private Long cust_id;
        private String cust_name;
        
        private String cust_source;
        private String cust_industry;
        private String cust_level;
        private String cust_phone;
        private String cust_mobile;

       Customer.hbm.xml

    <hibernate-mapping>
        <!-- 建立类与表的映射 -->
        <class name="com.xxx.domain.Customer" table="cst_customer" batch-size="6">
            <!-- 建立类中的属性与表中的主键对应 -->
            <id name="cust_id" column="cust_id">
                <generator class="native"></generator>
            </id>
    
            <!-- 建立类中的普通属性与表中的字段对应 -->
            <property name="cust_name" column="cust_name"></property>
            <property name="cust_source" column="cust_source"></property>
            <property name="cust_industry" column="cust_industry"></property>
            <property name="cust_level" column="cust_level"></property>
            <property name="cust_phone" column="cust_phone"></property>
            <property name="cust_mobile" column="cust_mobile"></property>
            
        </class>
    </hibernate-mapping>

      ③、DAO,只写写接口实现类,CustomerDao接口定义方法,用于Service层的调用

    public class CustomerDaoImpl implements CustomerDao {
    
        @Override
        public List<Customer> find() {
        Session session=HibernateUtils.getCurrentSession();
         Transaction tran= session.beginTransaction();
         List<Customer> list=session.createQuery("from Customer").list();
         tran.commit();
        return list;
        }

      ④、Service,Service的接口实现类,Service接口定义方法,用于Action调用

    public class CustomerServiceImpl implements CustomerService {
    
        @Override
        public List<Customer> find() {
        CustomerDao customerDao=new CustomerDaoImpl();
        
        return customerDao.find();
        }
    
    }

      ⑤、Action,数据处理,页面跳转并传值

    public class CustomerAction extends ActionSupport {
        public String find() {
        CustomerService customerService=new CustomerServiceImpl();
        List<Customer> list =customerService.find();
        //页面跳转,传值
        ServletActionContext.getRequest().setAttribute("list", list);
        return "findSuccess";
        }
    }

      ⑥、list.jsp 

        引入标签库:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

        引入标签库之前需要导入:jstl.jar、standard.jar

        <TABLE id=grid
            style="BORDER-TOP-WIDTH: 0px; FONT-WEIGHT: normal; BORDER-LEFT-WIDTH: 0px; BORDER-LEFT-COLOR: #cccccc; BORDER-BOTTOM-WIDTH: 0px; 
    BORDER-BOTTOM-COLOR: #cccccc; WIDTH: 100%; BORDER-TOP-COLOR: #cccccc; FONT-STYLE: normal; BACKGROUND-COLOR: #cccccc; BORDER-RIGHT-WIDTH: 0px;
    TEXT-DECORATION: none; BORDER-RIGHT-COLOR: #cccccc"
    cellSpacing=1 cellPadding=2 rules=all border=0> <TBODY> <TR style="FONT-WEIGHT: bold; FONT-STYLE: normal; BACKGROUND-COLOR: #eeeeee; TEXT-DECORATION: none"> <TD>客户名称</TD> <TD>客户级别</TD> <TD>客户来源</TD> <TD>客户所属行业</TD> <TD>电话</TD> <TD>手机</TD> <TD>操作</TD> </TR> <c:forEach items="${list }" var="customer"> <TR style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none"> <TD>${customer.cust_name }</TD> <TD>${customer.cust_level }</TD> <TD>${customer.cust_source }</TD> <TD>${customer.cust_industry }</TD> <TD>${customer.cust_phone }</TD> <TD>${customer.cust_mobile }</TD> <TD> <a href="${pageContext.request.contextPath }/customerServlet?method=edit&custId=${customer.cust_id}">修改</a> &nbsp;&nbsp; <a href="${pageContext.request.contextPath }/customerServlet?method=delete&custId=${customer.cust_id}">删除</a> </TD> </TR> </c:forEach> </TBODY> </TABLE>

    三、配置

      ①、web.xml配置过滤器

        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.jsp</welcome-file>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
        </welcome-file-list>

      ②、struts.xml配置请求处理Action和Action返回值的处理

    <struts>
        <package name="crm" extends="struts-default" namespace="/">
            <global-allowed-methods>regex:.*</global-allowed-methods>
            <action name="customer_*" class="com.xxx.web.action.CustomerAction" method="{1}">
            <result name="findSuccess">/jsp/customer/list.jsp</result>
            </action>
        </package>
    </struts>

      ③、hibernate.cfg.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.microsoft.sqlserver.jdbc.SQLServerDriver</property>
            <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=HibernateDB;</property>
            <property name="hibernate.connection.username">sa</property>
            <property name="hibernate.connection.password">AAA@111</property>
            
            <!-- 配置Hibernate的方言 -->
            <property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
            <!-- 可选配置start -->
            <!-- 控制台打印sql语句 -->
            <property name="hibernate.show_sql">true</property>
            <!-- 控制台打印sql语句 格式化-->
            <property name="hibernate.format_sql">true</property>
            
            <property name="hibernate.hbm2ddl.auto">update</property>
            <!-- 
            事务隔离级别
                1-Read uncommitted 
                2-Read committed
                4-Repeatable read
                8-Serializable
             -->
            <property name="hibernate.connection.isolation">2</property>
            
            <!-- 配置session绑定本地线程 
            thread:Session对象的生命周期与本地线程一致,线程关闭session关闭,所以不需要手动关闭(session.close())
            jta:session对象的生命周期与JTA事务绑定(跨数据库的)
            managed:Hibernate委托程序来关联Session对象的生命周期
            -->
            <property name="hibernate.current_session_context_class">thread</property>
            <!-- 可选配置end -->
            
            <!-- 配置C3P0连接池 -->
            <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
            <!--在连接池中可用的数据库连接的最少数目 -->
            <property name="c3p0.min_size">5</property>
            <!--在连接池中所有数据库连接的最大数目  -->
            <property name="c3p0.max_size">20</property>
            <!--设定数据库连接的过期时间,以秒为单位,
            如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
            <property name="c3p0.timeout">120</property>
             <!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
            <property name="c3p0.idle_test_period">3000</property>
            
            <!-- 配置映射 -->
            <mapping resource="com/xxx/domain/Customer.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>
  • 相关阅读:
    C++中pair的用法
    DFS例题:力扣200:岛屿数量
    DFS例题:力扣695:岛屿的最大面积
    DFS深度优先遍历
    java AQS源码分析
    spring实现事务原理
    java常见并发面试题目+示例代码
    java并发锁
    ThreadPoolExecutor
    线程通信
  • 原文地址:https://www.cnblogs.com/WarBlog/p/14031350.html
Copyright © 2011-2022 走看看