beans的配置

来源:互联网 发布:上海交大宿舍网络 编辑:程序博客网 时间:2024/06/03 23:56

连接mysql的配置:

<?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: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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
           
<context:annotation-config />
<context:component-scan base-package="com.register" />



<bean id="userService" class="com.bjsxt.service.UserService"/>

  <tx:advice id="txAdvice" transaction-manager="txManager">
  <tx:attributes>
    <tx:method name="get*" read-only="true"/>
    <tx:method name="add*"/>
  </tx:attributes>
  </tx:advice>
  
<aop:config>
    <aop:pointcut id="serviceOperation"
          expression="execution(public * com.bjsxt.service..*.*(..))"/>
    <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
 </aop:config>


<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.register.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>


<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean>


  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>


</beans>


jdbc.properties的配置:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/spring
jdbc.username=root
jdbc.password=123


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>


        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3307/spring</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123</property>


        <!-- JDBC connection pool (use the built-in) -->
        <!-- <property name="connection.pool_size">1</property> -->


        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>


        <!-- Enable Hibernate's automatic session context management -->
      <property name="current_session_context_class">thread</property> 


        <!--disable the second-level cache  -->
     <!-- <property name="cache.use_second_level_cache">true</property> --> 
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>


        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
         <property name="format_sql">true</property>


        <!-- Drop and re-create the database schema on startup -->
    <!--    <property name="hbm2ddl.auto">update</property>  -->   
       




    <mapping class="com.register.model.User"/> 
    

    </session-factory>


</hibernate-configuration>


这是按自己机子上配的。