SSM配置简单示例

SSM配置

目录结构

tree /f > catalogTree.txt

│ generatorConfig.xml
│ pom.xml
├─src
│ └─main
│ │ └─java
│ │ │ └─com
│ │ │ │ └─xxx
│ │ │ │ │ └─controller
│ │ │ │ │ ├─dao
│ │ │ │ │ ├─entity
│ │ │ │ │ ├─listener
│ │ │ │ │ ├─mapper
│ │ │ │ │ ├─service
│ │ │ │ │ └─utils
│ │ ├─resources
│ │ │ │ applicationContext.xml
│ │ │ │ dbConfig.properties
│ │ │ │ logback.xml
│ │ │ │ mybatisConfig.xml
│ │ │ └─mapper
│ │ └─webapp
│ │ │ index.html
│ │ ├─static
│ │ └─WEB-INF
│ │ │ dispatcherServlet-servlet.xml
│ │ │ web.xml
│ │ └─views
..

配置

generatorConfig.xml

generatorConfig.xml (src 同级)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--        默认是false:添加注释,这个标签必须放在最前面   -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--        数据库连接信息-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/project_xxx"
                        userId="root"
                        password="0101">
        </jdbcConnection>
        <javaTypeResolver>
            <!--
                默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
                    true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
            -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--        指定Entity生成位置-->
        <javaModelGenerator targetPackage="com.xxx.entity"
                            targetProject="src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaModelGenerator>

        <!--        指定Mapper.xml文件生成位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--        指定dao接口生成位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.dao" targetProject="src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--        指定每个表的生成策略  -->
        <table tableName="表名" domainObjectName="生成类的类名"/>
    </context>

</generatorConfiguration>

dispatcherServlet-servlet.xml

dispatcherServlet-servlet.xml (WEB-INF下)

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

    <!--    spring mvc配置文件  包含网站跳转逻辑控制-->
    <!--扫描业务逻辑组件-->
    <context:component-scan base-package="com.xxx" use-default-filters="false">
        <!-- 只扫描控制器-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--    视图解析器,方便页面返回-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".html"/>
    </bean>

    <!--两个标准配置  -->
    <!-- 将spring mvc不能处理的请求交给tomcat -->
    <mvc:default-servlet-handler/>
    <!-- 能支持spring mvc更高级的一些功能,JSR303校验,快捷的ajax...映射动态请求 -->
    <mvc:annotation-driven/>

</beans>

web.xml

web.xml (WEB-INF下)

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app 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"
         version="2.5">
  <!--  启动 spring 的容器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--  字符编码过滤器 注意放在所有过滤器之前-->
  <filter>
    <filter-name>CharacterEncodingFilter</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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <!--    又有请求都处理编码格式-->
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--    使用Rest风格的URI 处理页面发过来的请求,如普通的post转delete或put-->
  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--    处理PUT请求     HiddenHttpMethodFilter已被弃用-->
  <filter>
    <filter-name>formContentFilter</filter-name>
    <filter-class>org.springframework.web.filter.FormContentFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>formContentFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--  spring mvc 的前端控制器 拦截所有请求 -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--指定mvc位置 如果不配置,需要有与web.xml文件同级的springDispatcherServlet-servlet.xml-->
    <!--            <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>dispatcherServlet</servlet-name>
    <!--    拦截路径 所有-->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

applicationContext.xml

applicationContext.xml (resources 下)

<?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:aop="http://www.springframework.org/schema/aop"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd ">
    <!--  配置文件,主要配置和业务逻辑有关的-->


    <!--  解决乱码  Content-Type: text/plain;charset=ISO-8859-1  -->
    <!--    <bean class="org.springframework.http.converter.StringHttpMessageConverter">-->
    <!--        <property name="supportedMediaTypes">-->
    <!--            <list>-->
    <!--                <value>text/plain;charset=UTF-8</value>-->
    <!--            </list>-->
    <!--        </property>-->
    <!--    </bean>-->


    <!--    扫描包  除了控制器以外的所有-->
    <context:component-scan base-package="com.xxx">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--    引入配置文件-->
    <context:property-placeholder location="classpath:dbConfig.properties"/>
    <!--    destroy-method="close"  注销区东程序 防止内存泄漏   -->
    <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
        <property name="driverClass" value="${jdbc.class}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--================== 配置和MyBatis的整合=============== -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定mybatis全局配置文件的位置 -->
        <property name="configLocation" value="classpath:mybatisConfig.xml"/>
        <property name="dataSource" ref="pooledDataSource"/>
        <!-- 指定mybatis,mapper文件的位置 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>

    <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--扫描所有dao接口的实现,加入到ioc容器中 -->
        <property name="basePackage" value="com.xxx.dao"/>
        <!--        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />-->
    </bean>

    <!-- 配置一个可以执行批量(select,update等。。。)的sqlSession -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <!-- 将defaultExecutorType配置为BATCH,可以批量更新操作缓存SQL以提高性能,
                但是有个缺陷就是无法获取update、delete返回的行数。-->
        <constructor-arg name="executorType" value="BATCH"/>
    </bean>
    <!--=============================================  -->

    <!--     实现的接口放到ioc中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--   扫描接口-->
        <property name="basePackage" value="com.xxx.dao"/>
    </bean>

    <!--    事务控制    -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--   控制的数据源-->
        <property name="dataSource" ref="pooledDataSource"/>
    </bean>
    <!--  开启基于注解的事务     -->
    <aop:config>
        <!--  切入点表达式-->
        <aop:pointcut id="txPoint" expression="execution(* com.xxx.service..*(..))"/>
        <!--  配置事务增强-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>
    <!--   事务增强,切入方法    -->
    <tx:advice id="txAdvice">
        <tx:attributes>
            <!--   所有方法都是事务方法-->
            <tx:method name="*"/>
            <!--   认为是获取的方法,只读-->
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

</beans>

dbConfig.properties

dbConfig.properties (resources 下)

jdbc.jdbcUrl = 
jdbc.class = 
jdbc.user = 
jdbc.password = 

logback.xml

logback.xml (resources 下)

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">

    <!--定义日志文件的存储地址 -->
    <!--    <property name="LOG_HOME" value=""/>-->

    <!-- 控制台输出 -->
    <appender name="ConsoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    
    <!--日志等级设置-->
    <logger name="org.springframework" level="ALL"/>
    
    <root level="ALL">
        <appender-ref ref="ConsoleLog"/>
    </root>

    <logger name="com.xxx" level="ALL">
        <appender-ref ref="FileInfoLog"/>
        <appender-ref ref="FileErrorLog"/>
    </logger>

</configuration>

mybatisConfig.xml

mybatisConfig.xml (resources 下)

<?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="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <!--    别名-->
    <typeAliases>
        <package name="com.xxx.entity"/>
    </typeAliases>
    <!--    注册分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"/>
    </plugins>
</configuration>
posted @ 2022-08-29 22:37  夏末秋初~  阅读(18)  评论(0编辑  收藏  举报