工作中需要在施耐德楼控系统上添加后台管理功能和手机控制功能,单位采购的施耐德的产品仅仅是用于控制现场设备的楼控模块及上位机编程与HMI,我们需要在此基础上,自主开发手机端控制功能,那么就需要通过建立后台工程用于往施耐德的硬件上发信号或者修改其数据库。
本文即是建立在此想法的基础上,记录一下如何采用Spring、Hibernate、Rest这个框架构建一个可以快速开发的后台管理框架。
1.使用eclipse新建一个普通JAVA工程
2.右键工程名,选择属性,点击"Project Facets"将“Dynamic Web Module勾选上,一个动态Web工程即建好
3.将Spring、Hibernate、Rest所需的jar文件全部复制到Web工程的WEB-INF/lib文件夹下(建议将jar文件复制到工程下面而不是引用,否则还需在tomcat下面放置一份jar包,否则会照成运行时报错)
4.新建Web.xml用于配置Spring信息
<?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/j2ee" xmlns:javaee="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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <display-name>TacControlServerWeb</display-name> <distributable /> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/spring/applicationContext*.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> <listener> <!-- 自动装配ApplicationContext的配置信息 --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <!-- org.springframework.web.util.IntrospectorCleanupListener监听器主要负责处理由JavaBean Introspector使用而引起的缓冲泄露 --> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <listener> <!-- ContextLoaderListener实现ServletContextListener监听器接口,而ServletContextListener只负责监听Web容器的启动和关闭的事件。RequestContextFilter实现ServletRequestListener监听器接口,该监听器监听HTTP请求事件,Web服务器接收的每次请求都会通知该监听器。通过配置RequestContextFilter,Spring容器与Web容器结合的更加密切。 --> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <!-- rest监听器配置 --> <servlet> <display-name>JAX-RS REST Servlet</display-name> <servlet-name>JAX-RS REST Servlet</servlet-name> <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>szx.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>JAX-RS REST Servlet</servlet-name> <url-pattern>/szxrest/*</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>FindInfo</display-name> <servlet-name>FindInfo</servlet-name> <servlet-class>szx.web.servlet.FindInfo</servlet-class> </servlet> <servlet-mapping> <servlet-name>FindInfo</servlet-name> <url-pattern>/FindInfo</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
5.配置了Spring后,在Spring的配置文件中配置Hibernate信息和自动注入的方式,如下applicationContext-core.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" 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/aop http://www.springframework.org/schema/aop/spring-aop-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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="2000" /> <property name="maxIdle" value="1000"></property> <property name="maxWait" value="25000" /> <property name="poolPreparedStatements" value="false" /> <property name="defaultAutoCommit" value="true" /> </bean> <!-- Hibernate SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>szx</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=${hibernate.dialect} hibernate.query.substitutions=true 'Y', false 'N' hibernate.cache.use_second_level_cache=true hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.jdbc.fetch_size=50 hibernate.jdbc.batch_size=25 hibernate.show_sql=true hibernate.format_sql=false hibernate.use_sql_comments=true </value> </property> <property name="lobHandler" ref="lobHandler" /> </bean> <bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler"> <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor" /> </bean> <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" /> <!-- Transaction manager for a single Hibernate SessionFactory --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- Enable annotation-based configuration --> <context:annotation-config /> <!-- Enable classpath scanning for managed components --> <context:component-scan base-package="szx" name-generator="szx.core.spring.CustomBeanNameGenerator"/> <!-- Enable @AspectJ support --> <aop:aspectj-autoproxy /> <!-- Enable @Transactional support --> <tx:annotation-driven /> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* *..service.*Manager.*(..))" order="100" /> </aop:config> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="*" rollback-for="Throwable" /> </tx:attributes> </tx:advice> </beans>
6.新建jdbc.properties文件,配置数据库连接(数据库可以适配MySQL、Oracle、SqlServer)
#hibernate.dialect=cn.walle.core.support.hibernate.Oracle10gDialect hibernate.dialect=org.hibernate.dialect.SQLServerDialect #jdbc.driverClassName=oracle.jdbc.driver.OracleDriver #jdbc.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver #jdbc.url=jdbc:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName=taclogdata jdbc.driverClassName=net.sourceforge.jtds.jdbc.Driver jdbc.url=jdbc:jtds:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName=taclogdata #jdbc.url=jdbc:oracle:thin:@192.168.103.9:1521:orcl #jdbc.url=jdbc:oracle:thin:@219.237.193.91:1521:orcl #jdbc.url=jdbc:oracle:thin:@iwmds.rdh.com:1521:orcl jdbc.username=tacvista jdbc.password=tacvista
7.工程结构如下图
时间: 2024-10-03 11:08:34