鲜花网站项目(三)————spring3.1配置文件详解和DAO层核心解析

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

    <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">    
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />    
        <property name="url" value="jdbc:mysql://localhost:3306/flower?useUnicode=true&amp;characterEncoding=UTF-8"></property>    
        <property name="username" value="root" />  
         <property name="password" value="1213265442" />        
        <!-- 数据库连接池保持的最小连接数 -->    
        <property name="minIdle" value="5" /> <property name="maxIdle" value="30" /> 
         <property name="testOnBorrow" value="true" />    
        <!-- 返回连接时是否进行有效性验证 -->    
        <property name="testOnReturn" value="true" />    
        <!-- 连接空闲时是否进行有效性验证 -->    
        <property name="testWhileIdle" value="true" /> 
    </bean>    
    
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
           <property name="dataSource" ref="datasource"/>  
        <property name="hibernateProperties">
                <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                </props>
                </property>         
        <property name="mappingResources">  
            <list>  
                 <value>com/jikexueyuan/entity/Assess.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/Catalog.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/ConsumeDetaileInfo.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/ConsumeOrder.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/Flower.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/OrderForm.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/ShoppingCart.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/User.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/UserDetail.hbm.xml</value>  
            </list>  
           </property>
    </bean>
    
    <!-- user相关的bean -->
    <!-- ////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////// -->
    <bean id="UserDao" class="com.jikexueyuan.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="UserServices" class="com.jikexueyuan.services.UserServices">
        <property name="userDao" ref="UserDao"></property>
    </bean>
    <bean id="UserAction" class="com.jikexueyuan.action.UserAction">
        <property name="userSerivces" ref="UserServices"></property>
    </bean>
    
    <!-- catalog相关的bean -->
    <!-- ////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////// -->
    <bean id="catalogDao" class="com.jikexueyuan.dao.impl.CatalogDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="catalogServices" class="com.jikexueyuan.services.CatalogServices">
        <property name="catalogDao" ref="catalogDao"></property>
    </bean>
    <bean id="catalogAction" class="com.jikexueyuan.action.CatalogAction">
        <property name="catalogServices" ref="catalogServices"></property>
    </bean>


    <!-- flower相关的bean -->
    <!-- ////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////// -->
    <bean id="flowerDao" class="com.jikexueyuan.dao.impl.FlowerDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="flowerServices" class="com.jikexueyuan.services.FlowerServices">
        <property name="flowerDao" ref="flowerDao"></property>
    </bean>
    <bean id="flowerAction" class="com.jikexueyuan.action.FlowerAction">
        <property name="flowerServices" ref="flowerServices"></property>
    </bean>
    
    
    <!-- shopping相关的bean -->
    <!-- ////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////// -->
    <bean id="shoppingDao" class="com.jikexueyuan.dao.impl.ShoppingDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
        <property name="userDao" ref="UserDao"></property>
        <property name="flowerDao" ref="flowerDao"></property>
    </bean>
    <bean id="shoppingServices" class="com.jikexueyuan.services.ShoppingServices">
        <property name="shoppingDao" ref="shoppingDao"></property>
    </bean>
    <bean id="shoppingAction" class="com.jikexueyuan.action.ShoppingAction">
        <property name="shoppingServices" ref="shoppingServices"></property>
    </bean>
    
    
        <!-- orderForm相关的bean -->
    <!-- ////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////// -->
    <bean id="orderFormDao" class="com.jikexueyuan.dao.impl.OrderFormDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
        <property name="shoppingDao" ref="shoppingDao"></property>
        <property name="userDao" ref="UserDao"></property>
    </bean>
    <bean id="orderFormServices" class="com.jikexueyuan.services.OrderFormServices">
        <property name="orderFormDao" ref="orderFormDao"></property>
    </bean>
    <bean id="orderFormAction" class="com.jikexueyuan.action.OrderFormAction">
        <property name="orderFormServices" ref="orderFormServices"></property>
        <property name="userDao" ref="UserDao"></property>
    </bean>
    
            <!-- consumeRecord相关的bean -->
    <!-- ////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////// -->
    <!-- ////////////////////////////////////////////////////// -->
    <bean id="consumeRecordDao" class="com.jikexueyuan.dao.impl.ConsumeRecordDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
        <property name="orderFormDao" ref="orderFormDao"></property>
        <property name="userDao" ref="UserDao"></property>
    </bean>
    <bean id="consumeRecordServices" class="com.jikexueyuan.services.ConsumeRecordServices">
        <property name="consumeRecordDao" ref="consumeRecordDao"></property>
    </bean>
    <bean id="consumeRecorderAction" class="com.jikexueyuan.action.ConsumeOrderAction">
        <property name="consumeRecordServices" ref="consumeRecordServices"></property>
        <property name="userDao" ref="UserDao"></property>
    </bean>
    
    
    
    
    <aop:config proxy-target-class="true">
        <aop:pointcut expression="execution(* com.jikexueyuan.dao.impl.*.*(..))" id="cut"/>
        <aop:advisor advice-ref="defaultTransactionAdvice" pointcut-ref="cut"/>        
    </aop:config>
    
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="dataSource" ref="datasource"></property>  
    </bean>
    <tx:advice transaction-manager="transactionManager" id="defaultTransactionAdvice">
             <tx:attributes>  
                  <tx:method name="add*" read-only="false"/> 
               <tx:method name="*"/>  
          </tx:attributes> 
    </tx:advice>
    <tx:annotation-driven transaction-manager="transactionManager" /></beans>

这是配置数据库连接池


    <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">    
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />    
        <property name="url" value="jdbc:mysql://localhost:3306/flower?useUnicode=true&amp;characterEncoding=UTF-8"></property>    
        <property name="username" value="root" />  
         <property name="password" value="1213265442" />        
        <!-- 数据库连接池保持的最小连接数 -->    
        <property name="minIdle" value="5" /> <property name="maxIdle" value="30" /> 
         <property name="testOnBorrow" value="true" />    
        <!-- 返回连接时是否进行有效性验证 -->    
        <property name="testOnReturn" value="true" />    
        <!-- 连接空闲时是否进行有效性验证 -->    
        <property name="testWhileIdle" value="true" /> 
    </bean>    

 这是配置hibernate的sessionFactory

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
           <property name="dataSource" ref="datasource"/>  
        <property name="hibernateProperties">
                <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                </props>
                </property>         
        <property name="mappingResources">  
            <list>  
                 <value>com/jikexueyuan/entity/Assess.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/Catalog.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/ConsumeDetaileInfo.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/ConsumeOrder.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/Flower.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/OrderForm.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/ShoppingCart.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/User.hbm.xml</value>  
                 <value>com/jikexueyuan/entity/UserDetail.hbm.xml</value>  
            </list>  
           </property>
    </bean>

这是配置spring的aop

    <aop:config proxy-target-class="true">
        <aop:pointcut expression="execution(* com.jikexueyuan.dao.impl.*.*(..))" id="cut"/>
        <aop:advisor advice-ref="defaultTransactionAdvice" pointcut-ref="cut"/>        
    </aop:config>
    
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="dataSource" ref="datasource"></property>  
    </bean>
    <tx:advice transaction-manager="transactionManager" id="defaultTransactionAdvice">
             <tx:attributes>  
                  <tx:method name="add*" read-only="false"/> 
               <tx:method name="*"/>  
          </tx:attributes> 
    </tx:advice>

我采用的是面向接口编程的方法,所有的类先定义接口,在接口里面定义方法,在实现类里面实现方法

这是消费记录的DAO层接口

public interface ConsumeRecordDao {
    public void addConsumeRecord(String[] orderForm, Timestamp createdAt,int user_id);
    public List<ConsumeOrder> getAllConsumeOrder(User user);
    public Set<ConsumeDetaileInfo> getConsumeDetaileById(int consumeRecordId);
}

这是消费接口的实现类,采用了hibernate的标准化对象查询的方式

public class ConsumeRecordDaoImpl implements ConsumeRecordDao{
    private SessionFactory sessionFactory;
    private OrderFormDao orderFormDao;
    private UserDao userDao;
    public ConsumeRecordDaoImpl() {
        // TODO 自动生成的构造函数存根
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void setOrderFormDao(OrderFormDao orderFormDao) {
        this.orderFormDao = orderFormDao;
    }
    public OrderFormDao getOrderFormDao() {
        return orderFormDao;
    }
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public UserDao getUserDao() {
        return userDao;
    }
    @Override
    public void addConsumeRecord(String[] orderForm  ,Timestamp createdAt,int user_id) {
        // TODO 自动生成的方法存根
        User user = userDao.getUserById(user_id);
        ConsumeOrder consumeOrder = new ConsumeOrder(user, createdAt);
        sessionFactory.getCurrentSession().save(consumeOrder);
        for(int i=0;i<orderForm.length;i++){
            int orderFormId = Integer.parseInt(orderForm[i]);
            OrderForm form = orderFormDao.getOrderFormByOFId(orderFormId);
            ConsumeDetaileInfo info = new ConsumeDetaileInfo(form.getFlower(), user, form.getFlower().getFlowerName(), form.getFlower().getFlowerNumber(), form.getFlower().getFlowerPrice(), form.getSumPrice());
            info.setConsumeOrder(consumeOrder);
            sessionFactory.getCurrentSession().save(info);
            form.getFlower().getOrderForms().remove(form);
            form.getUser().getOrderForms().remove(form);
            form.setFlower(null);
            form.setUser(null);
            orderFormDao.deleteOrderForm(orderFormId);
        }
    }

    @Override
    public List<ConsumeOrder> getAllConsumeOrder(User user) {
        // TODO 自动生成的方法存根
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ConsumeOrder.class);
        Criterion criterion = Restrictions.eq("user", user);
        criteria.add(criterion);
        return criteria.list();
    }
    @Override
    public Set<ConsumeDetaileInfo> getConsumeDetaileById(int consumeRecordId) {
        // TODO 自动生成的方法存根
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ConsumeOrder.class);
        Criterion criterion = Restrictions.eq("orderId", consumeRecordId);
        criteria.add(criterion);
        List<ConsumeOrder> consumeOrders= criteria.list();
        ConsumeOrder consumeOrder = consumeOrders.get(0);
        Set<ConsumeDetaileInfo> sets = consumeOrder.getConsumeDetaileInfo();
        return sets;
    }

}

 

posted @ 2017-03-12 17:08  我们不该这样的  阅读(149)  评论(0编辑  收藏  举报