ssm整合框架基础配置文件

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中配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <import resource="spring-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>


</beans>
web.xml
<!--1.配置过滤器and前端控制器andSesstion过期时间-->
<!--DispatcherServlet-->
<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!-->
        <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!--encodingFilter-->
<filter>
    <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>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!--Session过期时间-->
<session-config>
    <session-timeout>100</session-timeout>
</session-config>
2. 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>
        <!--配置运行的sql在控制台打印输出-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <!--开启驼峰映射-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--全局设置主键自动回填-->
        <setting name="useGeneratedKeys" value="true"/>
        <!--配置默认的执行器,SIMPLE会直接执行sql没有什么特别的。REUSE会重用预处理器,也就是会重用同一个Sql的Statement。BATCH 通过批量操作来优化性能-->
        <setting name="defaultExecutorType" value="REUSE"/>
        <!--开启延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--设置超时时间,就是sql执行的超时时间,单位秒-->
        <setting name="defaultStatementTimeout" value="120"/>
    </settings>
    <!--设置dao的别名(取小名)-->
    <typeAliases>
        <package name="com.hgxit.ssmbuild.pojo"/>
    </typeAliases>
    <!--需要映射的mapper-->
    <mappers>
        <!--映射指定包下面的mapper-->
        <package name="com.hgxit.ssmbuild.dao"/>
        <!--指定映射某个xml-->
        <!--<mapper resource="com.hgxit.ssmbuild.dao.BookMapper.xml"/>-->
        <!--指定映射某个类-->
        <!--<mapper class="com.hgxit.ssmbuild.dao.BookMapper"/>-->
    </mappers>

</configuration>
3. spring-dao.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: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/context
       https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置整合mybatis -->
    <!-- 1.关联数据库文件 -->
    <context:property-placeholder location="classpath:database.properties"/>

    <!-- 2.数据库连接池 -->
    <!--数据库连接池
        c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置连接池属性 -->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

    <!--druid注入数据源-->
<!--    <bean class="${datasource.class}" id="dataSource">-->
<!--        <property name="driverClassName" value="${datasource.driver}"/>-->
<!--        <property name="url" value="${datasource.url}"/>-->
<!--        <property name="username" value="${datasource.username}"/>-->
<!--        <property name="password" value="${datasource.password}"/>-->
<!--        <property name="initialSize" value="${datasource.initalSize}"/>-->
<!--        <property name="minIdle" value="${datasource.minIdel}"/>-->
<!--        <property name="maxActive" value="${datasource.maxActive}"/>-->
<!--        <property name="maxWait" value="${datasource.maxWait}"/>-->
<!--    </bean>-->
 
  
  <!--
      从Mybatis的介绍中,可以知道SqlSessionFactory是产生SqlSession的基础,
      因此配置SqlSessionFactory十分关键。在Mybatis-Spring项目中提供了
      SqlSessionFactoryBean去支持SqlSessionFactory的配置
  -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    
    <!--
    	配置一个扫描器,它能够自动的扫描这些Mapper然后注入到SpringIoc容器中。
    	配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 
     -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.hgxit.ssmbuild.dao"/>
        <!--指定对标注了某个注解的Mapper我们要注入,如果不加入就默认注入包下所有mapper-->
        <!--<property name="annotationClass" value="org.springframework.stereotype.Repository"/>-->
    </bean>
</beans>
4. spring-mvc.xml

配置视图解析器,和扫描web相关的bean自动注入

<?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/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc
   https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
    <!-- 扫描web相关的bean,@Controller -->
    <context:component-scan base-package="com.hgxit.ssmbuild.controller" />

    <!-- 让Spring MVC不处理静态资源 -->
    <mvc:default-servlet-handler />
    
    <!--
    支持mvc注解驱动
        在spring中一般采用@RequestMapping注解来完成映射关系
        要想使@RequestMapping注解生效
        必须向上下文中注册DefaultAnnotationHandlerMapping
        和一个AnnotationMethodHandlerAdapter实例
        这两个实例分别在类级别和方法级别处理。
        而annotation-driven配置帮助我们自动完成上述两个实例的注入。
     -->
    <mvc:annotation-driven />

    <!-- 3.配置jsp 显示ViewResolver视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>
5. spring-service.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: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/context
   http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫描service相关的bean,注入到IOC容器-->
    <context:component-scan base-package="com.hgxit.ssmbuild.service" />

    <!--BookServiceImpl注入到IOC容器中,可以在类上加@Service注解代替-->
    <bean id="BookServiceImpl" class="com.hgxit.ssmbuild.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值