Tidhy

JavaBean.hbm.xml(hibernate配置方面的):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
        <!-- name:JavaBean类全路径
             table:表名字 -->
    <class name="com.itheima.elec.domain.ElecText" table="elec_text">
        <!-- name:主键
             type:主键类型,可忽略
             column:貌似也是主键 -->
        <id name="textID" type="String" column="textID">
                        <!-- 这个uuid,名词是什么忘记  -->
            <generator class="uuid"></generator>
        </id>
            <!-- 配置表其他的属性 -->
        <property name="textName" type="String" column="textName"></property>
        <property name="textDate" type="date" column="textDate"></property>
        <property name="textRemark" type="String" column="textRemark"></property>
    </class>
</hibernate-mapping>

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>
    <!-- 必须也要配置的参数有5个,4大参数,数据库的方言 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/struts_day01</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123</property>
        <!-- 数据库的方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
        <!-- 其他配置 -->
        
        <!-- 添加JavaBean映射的文件 -->
        <mapping resource="/itcast1128elec/src/com/itheima/elec/domain/ElecText.hbm.xml"/>
</session-factory>
</hibernate-configuration>   

 web.xml:

    <!-- 配置Spring框架整合WEB的监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>


    <!-- 解决延迟加载的问题 -->
    <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- 配置Struts2框架的核心的过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

 bean.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 8     http://www.springframework.org/schema/beans/spring-beans.xsd
 9     http://www.springframework.org/schema/context
10     http://www.springframework.org/schema/context/spring-context.xsd
11     http://www.springframework.org/schema/aop
12     http://www.springframework.org/schema/aop/spring-aop.xsd
13     http://www.springframework.org/schema/tx
14     http://www.springframework.org/schema/tx/spring-tx.xsd">
15     
16         <!-- 配置c3p0连接池 -->
17     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
18         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
19         <property name="jdbcUrl" value="jdbc:mysql:///ssh_crm"></property>
20         <property name="user" value="root"></property>
21         <property name="password" value="123"></property>
22     </bean>
23     
24     
25     
26         <!-- sessionfactory创建 -->
27         <!-- 配置Spring提供的支持注解ORM映射的Hibernate会话工厂 -->
28     <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
29         <property name="dataSource" ref="dataSource"></property>
30         <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
31     </bean>
32     <!-- 事务管理器 -->
33     <!-- 配置spring基于注解的声明式事务管理 -->
34     <bean id="transactionManager" class="org.springframework.org.hibernate5.HibernateTransactionManager">
35         <property name="sessionFactory" ref="sessionFactory"></property>
36     </bean>    
37     <!-- 通过setter注入Hibernate会话工厂 -->
38     <tx:annotation-driven transaction-manager="transactionManager" />
39     
40     
41     
42     <!-- 三大框架注入的关系
43          其中name属性是类里面生成的静态方法
44          ref是权限类名,也可以是id
45          这里有些乱的
46      -->
47     <bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype">
48         <property name="userService" ref="userService"></property>
49     </bean>
50     <bean id="userService" class="cn.itcast.service.UserService">
51         <property name="userDao" ref="userDaoImpl"></property>
52     </bean>
53     <bean id="userDaoImpl" class="cn.itcast.dao.UserDaoImpl">
54         <property name="sessionFactory" ref="sessionFactory"></property>
55     </bean>
56     
57     <!-- 这个是dao类里面注入模板对象的配置,class是模板hibernate5的接口地址
58          和上面的基本上都是一样的
59          不过因为dao层继承了     HibernateDaoSupport  ,所以这里可以省略了
60     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
61         <property name="sessionFactory" ref="sessionFactory"></property>
62     </bean>
63     -->
64 </beans>

 

posted @ 2017-04-25 22:18  tidhy  阅读(196)  评论(0编辑  收藏  举报