database.properties数据库配置参数
(1) druid连接池
datasource.class=com.alibaba.druid.pool.DruidDataSource
datasource.driver=com.mysql.cj.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=Asia/Shanghai
datasource.username=root
datasource.password=Abc@123456
datasource.initalSize=5
datasource.minIdel=5
datasource.maxActive=20
datasource.maxWait=3000
(2)c3p0连接池
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名称?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
jdbc.username=root
jdbc.password=Abc@123456
applicationContext.xml
1、application-context.xml是全局的,应用于多个serverlet,配合listener一起使用,web.xml中配置如下:
<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><import resource&#61;"spring-dao.xml"/><import resource&#61;"spring-service.xml"/><import resource&#61;"spring-mvc.xml"/>beans>
web.xml
<servlet><servlet-name>DispatcherServletservlet-name><servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class><init-param><param-name>contextConfigLocationparam-name><param-value>classpath:applicationContext.xmlparam-value>init-param><load-on-startup>1load-on-startup>
servlet>
<servlet-mapping><servlet-name>DispatcherServletservlet-name><url-pattern>/url-pattern>
servlet-mapping>
<filter><filter-name>encodingFilterfilter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class><init-param><param-name>encodingparam-name><param-value>utf-8param-value>init-param>
filter>
<filter-mapping><filter-name>encodingFilterfilter-name><url-pattern>/*url-pattern>
filter-mapping>
<session-config><session-timeout>100session-timeout>
session-config>
2. mybatis-config.xml
DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><setting name&#61;"logImpl" value&#61;"STDOUT_LOGGING"/><setting name&#61;"mapUnderscoreToCamelCase" value&#61;"true"/><setting name&#61;"useGeneratedKeys" value&#61;"true"/><setting name&#61;"defaultExecutorType" value&#61;"REUSE"/><setting name&#61;"lazyLoadingEnabled" value&#61;"true"/><setting name&#61;"defaultStatementTimeout" value&#61;"120"/>settings><typeAliases><package name&#61;"com.hgxit.ssmbuild.pojo"/>typeAliases><mappers><package name&#61;"com.hgxit.ssmbuild.dao"/>mappers>configuration>
3. spring-dao.xml
<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xmlns:context&#61;"http://www.springframework.org/schema/context"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><context:property-placeholder location&#61;"classpath:database.properties"/><bean id&#61;"dataSource" class&#61;"com.mchange.v2.c3p0.ComboPooledDataSource"><property name&#61;"driverClass" value&#61;"${jdbc.driver}"/><property name&#61;"jdbcUrl" value&#61;"${jdbc.url}"/><property name&#61;"user" value&#61;"${jdbc.username}"/><property name&#61;"password" value&#61;"${jdbc.password}"/><property name&#61;"maxPoolSize" value&#61;"30"/><property name&#61;"minPoolSize" value&#61;"10"/><property name&#61;"autoCommitOnClose" value&#61;"false"/><property name&#61;"checkoutTimeout" value&#61;"10000"/><property name&#61;"acquireRetryAttempts" value&#61;"2"/>bean>
<bean id&#61;"sqlSessionFactory" class&#61;"org.mybatis.spring.SqlSessionFactoryBean"><property name&#61;"dataSource" ref&#61;"dataSource"/><property name&#61;"configLocation" value&#61;"classpath:mybatis-config.xml"/>bean><bean class&#61;"org.mybatis.spring.mapper.MapperScannerConfigurer"><property name&#61;"sqlSessionFactoryBeanName" value&#61;"sqlSessionFactory"/><property name&#61;"basePackage" value&#61;"com.hgxit.ssmbuild.dao"/>bean>
beans>
4. spring-mvc.xml
配置视图解析器&#xff0c;和扫描web相关的bean自动注入
<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xmlns:context&#61;"http://www.springframework.org/schema/context"xmlns:mvc&#61;"http://www.springframework.org/schema/mvc"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttps://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package&#61;"com.hgxit.ssmbuild.controller" /><mvc:default-servlet-handler /><mvc:annotation-driven /><bean class&#61;"org.springframework.web.servlet.view.InternalResourceViewResolver"><property name&#61;"viewClass" value&#61;"org.springframework.web.servlet.view.JstlView" /><property name&#61;"prefix" value&#61;"/WEB-INF/jsp/" /><property name&#61;"suffix" value&#61;".jsp" />bean>beans>
5. spring-service.xml
<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xmlns:context&#61;"http://www.springframework.org/schema/context"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package&#61;"com.hgxit.ssmbuild.service" /><bean id&#61;"BookServiceImpl" class&#61;"com.hgxit.ssmbuild.service.BookServiceImpl"><property name&#61;"bookMapper" ref&#61;"bookMapper"/>bean><bean id&#61;"transactionManager" class&#61;"org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name&#61;"dataSource" ref&#61;"dataSource"/>bean>beans>