一步一步实现Spring4 + Hibernate4 + Freemarker2 的注解驱动开发(二.应用配置)
2014-03-11 12:57
549 查看
应用配置
编辑应用中需要的配置文件
web.xml 如下:
在src/main/resources 目录下创建database.properties文件,并创建目录spring,并在/src/main/resources/spring 下创建文件hibernateContext.xml和applicationContext.xml
database.properties文件内容如下:
hibernateContext.xml 内容如下:
applicationContext.xml 内容如下:
在MySQL中创建数据库toncen_cms(本文示例),并创建表site
为防止乱码,创建数据库时,应选择编码为utf-8
应用配置完成
编辑应用中需要的配置文件
web.xml 如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/applicationContext.xml, classpath:/spring/hibernateContext.xml</param-value> </context-param> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
在src/main/resources 目录下创建database.properties文件,并创建目录spring,并在/src/main/resources/spring 下创建文件hibernateContext.xml和applicationContext.xml
database.properties文件内容如下:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost/toncen_cms jdbc.user=toncen_dba jdbc.password=123456
hibernateContext.xml 内容如下:
<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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:database.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="cn.toncen.cms.entity" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
applicationContext.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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="cn.toncen.cms"/> 4000 <!-- 以下为SpringMVC内容 --> <!-- 支持spring4.0新的mvc注解 --> <mvc:annotation-driven /> <!-- 启动Spring MVC的注解功能 测试是否需要下面一行--> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 地址映射 --> <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> </bean> <!-- FreeMarker --> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="contentType" value="text/html;charset=UTF-8" /> </bean> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <!-- 模板根目录 --> <property name="templateLoaderPath" value="/" /> <!-- 编码格式 --> <property name="defaultEncoding" value="utf-8" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">0</prop> <prop key="default_encoding">UTF-8</prop> <prop key="number_format">0.##########</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="classic_compatible">true</prop> <prop key="template_exception_handler">ignore</prop> </props> </property> </bean>
<!-- 静态资源缓存,避免被filter过滤,找不到资源 --><mvc:resources mapping="/images/**" location="/WEB-INF/views/images/" cache-period="31556926" /> <mvc:resources mapping="/js/**" location="/WEB-INF/views/js/" cache-period="31556926" /> <mvc:resources mapping="/css/**" location="/WEB-INF/views/css/" cache-period="31556926" /></beans>
在MySQL中创建数据库toncen_cms(本文示例),并创建表site
create table site ( id int not null auto_increment, name varchar(128) not null, url varchar(256), recommend varchar(512), access_statistics int default 1, primary key (id) );
为防止乱码,创建数据库时,应选择编码为utf-8
应用配置完成
相关文章推荐
- 一步一步实现Spring4 + Hibernate4 + Freemarker2 的注解驱动开发(三.应用代码)
- 一步一步实现Spring4 + Hibernate4 + Freemarker2 的注解驱动开发(四.页面及小结)
- 一步一步实现Spring4 + Hibernate4 + Freemarker2 的注解驱动开发(一. 环境搭建)
- Spring4.X + Spring MVC + Mybatis3 零配置应用开发框架搭建详解 (3) - 实现最基本的登录处理
- Spring基于xml配置与注解驱动开发(一)
- Spring4.X + Spring MVC + Mybatis3 零配置应用开发框架搭建详解(4) - Scheduled注解实现任务调度
- Spring基于xml配置与注解驱动开发(二)
- Spring4.X + Spring MVC + Mybatis3 零配置应用开发框架搭建详解(4) - Scheduled注解实现任务调度
- SpringMVC学习笔记(三)使用IntelliJ IDEA开发Spring MVC HelloWorld(注解配置SpringMVC)
- 利用注解实现spring基本配置详解
- 演示在应用在使用hibernate实现模型层的开发(手动配置):
- 加强2注解。泛型。类加载器及其委托机制。代理的概念与作用原理,AOP概念。实现AOP功能的封装与配置。类似Spring。
- java开源框架应用技巧之spring配置文件中如果有多个.hbm.xml文件的话,无论是项目开发过程中还是维护过程中修改起来都会很麻烦切容易出错
- WEB开发框架配置文件和各层注解(SpringMVC + Spring + Hibernate)
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- 注解方式配置Spring实现Ioc
- Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)
- Spring4的知识应用总结(四)——Bean的生命周期和注解方式配置
- spring3零配置注解实现Bean定义(包括JSR-250、JSR-330)
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换