Build an example project of the Schneider Building Control System database backend server-(Project creation), Schneider Control System
During work, the background management and mobile phone control functions must be added to the Schneider building control system. The purchased Schneider products are only the building control module used to control on-site equipment, as well as the host computer programming and HMI, on this basis, we need to develop the mobile phone control function independently, so we need to build a background project to send signals to Schneider's hardware or modify its database.
Based on this idea, this article records how to use the Spring, Hibernate, and Rest frameworks to build a background management framework that can be developed quickly.
1. Use eclipse to create a common JAVA Project
2. Right-click the Project name, select properties, and click "Project Facets". Select "Dynamic Web Module". A Dynamic Web Project is created.
3. copy all jar files required by Spring, Hibernate, and Rest to the WEB-INF/lib folder of the Web Project (we recommend that you copy the jar file to the project rather than reference it, otherwise, you need to place a jar package under tomcat. Otherwise, an error will be reported)
4. Create Web. xml to configure Spring Information
<? 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-pa Ram> <param-name> contextConfigLocation </param-name> <param-value> classpath:/spring/applicationContext *. xml </param-value> </context-param> <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 </par Am-name> <param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name> encodingFilter </filter -name> <url-pattern>/* </url-pattern> </filter-mapping> <listener> <! -- Automatically assemble the configuration information of ApplicationContext --> <listener-class> org. springframework. web. context. ContextLoaderListener </listener-class> </listener> <! -- Org. springframework. web. util. the IntrospectorCleanupListener listener is mainly responsible for Handling Buffer leaks caused by the use of JavaBean Introspector --> <listener-class> org. springframework. web. util. introspectorCleanupListener </listener-class> </listener> <! -- ContextLoaderListener implements the ServletContextListener listener interface, while ServletContextListener only monitors the startup and shutdown events of Web containers. RequestContextFilter implements the ServletRequestListener listener interface, which listens to HTTP request events and notifies the listener of each request received by the Web server. By configuring RequestContextFilter, Spring containers are more closely integrated with Web containers. --> <Listener-class> org. springframework. web. context. request. RequestContextListener </listener-class> </listener> <! -- Rest Listener Configuration --> <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. After configuring Spring, configure Hibernate information and automatic injection in Spring configuration file, the following 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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.5.xsdhttp://www.springframework.org/schema/txhttp://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=truehibernate.cache.provider_class=org.hibernate.cache.EhCacheProviderhibernate.jdbc.fetch_size=50hibernate.jdbc.batch_size=25hibernate.show_sql=truehibernate.format_sql=falsehibernate.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. Create a jdbc. properties file and configure the database connection (the database can be adapted to MySQL, Oracle, and SqlServer)
#hibernate.dialect=cn.walle.core.support.hibernate.Oracle10gDialecthibernate.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=taclogdatajdbc.driverClassName=net.sourceforge.jtds.jdbc.Driverjdbc.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:orcljdbc.username=tacvistajdbc.password=tacvista
7. The project structure is as follows: