zoukankan      html  css  js  c++  java
  • 用spring+hibernate+struts 项目记录以及常用的用法进等

    一、hibernate
    1.
    -----BaseDao------
    // 容器注入
    private SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
    }

    public SessionFactory getSessionFactory() {
    return sessionFactory;
    }


    2.
    ---通过id查找
    public T findById(int id) {
    return (T) sessionFactory.getCurrentSession().get(clazz, id);
    }

    @Override
    public void delete(int id) {
    sessionFactory
    .getCurrentSession()
    .createQuery("delete from " + className + " where id=?")
    .setParameter(0, id).executeUpdate();
    }

    3.
    ---通过id查找
    @Override
    public Employee findById(int id) {
    // String hql = "from Employee e left join fetch e.dept where e.id=?";
    String hql = "from Employee e where e.id=?";
    return (Employee) getSessionFactory()
    .getCurrentSession()
    .createQuery(hql)
    .setParameter(0, id)
    .uniqueResult();
    }

    4.
    -------通过id删除
    public void delete(int id) {
    sessionFactory
    .getCurrentSession()
    .createQuery("delete from " + className + " where id=?")
    .setParameter(0, id).executeUpdate();
    }
    -------返回链表
    EmployeeDao
    @SuppressWarnings("unchecked")
    @Override
    public List<Employee> getAll(String employeeName) {
    return getSessionFactory().getCurrentSession()//
    .createQuery("from Employee where empName like ?")//
    .setParameter(0, "%" +employeeName + "%")//
    .list();
    }

    @Override
    public Employee findById(int id) {
    // String hql = "from Employee e left join fetch e.dept where e.id=?";
    String hql = "from Employee e where e.id=?";
    return (Employee) getSessionFactory()
    .getCurrentSession()
    .createQuery(hql)
    .setParameter(0, id)
    .uniqueResult();
    }


    5.
    查找全部的
    @Override
    public List<Employee> getAll(String employeeName) {
    return getSessionFactory().getCurrentSession()//
    .createQuery("from Employee where empName like ?")//
    .setParameter(0, "%" +employeeName + "%")//
    .list();
    }


    --------下拉链表--------
    <!--
    Struts下拉列表标签:
    name="deptId" 下拉列表标签的名称(服务器根据这个名称获取选择的项的实际的值value值)
    headerKey 默认选择项的实际的值
    headerValue 默认下拉列表显示的内容
    list 下拉列表显示数据的集合
    listKey 集合对象的哪个属性作为下拉列表的实例的值,即value值
    listValue 集合对象的哪个属性作为下拉列表显示的值
    value 默认选择的项的设置
    -->


    <s:select
    name="deptId"
    headerKey="-1"
    headerValue="请选择"
    list="#request.listDept"
    listKey="id"
    listValue="name"
    value="-1"
    ></s:select>



    6.
    //struts2的ognl 的iterator 跳出循环
    做项目遇到一个需求,前台已经获取到了一个小组所有人的昵称,需要展示出一个管理员的昵称,但是小组中又有普通成员又有管理员,而且管理员不一定只有一个。
    在用s:iterator遍历这个小组成员时需要过滤出管理员,然后在找到第一个管理员以后停止循环,下面是我的代码:
    [javascript] view plain copy print?
    <s:set name="index" value="1" />
    <s:iterator value="#detail.members" var="memeber" id="memeber" status="status">
    <s:if test='%{#memeber.attr1 == "admin" }'>
    <s:if test="#index==1">
    <s:set name="index" value="2" />
    <s:property value="#memeber.nickName"/>
    </s:if>
    </s:if>
    </s:iterator>

    s:if可以合成一个,这里写成两个方便大家理解,这样做的实质没有跳出循环,但是巧妙的控制了循环的次数。

    7. request进行传递数据
    可以通过这样方式给页面进行传数据,将数据放到栈里。
    这块要有这个 Map<String, Object> request;

    cn.dy.action.GoodsAction
    public class GoodsAction extends ActionSupport implements ModelDriven<Goods>, RequestAware{
    // 接收框架运行时候传入的代表request对象的map
    private Map<String, Object> request;
    @Override
    public void setRequest(Map<String, Object> request) {
    this.request = request;
    }
    }


    8.如何放入值栈
    //why为啥 这块要进将数据进行pop ,而后进行push进行
    public String viewUpdate(){
    // 获取要修改的记录的id
    int id = employee.getId();

    // 1. 根据员工的主键查询 (lazy="false")
    Employee emp = employeeService.findById(id); // 已经有部门信息
    // 2. 查询所有的部门
    List<Dept> listDept = deptService.getAll();

    // 数据回显
    ValueStack vs = ActionContext.getContext().getValueStack();
    vs.pop(); // 移除栈顶元素
    vs.push(emp); // 入栈
    // 保存
    request.put("listDept", listDept);

    return "edit";
    }

    //不继承BaseDao 的普通dao写法
    public class DeptDao implements IDeptDao {
    //
    private SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
    }

    @Override
    public Dept findById(int id) {
    return (Dept) sessionFactory.getCurrentSession().get(Dept.class, id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Dept> getAll() {
    //查出来的数据放在list
    return sessionFactory.getCurrentSession().createQuery("from Dept").list();
    }

    }

    9.在hibernate中,要注意的是,

      a.注意:在使用sessionFactory中要十分注意不同类之间不可共用一个sessionFactory ,否则会报 Struts has

       * detected an unhandled exception:
         * Messages: File: cn/dy/dao/impl/RepertoryDao.java Line number: 48

      b.在进行用hibernate中, 要进查找, 特别设计到外键后, 注意, 用jdbc是无法进行完成其外键的查询的

      c. hibernate之处理视图 

    二、spring 

    1.配置bean.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: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">

    <!-- 引入其他配置文件 -->
    <import resource="config/bean-base.xml"/>
    <import resource="config/bean-dao.xml"/>
    <import resource="config/bean-service.xml"/>
    <import resource="config/bean-action.xml"/>
    </beans>

     

     2.bean-action.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: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">

    <!-- Action中需要注入Service -->

    <!-- 1. 员工管理模块 -->
    <bean id="employeeAction" class="cn.dy.action.EmployeeAction" scope="prototype">
    <property name="employeeService" ref="employeeService"></property>
    <property name="deptService" ref="deptService"></property>
    </bean>

    <!-- 2. 管理员模块 -->
    <bean id="adminAction" class="cn.dy.action.AdminAction" scope="prototype">
    <property name="adminService" ref="adminService"></property>
    </bean>

    <!-- 商品的管理模块 -->
    <bean id="goodsAction" class="cn.dy.action.GoodsAction" scope="prototype">
    <property name="goodsService" ref="goodsService"></property>
    <property name="categoryService" ref="categoryService"></property>
    <property name="repertoryService" ref="repertoryService"></property>
    </bean>

    <!-- 入库订单的管理模块 -->
    <bean id="storeGdsFormAction" class="cn.dy.action.StoreGdsFormAction" scope="prototype">
    <property name="storeGdsFormService" ref="storeGdsFormService"></property>
    <property name="goodsServer" ref="goodsService"></property>
    <property name="recordStoreGdsServers" ref="recordStoreGdsServers"></property>
    </bean>

    <!-- 分页显示入库单 -->
    <bean id="showSeparatePageStoreGdsAction" class="cn.dy.action.showSeparatePageStoreGdsAction" scope="prototype">
    <property name="storeGdsFormService" ref="storeGdsFormService"></property>
    </bean>

    <!-- 表单ajax 验证 进行注入 -->
    <bean id="checkStoreIdAction" class="cn.dy.action.CheckStoreIdAction" scope="prototype">
    <property name="storeGdsFormService" ref="storeGdsFormService"></property>
    </bean>

    <!-- 出库订单的管理模块 -->
    <bean id="exportGdsFormAction" class="cn.dy.action.ExportGdsFormAction" scope="prototype">
    <property name="exportGdsFormServers" ref="exportGdsFormServers"></property>
    <property name="goodsServer" ref="goodsService"></property>
    <property name="recordExportGdsServers" ref="recordExportGdsServers"></property>
    </bean>

    <!-- 报表展现的管理模块 -->
    <bean id="reportAction" class="cn.dy.action.ReportAction" scope="prototype">
    <property name="reportShowExportDao" ref="reportShowExportDao"></property>
    <property name="reportShowStoreDao" ref="reportShowStoreDao"></property>
    </bean>
    </beans>

    3.bean-base.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: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">
    <context:annotation-config />

    <!-- 1. 连接池实例 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="jdbcUrl" value="jdbc:mysql://192.168.8.134:3306/hib_demo"></property>
    <property name="driverClass" value="com.mysql.jdbc.Driver"></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. Spring管理SessionFactory 【全部配置都写到spring中】 -->
    <!-- # 注入DataSource、 注入常用配置属性、映射配置属性 -->
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <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>
    <!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
    </props>
    </property>
    <property name="annotatedClasses">
    <list>
    <value>cn.dy.entity.UserInfo</value>
    <value>cn.dy.entity.Permission</value>
    <value>cn.dy.entity.FunctionComponents</value>
    <value>cn.dy.entity.Goods</value>
    <value>cn.dy.entity.Category</value>
    <value>cn.dy.entity.Repertory</value>
    <value>cn.dy.entity.StoreGdsForm</value>
    <value>cn.dy.entity.ExportGdsForm</value>
    <value>cn.dy.entity.RecordExportGds</value>
    <value>cn.dy.entity.RecordStoreGds</value>
    <value>cn.dy.entity.ShowRecordExportgds</value>
    <value>cn.dy.entity.ShowRecordStoreGds</value>
    </list>
    </property>
    </bean>


    <!-- 3. 事务相关配置 -->
    <!-- 3.1 事务管理器类 -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 3.2 事务增强(如何管理事务)-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="get*" read-only="true"/>
    <tx:method name="find*" read-only="true"/>
    <tx:method name="*" read-only="false"/>
    </tx:attributes>
    </tx:advice>

    <!-- 3.3 Aop配置 = 切入点表达式(拦截目标对象,生成代理) + 事务增强应用-->
    <aop:config>
    <aop:pointcut expression="execution(* cn.dy.service.impl.*.*(..))" id="pt"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
    </beans>

    4.bean-dao.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: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">

    <!-- Dao 注入 SessionFactory -->

    <bean id="adminDao" class="cn.dy.dao.impl.AdminDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="deptDao" class="cn.dy.dao.impl.DeptDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="employeeDao" class="cn.dy.dao.impl.EmployeeDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="goodsDao" class="cn.dy.dao.impl.GoodsDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="repertoryDao" class="cn.dy.dao.impl.RepertoryDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="categoryDao" class="cn.dy.dao.impl.CategoryDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="storeGdsFormDao" class="cn.dy.dao.impl.StoreGdsFormDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="recordStoreGdsDao" class="cn.dy.dao.impl.RecordStoreGdsDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="exportGdsFormDao" class="cn.dy.dao.impl.ExportGdsFormDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="recordExportGdsDao" class="cn.dy.dao.impl.RecordExportGdsDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 报表出库
    <bean id="reportShowExportDao" class="cn.dy.dao.impl.ReportShowExportDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    -->

    <!-- 报表入库
    <bean id="reportShowStoreDao" class="cn.dy.dao.impl.ReportShowStoreDao">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    -->
    </beans>

    5.bean-service.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: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">

    <!-- Service 需要注入 Dao -->

    <bean id="adminService" class="cn.dy.service.impl.AdminService">
    <property name="adminDao" ref="adminDao"></property>
    </bean>

    <bean id="deptService" class="cn.dy.service.impl.DeptService">
    <property name="deptDao" ref="deptDao"></property>
    </bean>

    <bean id="employeeService" class="cn.dy.service.impl.EmployeeService">
    <property name="employeeDao" ref="employeeDao"></property>
    </bean>

    <bean id="goodsService" class="cn.dy.service.impl.GoodsService">
    <property name="goodsDao" ref="goodsDao"></property>
    </bean>

    <bean id="repertoryService" class="cn.dy.service.impl.RepertoryService">
    <property name="repertoryDao" ref="repertoryDao"></property>
    </bean>

    <bean id="categoryService" class="cn.dy.service.impl.CategoryService">
    <property name="categoryDao" ref="categoryDao"></property>
    </bean>

    <!-- 入库单 -->
    <bean id="storeGdsFormService" class="cn.dy.service.impl.StoreGdsFormService">
    <property name="storeGdsFormDao" ref="storeGdsFormDao"></property>
    </bean>
    <!-- 入库单记录表 -->
    <bean id="recordStoreGdsServers" class="cn.dy.service.impl.RecordStoreGdsServers">
    <property name="recordStoreGdsDao" ref="recordStoreGdsDao"></property>
    </bean>

    <!-- 出库 单 -->
    <bean id="exportGdsFormServers" class="cn.dy.service.impl.ExportGdsFormServers">
    <property name="exportGdsFormDao" ref="exportGdsFormDao"></property>
    </bean>

    <!-- 出库单记录表 -->
    <bean id="recordExportGdsServers" class="cn.dy.service.impl.RecordExportGdsServers">
    <property name="recordExportGdsDao" ref="recordExportGdsDao"></property>
    </bean>


    </beans>

  • 相关阅读:
    js 数据结构-栈与队列
    mysql (已解决)Access denied for user 'root'@'localhost' (using password: NO)
    mysql (已解决p)MYSQL5.7启动不了,本地计算机上的 MySQL57 服务启动后停止。
    mysql 慢查询日志,灾难日志恢复,错误日志
    php json的相关操作
    (转)LitJson 遍历key
    (转)用Eclipse进行C++开发时Bianry not found的问题解决
    (转) 在Eclipse中进行C/C++开发的配置方法(20140721最新版)
    (转)如何在eclipse的配置文件里指定jdk路径
    (转)mongodb分片
  • 原文地址:https://www.cnblogs.com/nucdy/p/5901430.html
Copyright © 2011-2022 走看看