• 整理spring + mysql + redis + 测试 的配置格式 和源码


    经过多次整理,最终以这样的文件格式配置

    目前配好的基本模板:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
     5          version="4.0">
     6 <!--    &lt;!&ndash;    首页&ndash;&gt;-->
     7 <!--    <welcome-file-list>-->
     8 <!--        <welcome-file>/WEB-INF/jsp/toIndex.jsp</welcome-file>-->
     9 <!--    </welcome-file-list>-->
    10 
    11 
    12     <!-- 加载spring容器 -->
    13     <context-param>
    14         <param-name>contextConfigLocation</param-name>
    15         <param-value>classpath:spring/springcontext-*.xml</param-value>
    16     </context-param>
    17     <listener>
    18         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    19     </listener>
    20 
    21     <servlet>
    22         <servlet-name>dispatcher</servlet-name>
    23         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    24         <init-param>
    25             <param-name>contextConfigLocation</param-name>
    26             <param-value>classpath:spring/dispatcher-servlet.xml</param-value>
    27         </init-param>
    28         <load-on-startup>1</load-on-startup>
    29     </servlet>
    30     <servlet-mapping>
    31         <servlet-name>dispatcher</servlet-name>
    32         <url-pattern>/</url-pattern>
    33     </servlet-mapping>
    34 
    35 
    36     <!-- post请求乱码拦截器 -->
    37     <filter>
    38         <filter-name>CharacterEncodingFilter</filter-name>
    39         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    40         <init-param>
    41             <param-name>encoding</param-name>
    42             <param-value>utf-8</param-value>
    43         </init-param>
    44     </filter>
    45     <filter-mapping>
    46         <filter-name>CharacterEncodingFilter</filter-name>
    47         <url-pattern>/*</url-pattern>
    48     </filter-mapping>
    49 
    50 <!--    &lt;!&ndash;自定义监听,根据sessionid获取session&ndash;&gt;-->
    51 <!--    <listener>-->
    52 <!--        <listener-class>com.songs.monitor.MySessionListener</listener-class>-->
    53 <!--    </listener>-->
    54 </web-app>
    web.xml
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xmlns:mvc="http://www.springframework.org/schema/mvc"
     6        xsi:schemaLocation="http://www.springframework.org/schema/beans
     7     http://www.springframework.org/schema/beans/spring-beans.xsd
     8     http://www.springframework.org/schema/context
     9     http://www.springframework.org/schema/context/spring-context.xsd
    10     http://www.springframework.org/schema/mvc
    11     http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    12     <!-- 配置SpringMVC -->
    13     <!-- 1.开启SpringMVC注解模式 -->
    14     <!-- 简化配置:
    15         (1)自动注册DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter
    16         (2)提供一些列:数据绑定,数字和日期的format @NumberFormat, @DateTimeFormat, xml,json默认读写支持
    17     -->
    18     <mvc:annotation-driven/>
    19 
    20 
    21     <!-- 2.静态资源默认servlet配置
    22         (1)加入对静态资源的处理:js,gif,png
    23         (2)允许使用"/"做整体映射
    24      -->
    25     <mvc:default-servlet-handler/>
    26 
    27     <!-- 3.配置视图解析器-->
    28     <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    29         <property name="prefix" value="/WEB-INF/jsp/"/>
    30         <property name="suffix" value=".jsp"/>
    31     </bean>
    32 
    33     <!-- 4.扫描web相关的bean -->
    34     <context:component-scan base-package="cn.cen2guo.clinic.web"/>
    35 
    36 
    37     <!--&lt;!&ndash;  拦截器&ndash;&gt;-->
    38     <!--    <mvc:interceptors>-->
    39     <!--        <mvc:interceptor>-->
    40     <!--&lt;!&ndash;            拦截所有&ndash;&gt;-->
    41     <!--            <mvc:mapping path="/**"/>-->
    42     <!--&lt;!&ndash;  设置不拦截请求&ndash;&gt;-->
    43     <!--            <mvc:exclude-mapping path="/**/fonts"/>-->
    44     <!--            <mvc:exclude-mapping path="/**/*.css"/>-->
    45     <!--            <mvc:exclude-mapping path="/**/*.js"/>-->
    46     <!--            <mvc:exclude-mapping path="/**/*.png"/>-->
    47     <!--            <mvc:exclude-mapping path="/**/*.gif"/>-->
    48     <!--            <mvc:exclude-mapping path="/**/*.jpg"/>-->
    49     <!--            <mvc:exclude-mapping path="/**/*.jpeg"/>-->
    50     <!--            <mvc:exclude-mapping path="/**/*Login*"/>-->
    51     <!--            <mvc:exclude-mapping path="/**/*login*"/>-->
    52     <!--            <mvc:exclude-mapping path="/**/logOut"/>-->
    53     <!--&lt;!&ndash;拦截逻辑类&ndash;&gt;-->
    54     <!--            <bean class="com.songs.interceptor.MyInterceptor"/>-->
    55     <!--        </mvc:interceptor>-->
    56     <!--    </mvc:interceptors>-->
    57 
    58 
    59     <!--文件上传  -->
    60     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    61 
    62 
    63 
    64 
    65 </beans>
    dispatcher-servlet.xml
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xmlns:tx="http://www.springframework.org/schema/tx"
     6        xmlns:aop="http://www.springframework.org/schema/aop"
     7        xsi:schemaLocation="http://www.springframework.org/schema/beans
     8     http://www.springframework.org/schema/beans/spring-beans.xsd
     9     http://www.springframework.org/schema/context
    10     http://www.springframework.org/schema/context/spring-context.xsd
    11     http://www.springframework.org/schema/tx
    12     http://www.springframework.org/schema/tx/spring-tx.xsd
    13     http://www.springframework.org/schema/aop
    14     http://www.springframework.org/schema/aop/spring-aop.xsd">
    15     <!-- 扫描service包下所有使用注解的类型 -->
    16     <context:component-scan base-package="cn.cen2guo.clinic.service" />
    17 
    18     <!-- 配置事务管理器 -->
    19     <bean id="transactionManager"
    20           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    21         <!-- 注入数据库连接池 -->
    22         <property name="dataSource" ref="dataSource" />
    23     </bean>
    24 
    25     <!-- 通知 -->
    26     <tx:advice id="txAdvice" transaction-manager="transactionManager">
    27         <tx:attributes>
    28             <!-- 传播行为 -->
    29             <tx:method name="save*" propagation="REQUIRED"/>
    30             <tx:method name="insert*" propagation="REQUIRED"/>
    31             <tx:method name="delete*" propagation="REQUIRED"/>
    32             <tx:method name="update*" propagation="REQUIRED"/>
    33             <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
    34             <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
    35         </tx:attributes>
    36     </tx:advice>
    37     <!-- 切面 -->
    38     <aop:config>
    39         <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.cen2guo.clinic.service.*.*(..))"/>
    40     </aop:config>
    41 
    42 </beans>
    springcontext-service.xml
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans
     6     http://www.springframework.org/schema/beans/spring-beans.xsd">
     7 
     8 
     9     <!-- spring的属性加载器,加载所有properties文件中的属性,供所有springcontext-*.xml文件共同使用 -->
    10     <bean id="configPropertyConfigurer"
    11           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    12         <property name="locations">
    13             <list>
    14                 <!--这样写可以引入多个properties文件-->
    15                 <!-- <value>/WEB-INF/configInfo.properties</value> -->
    16                 <value>classpath:redis.properties</value>
    17                 <value>classpath:jdbc.properties</value>
    18             </list>
    19         </property>
    20     </bean>
    21 
    22     <!-- 导入redis配置文件-->
    23     <import resource="classpath:redis/redisConfigure.xml"/>
    24 
    25     <!--导入mysql配置文件-->
    26     <import resource="classpath:mysql/mysqlConfigure.xml"/>
    27 
    28     <!-- 导入自定义注册构造注入的bean-->
    29     <import resource="classpath:myxml/my_javabean.xml"/>
    30 
    31 </beans>
    springcontext-dao.xml
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans
     6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
     7 
     8 
     9     <!-- Redis连接池配置 -->
    10     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    11         <!-- 控制一个pool能分配多少个jedis实例 -->
    12         <property name="maxTotal" value="${redis.pool.maxActive}"/>
    13         <!-- 连接池中最多空闲多少个maxIdle个连接,这里为20,表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除,处于待命状态,随时连接 -->
    14         <property name="maxIdle" value="${redis.pool.maxIdle}"/>
    15         <!-- 最大等待时间,当没有可用连接时,连接池等待连接被归还的最大时间(以毫秒计数),超过时间即抛出异常 -->
    16         <property name="maxWaitMillis" value="${redis.pool.maxWait}"/>
    17         <!-- 在获取连接时,检查有效性 -->
    18         <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
    19     </bean>
    20     <!-- 创建Redis连接池,并做相关配置 -->
    21     <bean id="jedisWritePool" class="cn.cen2guo.clinic.redis.JedisPoolWriper"
    22           depends-on="jedisPoolConfig">
    23         <constructor-arg index="0" ref="jedisPoolConfig"/>
    24         <constructor-arg index="1" value="${redis.hostname}"/>
    25         <constructor-arg index="2" value="${redis.port}" type="int"/>
    26         <constructor-arg index="3" value="${redis.timeout}" type="int"/>
    27         <constructor-arg index="4" value="${redis.password}"/>
    28     </bean>
    29     <!-- 创建Redis工具类,封装好Redis的连接以进行相关操作 -->
    30     <bean id="jedisUtil" class="cn.cen2guo.clinic.redis.JedisUtil"
    31     >
    32         <property name="jedisPool" ref="jedisWritePool"/>
    33     </bean>
    34 <!--    这里的红色下划线提示不是指写错了,而是警告,如果使用自动补全修正,会报错UnsatisfiedDependencyException
    35 为什么使用$进行隔开呢,是因为这是两个类嵌套定义,因为不是文件路径,无法使用.符号进行区分,故使用$符号时没问题的
    36 -->
    37     <bean id="jedisKeys" class="cn.cen2guo.clinic.redis.JedisUtil$Keys"
    38     >
    39         <constructor-arg ref="jedisUtil"/>
    40     </bean>
    41     <bean id="jedisStrings" class="cn.cen2guo.clinic.redis.JedisUtil$Strings"
    42     >
    43         <constructor-arg ref="jedisUtil"/>
    44     </bean>
    45     <bean id="jedisLists" class="cn.cen2guo.clinic.redis.JedisUtil$Lists"
    46     >
    47         <constructor-arg ref="jedisUtil"/>
    48     </bean>
    49     <bean id="jedisSets" class="cn.cen2guo.clinic.redis.JedisUtil$Sets"
    50     >
    51         <constructor-arg ref="jedisUtil"/>
    52     </bean>
    53     <bean id="jedisHash" class="cn.cen2guo.clinic.redis.JedisUtil$Hash"
    54     >
    55         <constructor-arg ref="jedisUtil"/>
    56     </bean>
    57 
    58 
    59 </beans>
    redisConfigure.xml
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans
     6     http://www.springframework.org/schema/beans/spring-beans.xsd">
     7 
     8     <!-- 配置 数据源 -->
     9     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    10         <!-- 驱动 -->
    11         <property name="driverClassName" value="${jdbc.driverClassName}"/>
    12         <!-- url -->
    13         <property name="url" value="${jdbc.url}"/>
    14         <!-- 用户名 -->
    15         <property name="username" value="${jdbc.username}"/>
    16         <!-- 密码 -->
    17         <property name="password" value="${jdbc.password}"/>
    18     </bean>
    19 
    20     <!-- 配置 Mybatis的工厂 -->
    21     <bean class="org.mybatis.spring.SqlSessionFactoryBean">
    22         <!-- 数据源 -->
    23         <property name="dataSource" ref="dataSource"/>
    24         <!-- 配置Mybatis的核心 配置文件所在位置 -->
    25         <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
    26         <!-- 配置pojo别名 -->
    27         <property name="typeAliasesPackage" value="cn.cen2guo.clinic.entity"/>
    28         <!--当mapper中的接口文件与xml文件在同一个包下但是不在同一级时-->
    29         <!--需要指定mapper 的xml文件路径,如果在同一级则可不写-->
    30         <!-- 否则会报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)-->
    31         <property name="mapperLocations" value="classpath:cn/cen2guo/clinic/mapper/mapperXML/*.xml"/>
    32     </bean>
    33 
    34     <!--扫描mapper接口, 写在此包下即可被扫描到 -->
    35     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    36         <property name="basePackage" value="cn.cen2guo.clinic.mapper"/>
    37     </bean>
    38 </beans>
    mysqlConfigure.xml
    1 <?xml version="1.0" encoding="UTF-8" ?>
    2 <beans xmlns="http://www.springframework.org/schema/beans"
    3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4        xsi:schemaLocation="http://www.springframework.org/schema/beans
    5     http://www.springframework.org/schema/beans/spring-beans.xsd">
    6     <!-- 位置信息服务接口-->
    7     <bean id="locationService" class="cn.cen2guo.clinic.service.serviceImpl.LocationServiceImpl"/>
    8 
    9 </beans>
    my_javabean.xml
     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE configuration
     3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
     4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
     5 <configuration>
     6     <settings>
     7         <!-- 开启log4j日志,打印到控制台,关闭则注释掉该语句即可-->
     8 <!--        <setting name="logImpl" value="STDOUT_LOGGING"/>-->
     9 
    10         <!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
    11         <setting name="mapUnderscoreToCamelCase" value="true"/>
    12         <!-- 使用列别名替换列名 默认:true -->
    13         <setting name="useColumnLabel" value="true"/>
    14         <!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
    15         <setting name="useGeneratedKeys" value="true"/>
    16     </settings>
    17     <!-- pageHelper分页插件 -->
    18 <!--    <plugins>-->
    19 <!--        <plugin interceptor="com.github.pagehelper.PageHelper">-->
    20 <!--            <property name="dialect" value="mysql"/>-->
    21 <!--            <property name="offsetAsPageNum" value="true"/>-->
    22 <!--            &lt;!&ndash; rowBoundsWithCount设置为true时,使用 RowBounds 分页会进行 count 查询。 &ndash;&gt;-->
    23 <!--            <property name="rowBoundsWithCount" value="true"/>-->
    24 <!--            &lt;!&ndash; pageSizeZero 为 true, 当 pageSize=0 或者 RowBounds.limit = 0 就会查询出全部的结果 &ndash;&gt;-->
    25 <!--            <property name="pageSizeZero" value="true"/>-->
    26 <!--            &lt;!&ndash; reasonable 为 true,这时如果 pageNum<=0 会查询第一页,如果 pageNum>总页数 会查询最后一页 &ndash;&gt;-->
    27 <!--            <property name="reasonable" value="true"/>-->
    28 <!--            <property name="returnPageInfo" value="check"/>-->
    29 <!--        </plugin>-->
    30 <!--    </plugins>-->
    31 </configuration>
    mybatis-config.xml
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE generatorConfiguration
     3         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
     4         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
     5 <generatorConfiguration>
     6     <context id="testTables" targetRuntime="MyBatis3">
     7         <commentGenerator>
     8             <!-- 是否去除自动生成的注释 true:是 : false:否 -->
     9             <property name="suppressAllComments" value="true"/>
    10         </commentGenerator>
    11         <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
    12         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
    13                         connectionURL="jdbc:mysql://localhost:3306/clinic"
    14                         userId="root"
    15                         password="mysql">
    16         </jdbcConnection>
    17         <!--
    18             默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,
    19             为true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal
    20         -->
    21         <javaTypeResolver>
    22             <property name="forceBigDecimals" value="false"/>
    23         </javaTypeResolver>
    24 
    25         <!-- targetProject:生成PO类的位置,重要!! -->
    26         <javaModelGenerator targetPackage="cn.cen2guo.clinic.entity"
    27                             targetProject="src/main/java">
    28             <!-- enableSubPackages:是否让schema作为包的后缀 -->
    29             <property name="enableSubPackages" value="false"/>
    30             <!-- 从数据库返回的值被清理前后的空格 -->
    31             <property name="trimStrings" value="true"/>
    32         </javaModelGenerator>
    33         <!-- targetProject:mapper映射文件生成的位置,重要!! -->
    34         <sqlMapGenerator targetPackage="cn.cen2guo.clinic.mapper.mapperXML"
    35                          targetProject="src/main/java">
    36             <property name="enableSubPackages" value="false"/>
    37         </sqlMapGenerator>
    38         <!-- targetPackage:mapper接口生成的位置,重要!! -->
    39         <javaClientGenerator type="XMLMAPPER"
    40                              targetPackage="cn.cen2guo.clinic.mapper"
    41                              targetProject="src/main/java">
    42             <!-- 是否让schema作为包的后缀 -->
    43             <property name="enableSubPackages" value="fasle"/>
    44         </javaClientGenerator>
    45 
    46 
    47         <!-- 指定数据库表,要生成哪些表,就写哪些表,要和数据库中对应,不能写错! -->
    48 <!--        <table tableName="t_province" enableCountByExample="false" enableUpdateByExample="false"-->
    49 <!--               enableSelectByExample="false" enableDeleteByExample="false"-->
    50 <!--               selectByExampleQueryId="false"/>-->
    51 <!--        <table tableName="t_city" enableCountByExample="false" enableUpdateByExample="false"-->
    52 <!--               enableSelectByExample="false" enableDeleteByExample="false"-->
    53 <!--               selectByExampleQueryId="false"/>-->
    54 <!--        <table tableName="t_region" enableCountByExample="false" enableUpdateByExample="false"-->
    55 <!--               enableSelectByExample="false" enableDeleteByExample="false"-->
    56 <!--               selectByExampleQueryId="false"/>-->
    57 
    58     </context>
    59 </generatorConfiguration>
    mybatis-generator.xml

    单元测试:

    测试时,需要用到mysql和redis,那么只需要获取dao层xml配置文件即可,当然,无法使用spring的@Service  ,使用接口则必须手动构造注入bean,

    然后由配置文件的ApplicationContext ,获取javabean , 这就是为什么还需要 有 my_javabean.xml 这个文件的原因,调用接口和接口的方法里使用redis和mysql的方法无变化,正常使用即可。

  • 相关阅读:
    zabbix报警把特定的应用集发送给developer
    logstash 判断接口响应时间发送zabbix告警
    zabbix 对于logstash告警连续发邮件
    java使double保留两位小数的多方法
    Vagrant 和 docker
    golang binarySearch
    go channel实现
    5个jvm命令
    字符串匹配的Boyer-Moore算法
    Rabin-Karp 算法
  • 原文地址:https://www.cnblogs.com/c2g5201314/p/12246393.html
Copyright © 2020-2023  润新知