• Java Web开发之Spring | SpringMvc | Mybatis | Hibernate整合、配置、使用


    1.Spring与Mybatis整合

     web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
                xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>SpringMvcTest</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
      <!-- 配置上下文加载监听器,根容器由此监听器创建 -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <!-- 配置前端控制器 -->
      <servlet>
        <servlet-name>ptmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>    <!-- 设置该控制器加载时机(应用启动时即加载)以及加载优先级(1) -->
      </servlet>
      <servlet-mapping>
        <servlet-name>ptmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      
    </web-app>

       applicationContext.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:context="http://www.springframework.org/schema/context"  //添加注解
        xmlns:tx="http://www.springframework.org/schema/tx"   //添加事物
        xmlns:aop="http://www.springframework.org/schema/aop"  //添加事物切面(利用面向切面的技术,即代理模式)
        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/tx 
                        http://www.springframework.org/schema/tx/spring-tx.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <context:component-scan base-package="com.frank.services"></context:component-scan>
    
    <!-- Mybatis与Spring集成配置: -->
        <!-- 1.配置数据源(连接池) -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">  <!-- 关闭连接对象销毁方法(数据库访问完毕后再将连接放回连接池,而不是关闭连接) -->
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
            <property name="username" value="x1" />
            <property name="password" value="x1" />
        </bean>
    
        <!-- 2.配置sqlSession工厂(MyBatils文档) -->
        <!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
            <property name="dataSource" ref="dataSource" /> </bean> -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
            autowire="byName">
        </bean>
    
        <!-- 3.配置映射器扫描配置器MapperScannerConfigurer(MyBatils文档) -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.frank.daomapper" />
        </bean>
    
    <!-- Spring中事务的配置: -->
        <!-- 1.配置事务管理器 -->
        <bean id="txManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        <!-- 2.配置事务通知(即事务处理规则) -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="get*" read-only="true"/>    <!-- 将查询事物设置为只读 -->
                <tx:method name="*" propagation="REQUIRED"/>    <!-- propagation="REQUIRED"为默认,可不写 -->
            </tx:attributes>
        </tx:advice>
    
        <!-- 3.配置事物切面 (即事务规则应用的“地方”)-->
        <aop:config>
            <aop:pointcut id="servicePointCut"
                expression="execution(* com.frank.services.*Service.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointCut" />
        </aop:config>
    
    </beans>

      ptmvc-servlet.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:context="http://www.springframework.org/schema/context"
            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">
    
            <context:component-scan base-package="com.frank.controller"></context:component-scan>
    </beans>

    2.Spring与Hibernate整合:

      web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        id="WebApp_ID" version="3.1">
        <display-name>SpringTest</display-name>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>myservlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>myservlet</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
        
        <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.frank.filter.CharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>

      applicationContext.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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
        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/tx
                            http://www.springframework.org/schema/tx/spring-tx.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop.xsd">
                            
        <context:component-scan base-package="com.frank.services,com.frank.daomapper" />
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
            <property name="url"
                value="jdbc:sqlserver://localhost:1433;databaseName=mydb2" />
            <property name="username" value="sa" />
            <property name="password" value="root" />
        </bean>
    
        <!-- SessionFactory配置 -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
            <property name="mappingLocations" value="classpath:com/frank/model/*.hbm.xml"></property>
            <property name="hibernateProperties">
                <value>
                    hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect
                    hibernate.show_sql=true
                </value>
            </property>
        </bean>
    
        <bean id="txManager"
            class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="get*" read-only="true" />
                <tx:method name="*" />
            </tx:attributes>
        </tx:advice>
    
        <aop:config>
            <aop:pointcut id="servicePointCut"
                expression="execution(* com.frank.services.*Service.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointCut" />
        </aop:config>
    </beans>

      myservlet-servlet.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:context="http://www.springframework.org/schema/context"
        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">
        <context:component-scan base-package="com.frank.controller" />
    </beans>

  • 相关阅读:
    关于js中"window.location.href"、"location.href"、"parent.location.href"、"top.location.href"的用法
    对于json数据的应用01
    关于session应用(1)session过期时间设置
    关于session应用(2)JAVA中怎么使用session
    Jquery常用技巧(3)
    0101对称二叉树 Marathon
    0112路径之和 & 0113所有路径之和 Marathon
    0106105从中序与后序遍历序列中构造二叉树 Marathon
    0110平衡二叉树 Marathon
    0513找树左下角的值 Marathon
  • 原文地址:https://www.cnblogs.com/FrankLei/p/6443947.html
Copyright © 2020-2023  润新知