1.配置文件
pom.xml
指定一个spring.version变量
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.0.0.RELEASE</spring.version> </properties>
把spring+hibernate核心包引进
1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-web</artifactId> 4 <version>${spring.version}</version> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework</groupId> 8 <artifactId>spring-orm</artifactId> 9 <version>${spring.version}</version> 10 </dependency> 11 <dependency> 12 <groupId>org.hibernate</groupId> 13 <artifactId>hibernate-core</artifactId> 14 <version>3.6.10.Final</version> 15 </dependency>
还需要 web和数据库交互的需要
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.11.0.GA</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> <classifier>bin</classifier> </dependency>
jdbc.properties 数据库账号信息
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.1.2:3306/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF8 jdbc.user=root jdbc.password=root
application-hiberntate.xml
加载资源文件
<!-- 加载资源文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> <property name="fileEncoding"> <value>UTF-8</value> </property> <property name="ignoreUnresolvablePlaceholders" value="true" /> </bean>
数据源
<!-- c3po 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"><value>${jdbc.driverClassName}</value></property> <property name="jdbcUrl"><value>${jdbc.url}</value></property> <property name="user"><value>${jdbc.user}</value></property> <property name="password"><value>${jdbc.password}</value></property> <property name="minPoolSize"><value>10</value></property> <property name="maxPoolSize"><value>300</value></property> <property name="maxIdleTime"><value>120</value></property> <property name="idleConnectionTestPeriod"><value>3</value></property> <property name="initialPoolSize"><value>10</value></property> <property name="numHelperThreads"><value>3</value></property> <property name="acquireRetryAttempts"><value>8</value></property> <property name="testConnectionOnCheckin" value="false"/> </bean>