spring+spring mvc+hibernate+maven+oracle10g框架搭建及hibernate Tool使用汇总(附github源码)

来源:互联网 发布:网络语言王炸什么意思 编辑:程序博客网 时间:2024/05/19 03:46

今天搭建了一套ssh的maven管理的项目框架,大多是网上已经有的资源的整合,中间也遇到了一些小问题,现总结下,分享给有需要的朋友,大家多多进步;
首先贴上源码:https://github.com/ZhangXixiang/hibernate-springmvc-maven.git
源码分两块:springmvc_hibernate_demo(工程中含oracle10g的数据库脚本)和hibernateGenerator(用于根据数据库表生成bean)

本文中的demo是基于注解扫描包实现实体类和数据库映射的,并非基于xml配置的;
springmvc_hibernate_demo(工程中含oracle10g的数据库脚本)
引用自http://blog.csdn.net/fengshizty/article/details/43635305
仅仅是切换了数据库为oracle10g,所以不做过多赘述;
下面简单的分析下大神搭建的demo,对于web工程,我们首先应该从web.xml开始阅读:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    id="WebApp_ID" version="2.5">    <display-name>springmvc_demo</display-name>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value>    </context-param>    <filter>        <filter-name>encodingFilter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>utf-8</param-value>        </init-param>        <init-param>            <param-name>forceEncoding</param-name>            <param-value>true</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>encodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!-- openSessionInView配置 作用是延迟session关闭到view层 -->    <filter>        <filter-name>openSessionInViewFilter</filter-name>        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>        <init-param>            <param-name>singleSession</param-name>            <param-value>true</param-value>        </init-param>    </filter>    <!-- 监听servletContext,启动contextConfigLocation中的spring配置信息 -->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- 防止spring内存溢出监听器 -->    <listener>        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>    </listener>    <servlet>        <description>spring mvc servlet</description>        <servlet-name>rest</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <!-- 此处配置的是SpringMVC的配置文件 -->            <param-value>classpath:spring-mvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>rest</servlet-name>        <url-pattern>*.htmls</url-pattern>    </servlet-mapping>    <filter-mapping>        <filter-name>openSessionInViewFilter</filter-name>        <url-pattern>*.htmls</url-pattern>    </filter-mapping>    <!-- 配置session超时时间,单位分钟 -->    <session-config>        <session-timeout>30</session-timeout>    </session-config>    <welcome-file-list>        <welcome-file>/index.jsp</welcome-file>    </welcome-file-list></web-app>

可以看到原作者还是配置的比较多的东西,我们主要看到:
web.xml,看到其中有加载spring.xml的配置;所以我们看下spring的配置文件;

<?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-4.1.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-4.1.xsd">    <!-- 加载配置文件 -->    <context:property-placeholder location="classpath:config.properties"/>    <!-- 扫描service自动注入为bean -->    <context:component-scan base-package="org.andy.work.service.impl,org.andy.work.dao.impl" /></beans>

web.xml中有加载spring-hibernate.xml的配置;所以我们看下spring的配置文件,其中配置了数据源,sessionFactory,二级缓存,事务设置aop也有配置;

<?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: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-4.1.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-4.1.xsd        http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">    <!-- 配置数据源 c3p0 -->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"        destroy-method="close">        <property name="driverClass" value="${jdbc.driver}" />        <property name="jdbcUrl" value="${jdbc.url}" />        <property name="user" value="${jdbc.username}" />        <property name="password" value="${jdbc.password}" />        <!-- 请求超时时间 -->        <property name="checkoutTimeout" value="30000" />        <!-- 每60秒检查所有连接池中的空闲连接。默认值: 0,不检查 -->        <property name="idleConnectionTestPeriod" value="30" />        <!-- 连接数据库连接池最大空闲时间 -->        <property name="maxIdleTime" value="30" />        <!-- 连接池初始化连接数 -->        <property name="initialPoolSize" value="5" />        <property name="minPoolSize" value="5" />        <property name="maxPoolSize" value="20" />        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 -->        <property name="acquireIncrement" value="5" />    </bean>    <!-- 配置hibernate的SessionFactory -->    <bean id="sessionFactory"        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">        <!-- 注入数据源 相关信息看源码 -->        <property name="dataSource" ref="dataSource" />        <!-- hibernate配置信息 -->        <property name="hibernateProperties">            <props>                <prop key="hibernate.dialect">${hibernate.dialect}</prop>                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>                <!-- 开启二级缓存 ehcache -->                <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>                <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>                <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.cache.provider_configuration_file_resource_path}                </prop>            </props>        </property>        <!-- 扫描hibernate注解配置的entity -->        <property name="packagesToScan" value="org.andy.work.entity" />    </bean>    <!-- 配置事务管理器 -->    <bean id="transactionManager"        class="org.springframework.orm.hibernate4.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>    <!-- 配置事务增强处理Bean,指定事务管理器 -->    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">        <!-- 配置详细事务处理语义 -->        <tx:attributes>            <tx:method name="insert*" propagation="REQUIRED" />            <tx:method name="update*" propagation="REQUIRED" />            <tx:method name="delete*" propagation="REQUIRED" />            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />            <tx:method name="load*" propagation="SUPPORTS" read-only="true" />            <!-- 其他采用默认事务方式 -->            <tx:method name="*" />        </tx:attributes>    </tx:advice>    <!-- Spring aop事务管理 -->    <aop:config>        <!-- 配置切入点 -->        <aop:pointcut id="transactionPointcut"            expression="execution(* org.andy.work.service..*Impl.*(..))" />        <!-- 指定在txAdvice切入点应用txAdvice事务增强处理 -->        <aop:advisor pointcut-ref="transactionPointcut"            advice-ref="transactionAdvice" />    </aop:config></beans>

web.xml中配置了spring-mvc.xml这个文件;下面我们看下spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:mvc="http://www.springframework.org/schema/mvc" 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"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-4.1.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-4.1.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">    <!-- 自动扫描@Controller注入为bean -->    <context:component-scan base-package="org.andy.work.controller" />    <!-- 以下为SpringMVC配置 -->    <mvc:annotation-driven>        <!-- 返回json数据,@response使用 -->        <mvc:message-converters register-defaults="true">            <bean                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">                <property name="supportedMediaTypes">                    <list>                        <value>text/html;charset=UTF-8</value>                        <value>application/json;charset=UTF-8</value>                    </list>                </property>            </bean>        </mvc:message-converters>    </mvc:annotation-driven>    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass"            value="org.springframework.web.servlet.view.JstlView" />        <property name="prefix" value="/WEB-INF/views" />        <property name="suffix" value=".jsp" />    </bean></beans>

spring-mvc.xml中主要配置了mvc的驱动及controller的自动扫描;

这样看下来基本上项目的大体架构已经明确了;按照原文的操作,能够应用这套代码咯;

==================================================
下面说说hibernate Tool 使用中的一个问题:
先在eclipse中安装工具http://www.cnblogs.com/AlanLee/p/5836823.html
使用工具建立反向工程参考原文:http://blog.csdn.net/u011202334/article/details/52459454
感谢原文作者,写的很细,但是在这一步没有说明清楚:
这里写图片描述

其中Default Schema如果填写的是小写的话,那么后面在读取数据库中的表的时候会读不出来,也不会报错,所以我们要注意下,这一项可以不填写,但是如果用户很多的话,eclipse需要加载很长时间哦;完了,可以按照原文的操作,生成我们的Bean咯;全文终。

阅读全文
0 0
原创粉丝点击