最近,正在为公司搭建java部门的开发平台,记录下搭建的整个过程。
jdk1.6.0_04
Tomcat6.0.29-windows-x86
Eclipse3.7 (Version: Indigo Release Build id: 20110615-0604)
后端:Mybatis(3.0.5) + Spring(3.0.5)MVC
前端:jQuery1.5.1
MySQL(5.1)
整个工程的编码是“utf-8”(包括里面所有的文件)。首先看一下整个项目的结构(里面涉及到公司名称的部分进行了处理):
接着,看看需要哪些jar包:
<?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_2_5.xsd"> <!-- 载入spring上下文 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 载入springMVC配置 如果<servlet-name>的名称为testMVC,则对应的xml文件则是testMVC-servlet.xml --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Spring监听 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <!-- Spring 刷新Introspector防止内存泄露 --> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- Spring过滤中文字符集 --> <filter> <filter-name>SetCharacterEncoding</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> </filter> <!-- 要过滤的类型 --> <filter-mapping> <filter-name>SetCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
2.2.2 applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" > <!-- jdbc属性文件读入 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- 数据库连接池 --> <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="minPoolSize" value="5"/> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="30"/> <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="10"/> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="10"/> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="5"/> <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。 但由于预缓存的statements属于单个connection而不是整个连接池。所以设 置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection 均为0,则缓存被关闭。Default: 0--> <property name="maxStatements" value="0"/> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="60"/> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts" value="30"/> <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据 源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设 为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default: false--> <property name="breakAfterAcquireFailure" value="true"/> <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection 提交的时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 等方法来提升连接测试的性能。Default: false --> <property name="testConnectionOnCheckout" value="false"/> </bean> <!-- Mybatis's sqlSessionFactory config --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 启用事务 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" rollback-for="com.googosoft.framework.exception.CRUDException,Exception" /> <tx:method name="create*" rollback-for="com.googosoft.framework.exception.CRUDException,Exception" /> <tx:method name="copy*" rollback-for="com.googosoft.framework.exception.CRUDException,Exception" /> <tx:method name="update*" rollback-for="com.googosoft.framework.exception.CRUDException,Exception" /> <tx:method name="delete*" rollback-for="com.googosoft.framework.exception.CRUDException,Exception" /> <tx:method name="find*" read-only="true" rollback-for="com.googosoft.framework.exception.CRUDException,Exception" /> <tx:method name="list*" read-only="true" rollback-for="com.googosoft.framework.exception.CRUDException,Exception" /> <tx:method name="is*" read-only="true" rollback-for="com.googosoft.framework.exception.CRUDException,Exception" /> <tx:method name="*" read-only="true" rollback-for="com.googosoft.framework.exception.CRUDException,Exception" /> </tx:attributes> </tx:advice> <!-- AOP代理设置 --> <aop:config> <aop:advisor pointcut="execution(* com.googosoft.framework.service.*.*(..))" advice-ref="txAdvice" /> </aop:config> <!—- 自动扫描 XXXMapper.java --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.googosoft.framework.persistence" /> </bean> <!-- 激活annotation功能 --> <context:annotation-config /> <context:spring-configured/> </beans>
其中,需要注意的2个地方是:
1. jdbc.properties文件的位置:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean>
这里指的是classpath的位置,如果还有文件夹,可以写成“XXX / jdbc.properties”
2. mybatis-config.xml文件的位置:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean>
与jdbc需要注意的是一样的。
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" > <!-- 扫描指定package下所有带有如@controller,@services,@resource,并把所注释的注册为Spring Beans --> <context:component-scan base-package="com.googosoft"> <context:include-filter type="regex" expression="com.googosoft.framework.service.*"/> <context:include-filter type="regex" expression="com.googosoft.framework.web.*"/> </context:component-scan> <!-- 只能用于springMVC,用于配置springMVC的注解驱动 --> <mvc:annotation-driven /> <!-- Spring mvc视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/view/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
2.2.4 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 运行时,参数信息 --> <settings> <!-- 使全局的映射器启用或禁用缓存。 --> <setting name="cacheEnabled" value="true"/> <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 --> <setting name="aggressiveLazyLoading" value="true"/> <!-- 允许或不允许多种结果集从一个单独的语句中返回(需要适合的驱动)。 --> <setting name="multipleResultSetsEnabled" value="true"/> <!-- 使用列标签代替列名。 --> <setting name="useColumnLabel" value="true"/> <!-- 允许JDBC支持生成的键。 --> <setting name="useGeneratedKeys" value="false"/> <!-- 指定MyBatis如何自动映射列到字段/属性。 --> <setting name="autoMappingBehavior" value="PARTIAL"/> <!-- 配置默认的执行器。 --> <setting name="defaultExecutorType" value="SIMPLE"/> </settings> <!-- java类别名 --> <typeAliases> <typeAlias alias="User" type="com.googosoft.framework.domain.system.User" /> <typeAlias alias="Department" type="com.googosoft.framework.domain.system.Department" /> </typeAliases> <!-- SQL映射语句 --> <mappers> <mapper resource="mapper/system/UserMapper.xml"/> <mapper resource="mapper/system/DepartmentMapper.xml"/> </mappers> </configuration>
2.2.5 jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/gframe?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=root
2.2.6 log4j.properties
log4j.rootLogger=INFO,stdout,docout ### print Mybatis logging log4j.logger.org.apache.ibatis=DEBUG log4j.logger.java.sql=DEBUG log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=-----------------------------------------------------------------------------------%nLevel\:[%p]%nTime\:[%d]%nClass\:[%l]%nMessage\:[%m]%n log4j.appender.docout=org.apache.log4j.DailyRollingFileAppender log4j.appender.docout.File=log/log.html log4j.appender.docout.Append=true log4j.appender.docout.DatePattern=yyyy-MM-dd'.html' log4j.appender.docout.layout=org.apache.log4j.HTMLLayout