SpringMVC4+Spring4+Hibernate4框架整合

  SpringMVC4 + Spring4 + Hibernate4 框架搭建中。。。



前言


现在很多企业流行用mybatis + spring + springmvc框架,但是mybatis或ibatis编写SQL较麻烦且对于数据库的移植性不好,你觉得呢?如果你也感觉mybatis或ibatis编写SQL较麻烦,那SpringMVC+Spring+Hibernate的整合适合你,如果你觉得Strut2没有SpringMVC好用,存在漏洞,执行效率不高容易爆内存,那SpringMVC+Spring+Hibernate整合适合你,看好SpringMVC+Spring+Hibernate的搭配,此SSH非彼SSH。

总体来说:Hibernate入门门槛比Mybatis高,但个人还是偏向于Hibernate,用惯了Hibernate的都感觉Hibernate才是标准的ORM框架,而且数据库可移植性好(换数据库只需修改方言和数据库配置即可);Mybatis这种半自动化的虽然灵活,可优化SQL,但是对于数据库的移植性来说就差了,而对于Hiberante只要控制的好,功能更强大。


选择SpringMVC4+Spring4+Hibernate4的原因


仔细分析过Struts2与SpringMVCMybatis与Hibernate的优势与劣势之后,总想搭建出一套在表现出、持久层中整体占优势的框架。于是就决定了SpringMVC+Spring+Hibernate框架的整合搭建,为了考虑到系统以后会用到新框架的一些特性,所有就决定搭建SpringMVC4+Spring4+Hibernate4的框架。


目标:有点追求,搭建出一套属于自己的框架,慢慢维护这套框架,深入研究这套框架的新特性,封装这套框架,让它变得越来越好。。。也让自己拥有“走在架构师路上”的感觉。



准备工作


hibernate4下载地址  http://hibernate.org/orm/

SpringMVC  官网:http://projects.spring.io/spring-framework
下载地址:http://repo.spring.io/release/org/springframework/spring/4.2.2.RELEASE/


仅下载:

spring-framework-4.2.2.RELEASE-dist.zip   
spring-framework-4.2.2.RELEASE-docs.zip 
spring-framework-4.2.2.RELEASE-schema.zip 


后续将加入Spring-Security的安全访问控制解决方案的安全框架

下载地址:http://repo.spring.io/libs-release-local/org/springframework/security/spring-security/

Spring Security开发手册:http://docs.spring.io/spring-security/site/docs/4.1.0.RELEASE/reference/htmlsingle/#ns-minimal


搭建需要准备jar(目前基础版本,后面会随着spring security应用以及基类封装jar会增多):


                                                   



搭建环境的配置


一、web.xml中的配置文件:


[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">  
  6.     <display-name>nssh</display-name>  
  7.     <welcome-file-list>  
  8.         <welcome-file>index.html</welcome-file>  
  9.         <welcome-file>index.htm</welcome-file>  
  10.         <welcome-file>index.jsp</welcome-file>  
  11.         <welcome-file>default.html</welcome-file>  
  12.         <welcome-file>default.htm</welcome-file>  
  13.         <welcome-file>default.jsp</welcome-file>  
  14.     </welcome-file-list>  
  15.   
  16.     <!-- 配置Spring IOC 容器 -->  
  17.     <context-param>  
  18.         <param-name>contextConfigLocation</param-name>  
  19.         <param-value>classpath:applicationContext.xml</param-value>  
  20.     </context-param>  
  21.     <listener>  
  22.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  23.     </listener>  
  24.   
  25.     <!-- 配置SpringMVC 的 DispatcherServlet 控制器 -->  
  26.     <servlet>  
  27.         <servlet-name>dispatcherServlet</servlet-name>  
  28.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  29.         <!-- 配置DispatcherServlet的一个初始化参数:配置SpringMVC配置文件的位置名称 -->  
  30.         <init-param>  
  31.             <param-name>contextConfigLocation</param-name>  
  32.             <param-value>classpath:springmvc.xml</param-value>  
  33.         </init-param>  
  34.         <load-on-startup>1</load-on-startup>  
  35.     </servlet>  
  36.     <servlet-mapping>  
  37.         <servlet-name>dispatcherServlet</servlet-name>  
  38.         <url-pattern>/*</url-pattern>  
  39.     </servlet-mapping>  
  40.   
  41.     <!-- 配置编码方式过滤器,注意一点:要配置在所有过滤器的前面 -->  
  42.     <filter>  
  43.         <filter-name>characterEncodingFilter</filter-name>  
  44.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  45.         <init-param>  
  46.             <param-name>encoding</param-name>  
  47.             <param-value>UTF-8</param-value>  
  48.         </init-param>  
  49.     </filter>  
  50.     <filter-mapping>  
  51.         <filter-name>characterEncodingFilter</filter-name>  
  52.         <url-pattern>/*</url-pattern>  
  53.     </filter-mapping>  
  54.       
  55.     <!-- 为了使用SpringMVC框架实现REST风格,需要配置  HiddenHttpMethodFilter-->  
  56.     <filter>  
  57.         <filter-name>hiddenHttpMethodFilter</filter-name>  
  58.         <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
  59.     </filter>  
  60.     <filter-mapping>  
  61.         <filter-name>hiddenHttpMethodFilter</filter-name>  
  62.         <url-pattern>/*</url-pattern>  
  63.     </filter-mapping>  
  64.       
  65. </web-app></span><span style="font-size: 18px;">  
  66. </span>  



二、Spring的applicationContext.xml配置文件:


[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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" xmlns:jee="http://www.springframework.org/schema/jee"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"  
  6.     default-lazy-init="true">  
  7.   
  8.     <!-- 配置自动扫描的包 -->   
  9.     <context:component-scan base-package="com.ywx" use-default-filters="false">  
  10.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  11.         <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  12.     </context:component-scan>  
  13.       
  14.     <!-- 配置数据源 -->  
  15.     <context:property-placeholder location="classpath:db.properties"/>  
  16.       
  17.     <!-- 配置DataSource -->  
  18.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  19.         <property name="user" value="${jdbc.user}"></property>  
  20.         <property name="password" value="${jdbc.password}"></property>  
  21.         <property name="driverClass" value="${jdbc.driverClass}"></property>  
  22.         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>  
  23.     </bean>  
  24.       
  25.     <!-- 配置SessionFactory -->  
  26.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  27.         <!-- 配置数据源属性 -->  
  28.         <property name="dataSource" ref="dataSource"></property>  
  29.         <!-- 配置扫描的实体包(pojo) -->  
  30.         <property name="namingStrategy">  
  31.             <bean class="org.hibernate.cfg.ImprovedNamingStrategy"></bean>  
  32.         </property>  
  33.         <property name="packagesToScan" value="com.ywx.entity"></property>  
  34.           
  35.         <!-- 配置Hibernate 的常用属性 -->  
  36.         <property name="hibernateProperties">  
  37.             <props>  
  38.             <!-- 数据库的方言 -->  
  39.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>  
  40.                 <prop key="hibernate.show_sql">true</prop>  
  41.                 <prop key="hibernate.format_sql">true</prop>  
  42.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  43.             </props>  
  44.         </property>  
  45.     </bean>  
  46.       
  47.     <!-- 配置Hibernate 的事物管理器 -->  
  48.     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  49.         <property name="sessionFactory" ref="sessionFactory"></property>  
  50.     </bean>  
  51.       
  52. </beans>  


三、SpringMVC中springmvc.xml文件:


[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;"><?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" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  7.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
  8.   
  9.     <!-- 配置自动扫描的包 -->  
  10.     <context:component-scan base-package="com.ywx" use-default-filters="false">  
  11.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  12.         <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  13.     </context:component-scan>  
  14.   
  15.     <!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图 -->  
  16.     <bean  
  17.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  18.         <property name="prefix" value="/views/"></property>  
  19.         <property name="suffix" value=".jsp"></property>   
  20.     </bean>  
  21.   
  22.     <!-- 配置静态资源:default-servlet-handler将在SpringMVC上下文中定义DefaultServletHttpRequestHandler,   
  23.         它会对进入DispatcherServlet的请求进行帅选,如果发现是没有经过映射的请求,就将该请求交由WEB应用服务器默认的 Servlet处理。如果不是静态资源的请求,才由DispatcherServlet继续处理。 -->  
  24.     <mvc:default-servlet-handler />  
  25.     <!-- 配置开启注解 -->  
  26.     <mvc:annotation-driven/>  
  27.   
  28. </beans>  
  29. </span>  



四、数据库db.properties文件:


[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. jdbc.user=root  
  2. jdbc.password=root  
  3. jdbc.driverClass=com.mysql.jdbc.Driver  
  4. jdbc.jdbcUrl=jdbc:mysql://localhost:3306/mysql  
  5.   
  6. #jdbc.user=ssh  
  7. #jdbc.password=ssh  
  8. #jdbc.driverClass=oracle.jdbc.driver.OracleDriver  
  9. #jdbc.jdbcUrl=jdbc:oracle:thin:@localhost:1521:ORCL  



测试环节


一、编写测试类:



二、测试结果:






控制台出现的红色部分是因为log日子没有处理


log日子处理:


导入log4j.jar

然后修改log4j.properties文件如下(日子输出等级根据需求而设置):

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ### direct log messages to stdout ###  
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
  3. log4j.appender.stdout.Target=System.out  
  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
  5. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n  
  6.   
  7. ### direct messages to file hibernate.log ###  
  8. #log4j.appender.file=org.apache.log4j.FileAppender  
  9. #log4j.appender.file.File=hibernate.log  
  10. #log4j.appender.file.layout=org.apache.log4j.PatternLayout  
  11. #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n  
  12.   
  13. ### set log levels - for more verbose logging change 'info' to 'debug' ###  
  14.   
  15. log4j.rootLogger=warn, stdout  
  16.   
  17. #log4j.logger.org.hibernate=info  
  18. #log4j.logger.org.hibernate=debug  
  19. log4j.logger.com.ywx=debug  



日志处理为debug后的输出为:





现在SpirngMVC+Spring+Hibernate的框架整合完成,目前还是一个空壳,其底层的DAO基类还在封装中。。。



基础代码封装遇到的问题

1、Spring4+Hibernate4整合后SessionFactory、Session及log的问题

2、mappedBy与@JoinColumn共存问题

3、Hibernate4主键生成策略问题



框架整合搭建完成,底层对Hibernate Dao的原生API封装完成,还加了一套字典、用户权限后的jar:

                          


本套架构借鉴了公司一个大型项目底层架构的同时,在其基础上扩展了dao和service层的多态,解决了部分单继承的局限。整体来说架构的健壮性增强了,也提供了对事务一致性的控制,但其复杂度也增加了。总的来说,这套后台的架构我甚是满意 ^ _ ^,后续会提供源码。



集成Spring-Security安全访问控制框架



1、下载Spring-Security:http://repo.spring.io/libs-release-local/org/springframework/security/spring-security/



下载第一个,里面包含了所有的文件。




源码请点击:源码下载


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值