spring-mybatis.xml文件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xmlns:task="http://www.springframework.org/schema/task" 9 xsi:schemaLocation="http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-4.0.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 15 http://www.springframework.org/schema/tx 16 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 17 http://www.springframework.org/schema/mvc 18 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 19 http://www.springframework.org/schema/task 20 http://www.springframework.org/schema/task/spring-task-3.1.xsd"> 21 <!-- 自动扫描 --> 22 <context:component-scan base-package="com.demo"/> 23 <!-- 引入配置文件 --> 24 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 25 <property name="location" value="classpath:application.properties"/> 26 </bean> 27 <!-- 数据源 --> 28 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 29 <property name="url" value="${spring.datasource.url}" /> 30 <property name="username" value="${spring.datasource.username}" /> 31 <property name="password" value="${spring.datasource.password}" /> 32 <!-- 初始化连接大小 --> 33 <property name="initialSize" value="${initialSize}" /> 34 <!-- 连接池最大使用连接数量 --> 35 <property name="maxActive" value="${maxActive}" /> 36 <!-- 连接池最大空闲 --> 37 <!-- <property name="maxIdle" value="${maxIdle}" /> --> 38 <!-- 连接池最小空闲 --> 39 <property name="minIdle" value="${minIdle}" /> 40 <!-- 获取连接最大等待时间 --> 41 <property name="maxWait" value="${maxWait}" /> 42 <property name="validationQuery" value="SELECT 1" /> 43 <property name="testWhileIdle" value="true" /> 44 <property name="testOnBorrow" value="false" /> 45 <property name="testOnReturn" value="false" /> 46 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 47 <property name="timeBetweenEvictionRunsMillis" value="60000" /> 48 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 49 <property name="minEvictableIdleTimeMillis" value="25200000" /> 50 <property name="filters" value="mergeStat" /> 51 </bean> 52 <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> 53 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 54 <property name="dataSource" ref="dataSource"/> 55 <!-- 自动扫描mapping.xml文件 --> 56 <property name="mapperLocations" value="${mybatis.xml-locations}"/> 57 </bean> 58 <!-- Dao接口所在包名,Spring会自动查找其下的类 --> 59 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 60 <property name="basePackage" value="${mybatis.dao-locations}"/> 61 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 62 </bean> 63 <!-- 事务管理 --> 64 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 65 <property name="dataSource" ref="dataSource"/> 66 </bean> 67 <!-- 声明式 Spring AOP的方式实现事务管理 --> 68 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 69 <tx:attributes> 70 <tx:method name="query*" propagation="SUPPORTS" read-only="true"/> 71 <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> 72 <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> 73 <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> 74 <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" /> 75 <tx:method name="create*" propagation="REQUIRED" rollback-for="Exception"/> 76 <tx:method name="add**" propagation="REQUIRED" rollback-for="Exception"/> 77 <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/> 78 <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/> 79 <tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception"/> 80 <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/> 81 </tx:attributes> 82 </tx:advice> 83 <!-- 第一个*表示返回值,第二个*表示impl下class,第三个*表示方法名,(..)表示方法参数 --> 84 <aop:config> 85 <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.demo.service.*.*(..))"/> 86 </aop:config> 87 </beans>
spring-mvc.xml文件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:task="http://www.springframework.org/schema/task" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context-4.0.xsd 14 http://www.springframework.org/schema/mvc 15 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 16 http://www.springframework.org/schema/task 17 http://www.springframework.org/schema/task/spring-task-4.0.xsd"> 18 <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> 19 <context:component-scan base-package="com.demo.controller"/> 20 <mvc:annotation-driven> 21 <mvc:message-converters> 22 <!-- 避免IE执行AJAX时, 返回JSON出现下载文件 --> 23 <bean id="mappingJacksonHttpMessageConverter" 24 class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 25 <property name="supportedMediaTypes"> 26 <list> 27 <value>text/html;charset=UTF-8</value> 28 </list> 29 </property> 30 </bean> 31 </mvc:message-converters> 32 </mvc:annotation-driven> 33 <!-- 文件上传 --> 34 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 35 <!-- 默认编码 --> 36 <property name="defaultEncoding" value="utf-8" /> 37 <!-- 文件大小最大值 --> 38 <property name="maxUploadSize" value="5000000"/> 39 <!-- 内存中的最大值 --> 40 <property name="maxInMemorySize" value="40960" /> 41 </bean> 42 <!-- 定义跳转的文件的前后缀 ,视图模式配置--> 43 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 44 <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> 45 <property name="prefix" value="/WEB-INF/jsp/" /> 46 <property name="suffix" value=".jsp" /> 47 </bean> 48 <!-- 资源文件映射 --> 49 <mvc:resources location="/WEB-INF/assets/" mapping="/assets/**"/> 50 <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/> 51 <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/> 52 </beans>
application.properties文件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 ##数据库连接信息 2 spring.datasource.driver-class-name=com.mysql.jdbc.Driver 3 spring.datasource.url=jdbc:mysql://localhost:3306/grd?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT&tinyInt1isBit=false 4 spring.datasource.username=root 5 spring.datasource.password=SFgrd123$ 6 7 #定义初始连接数 8 initialSize=0 9 #定义最大连接数 10 maxActive=20 11 #定义最大空闲 12 maxIdle=20 13 #定义最小空闲 14 minIdle=1 15 #定义最长等待时间 16 maxWait=60000 17 18 ##指向mapper的xml文件位置 19 mybatis.xml-locations=classpath*:db/mappers/*.xml 20 mybatis.dao-locations=com.demo.mapper.* 21 ##指向实体类的位置 22 mybatis.entity-locations=com.demo.entity.* 23 24 25 #页面热加载 26 spring.thymeleaf.cache = false 27 #访问相关配置 28 server.port=8888 29 server.tomcat.uri-encoding=UTF-8 30 #项目访问名称,如果不配置直接访问bean就可以 31 server.servlet.context-path=/personal-website 32 33 #Spring boot视图配置 34 spring.mvc.view.prefix=/WEB-INF/ 35 spring.mvc.view.suffix=.jsp 36 #静态文件访问配置 37 spring.mvc.static-path-pattern=/static/*
application.yml文件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #公共配置与profiles选择无关 2 mybatis: 3 typeAliasesPackage: com.demo.entity.* 4 mapperLocations: classpath*:db.mappers/*.xml 5 6 7 #开发配置 8 spring: 9 profiles: dev 10 11 datasource: 12 url: jdbc:mysql://localhost:3306/grd?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT&tinyInt1isBit=false 13 username: root 14 password: SFgrd123$ 15 driver-class-name: com.mysql.jdbc.Driver 16 17 # 使用druid数据源 18 type: com.alibaba.druid.pool.DruidDataSource
generatorConfig.xml文件 - 生成器自定义配置
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 6 <generatorConfiguration> 7 <!--导入属性配置--> 8 <properties resource="generator.properties"></properties> 9 10 <!--指定特定数据库的jdbc驱动jar包的位置--> 11 <classPathEntry location="${db.driverLocation}"/> 12 13 <context id="default" targetRuntime="MyBatis3"> 14 <property name="javaFileEncoding" value="UTF-8"/> 15 16 <!-- 格式化java代码--> 17 <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> 18 19 <!-- 格式化XML代码--> 20 <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> 21 22 <!-- generate entity时,生成serialVersionUID --> 23 <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> 24 25 <!-- 这个插件只会增加字符串字段映射到一个JDBC字符的方法 --> 26 <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" /> 27 28 <!-- genenat entity时,生成toString --> 29 <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> 30 31 <plugin type="org.mybatis.generator.plugins.SqlMapConfigPlugin" /> 32 33 <!-- Mapper.java自动继承通用Mapper接口 --> 34 <plugin type="com.ch.generator.mybatis.TkMapperPluginExtend"> 35 <property name="mappers" value="${super.mappers}"/> 36 <!-- caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true --> 37 <property name="caseSensitive" value="true"/> 38 <!-- 生成对外的对应dto文件 --> 39 <!--<property name="dtoTargetProject" value="src/main/java"/> 40 <property name="dtoTargetPackage" value="${dto.package.path}"/>--> 41 </plugin> 42 43 <!-- optional,旨在创建class时,对注释进行控制 --> 44 <commentGenerator type="com.ch.generator.mybatis.MyCommentGenerator"> 45 <!--<property name="suppressAllComments" value="true"/>--> 46 <!--<property name="suppressJpa" value="true"/>--> 47 <!--<property name="suppressSwagger" value="false"/>--> 48 </commentGenerator> 49 50 <!--jdbc的数据库连接 --> 51 <jdbcConnection 52 driverClass="${db.driverClassName}" 53 connectionURL="${db.connectionURL}" 54 userId="${db.userName}" 55 password="${db.password}"> 56 </jdbcConnection> 57 58 59 <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制--> 60 <javaTypeResolver type="com.ch.generator.mybatis.MyJavaTypeResolver"> 61 <!--<property name="forceBigDecimals" value="true"/>--> 62 </javaTypeResolver> 63 64 <!-- 生成模型的包名和位置--> 65 <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类 66 targetPackage 指定生成的model生成所在的包名 67 targetProject 指定在该项目下所在的路径 68 --> 69 <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">--> 70 <javaModelGenerator targetPackage="${entity.package.path}" targetProject="./src/main/java"> 71 <!-- 是否允许子包,即targetPackage.schemaName.tableName --> 72 <property name="enableSubPackages" value="false"/> 73 <!-- 是否对model添加 构造函数 --> 74 <property name="constructorBased" value="true"/> 75 <!-- 是否对类CHAR类型的列的数据进行trim操作 (去空)--> 76 <property name="trimStrings" value="true"/> 77 <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 --> 78 <property name="immutable" value="false"/> 79 </javaModelGenerator> 80 81 <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 --> 82 <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">--> 83 <sqlMapGenerator targetPackage="${mapper.xml.package.path}" targetProject="./src/main/resources"> 84 <property name="enableSubPackages" value="false"/> 85 <property name="" value=""/> 86 </sqlMapGenerator> 87 88 <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 89 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 90 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 91 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 92 --> 93 94 <!-- targetPackage:mapper接口dao生成的位置 --> 95 <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">--> 96 <javaClientGenerator type="XMLMAPPER" targetPackage="${mapper.dao.package.path}" targetProject="./src/main/java"> 97 <!-- enableSubPackages:是否让schema作为包的后缀 --> 98 <property name="enableSubPackages" value="false" /> 99 </javaClientGenerator> 100 101 <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName="tm_user_outer"是实体类名 --> 102 <table tableName="tm_outer_user" schema="root" 103 enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" 104 enableSelectByExample="false" selectByExampleQueryId="false"> 105 106 <property name="useActualColumnNames" value="false"/> 107 <generatedKey column="ID" sqlStatement="Mysql" identity="true"/> 108 <!-- 数据库中该字段的类型是 txt ,不同版本生成对应字体类的属性类型可能不同,因此指定转换类型 --> 109 <columnOverride column="remark" jdbcType="LONGVARCHAR" /> 110 </table> 111 <!-- mybatis插件的搭建 --> 112 </context> 113 </generatorConfiguration>
generator.properties文件 - 数据源配置
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #数据库连接信息 2 db.driverClassName=com.mysql.jdbc.Driver 3 db.connectionURL=jdbc:mysql://localhost:3306/grd?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT&tinyInt1isBit=false 4 db.userName=root 5 db.password=SFgrd123$ 6 7 #驱动包 8 db.driverLocation=D:\\Maven\\repository\\mysql\\mysql-connector-java\\8.0.15\\mysql-connector-java-8.0.15.jar 9 super.mappers=tk.mybatis.mapper.common.Mapper 10 11 #generator自动生成代码文件路径配置 12 entity.package.path=com.demo.entity 13 dto.package.path=com.demo.dto 14 mapper.xml.package.path=db.mappers 15 mapper.dao.package.path=com.demo.dao
pom.xml文件 - maven依赖
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.3.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.example</groupId> 12 <artifactId>demo</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>demo</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.freemarker</groupId> 24 <artifactId>freemarker</artifactId> 25 <version>2.3.28</version> 26 </dependency> 27 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-data-jpa</artifactId> 31 </dependency> 32 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-web</artifactId> 36 </dependency> 37 <dependency> 38 <groupId>org.mybatis.spring.boot</groupId> 39 <artifactId>mybatis-spring-boot-starter</artifactId> 40 <version>2.0.0</version> 41 </dependency> 42 43 <!-- 连接池 --> 44 <dependency> 45 <groupId>com.alibaba</groupId> 46 <artifactId>druid</artifactId> 47 <version>1.1.0</version> 48 </dependency> 49 50 <dependency> 51 <groupId>mysql</groupId> 52 <artifactId>mysql-connector-java</artifactId> 53 <scope>8.0.15</scope> 54 </dependency> 55 56 <dependency> 57 <groupId>org.springframework.boot</groupId> 58 <artifactId>spring-boot-starter-test</artifactId> 59 <scope>test</scope> 60 </dependency> 61 62 <dependency> 63 <groupId>org.mybatis.generator</groupId> 64 <artifactId>mybatis-generator-core</artifactId> 65 <version>1.3.5</version> 66 </dependency> 67 68 <dependency> 69 <groupId>org.apache.commons</groupId> 70 <artifactId>commons-lang3</artifactId> 71 </dependency> 72 73 <dependency> 74 <groupId>org.projectlombok</groupId> 75 <artifactId>lombok</artifactId> 76 <version>1.16.18</version> 77 </dependency> 78 79 <!-- -springboot 整合Web组件 整合SpringMVC 就会把传统方式的SpringMVC依赖的jar全部给下载来 --> 80 <!-- 引入spring-boot-starter-web 帮你整合好所有相关的依赖jar包 原理 maven依赖传递 --> 81 82 <!-- 原理: spring-boot-starter-parent< 中,整合号相关 jar依赖信息 --> 83 <dependency> 84 <groupId>org.springframework.boot</groupId> 85 <artifactId>spring-boot-starter-web</artifactId> 86 </dependency> 87 <dependency> 88 <groupId>org.springframework.boot</groupId> 89 <artifactId>spring-boot-starter-thymeleaf</artifactId> 90 </dependency> 91 92 <dependency> 93 <groupId>tk.mybatis</groupId> 94 <artifactId>mapper</artifactId> 95 <version>4.0.2</version> 96 </dependency> 97 98 <dependency> 99 <groupId>com.ch</groupId> 100 <artifactId>ch-mybatis-generator</artifactId> 101 <version>1.0.2-SNAPSHOT</version> 102 </dependency> 103 104 <dependency> 105 <groupId>tk.mybatis</groupId> 106 <artifactId>mapper-spring</artifactId> 107 <version>1.0.0</version> 108 </dependency> 109 110 <!-- 单元测试 --> 111 <dependency> 112 <groupId>junit</groupId> 113 <artifactId>junit</artifactId> 114 <version>${junit.version}</version> 115 <scope>test</scope> 116 </dependency> 117 <!-- 日志处理 --> 118 <dependency> 119 <groupId>org.slf4j</groupId> 120 <artifactId>slf4j-log4j12</artifactId> 121 <version>${slf4j.version}</version> 122 </dependency> 123 <!-- 文件上传组件 --> 124 <dependency> 125 <groupId>commons-fileupload</groupId> 126 <artifactId>commons-fileupload</artifactId> 127 <version>1.4</version> 128 </dependency> 129 <!-- Redis客户端 --> 130 <dependency> 131 <groupId>redis.clients</groupId> 132 <artifactId>jedis</artifactId> 133 <version>${jedis.version}</version> 134 </dependency> 135 <!-- solr客户端 --> 136 <dependency> 137 <groupId>org.apache.solr</groupId> 138 <artifactId>solr-solrj</artifactId> 139 <version>7.5.0</version> 140 </dependency> 141 142 <!-- 使用json序列化 --> 143 <dependency> 144 <groupId>org.jboss.resteasy</groupId> 145 <artifactId>resteasy-jackson-provider</artifactId> 146 </dependency> 147 <!-- 使用xml序列化 --> 148 <dependency> 149 <groupId>org.jboss.resteasy</groupId> 150 <artifactId>resteasy-jaxb-provider</artifactId> 151 </dependency> 152 <dependency> 153 <groupId>org.json</groupId> 154 <artifactId>json</artifactId> 155 <version>20180813</version> 156 </dependency> 157 <dependency> 158 <groupId>com.google.collect</groupId> 159 <artifactId>com.springsource.com.google.common.collect</artifactId> 160 <version>0.8.0.20080820</version> 161 </dependency> 162 </dependencies> 163 164 <build> 165 <resources> 166 <resource> 167 <directory>src/main/java</directory> 168 <includes> 169 <include>**/*.properties</include> 170 <include>**/*.xml</include> 171 </includes> 172 <filtering>false</filtering> 173 </resource> 174 <resource> 175 <directory>src/main/resources</directory> 176 <includes> 177 <include>**/*.properties</include> 178 <include>**/mappers/*.xml</include> 179 </includes> 180 <filtering>false</filtering> 181 </resource> 182 </resources> 183 <plugins> 184 <plugin> 185 <groupId>org.springframework.boot</groupId> 186 <artifactId>spring-boot-maven-plugin</artifactId> 187 </plugin> 188 <plugin> 189 <groupId>org.mybatis.generator</groupId> 190 <artifactId>mybatis-generator-maven-plugin</artifactId> 191 <version>1.3.2</version> 192 <configuration> 193 <configurationFile> 194 <!-- 该路径就是我们创建generatorConfig.xml文件的全路径,千万别配错了 --> 195 src\main\resources\generatorConfig.xml 196 </configurationFile> 197 <verbose>true</verbose> 198 <overwrite>true</overwrite> 199 </configuration> 200 <dependencies> 201 <dependency> 202 <groupId>com.ch</groupId> 203 <artifactId>ch-mybatis-generator</artifactId> 204 <version>1.0.2-SNAPSHOT</version> 205 </dependency> 206 <dependency> 207 <groupId>tk.mybatis</groupId> 208 <artifactId>mapper</artifactId> 209 <version>4.0.2</version> 210 </dependency> 211 </dependencies> 212 </plugin> 213 </plugins> 214 </build> 215 216 </project>
log4j.properties文件 - 日志配置
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 log4j.rootLogger=info,fileAppender,consoleAppender 2 3 #打印日志到控制台,如需要此功能,在第一行末尾加上内容:,consoleAppender 4 log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender 5 log4j.appender.consoleAppender.Target = System.out 6 log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout 7 log4j.appender.consoleAppender.layout.ConversionPattern = %d %p %c %L %M - %m%x%n 8 9 #打印系统日志到文件 10 log4j.appender.fileAppender=org.apache.log4j.DailyRollingFileAppender 11 log4j.appender.fileAppender.File=../logs/pats-client/pats-client.log 12 log4j.appender.fileAppender.DatePattern = '_'yyyyMMdd 13 log4j.appender.fileAppender.layout = org.apache.log4j.PatternLayout 14 log4j.appender.fileAppender.layout.ConversionPattern = %d %p %c %L %M - %m%x%n 15 16 #source日志 17 log4j.logger.com.pats.client.log.datalog.SourceDataLog=INFO,sourceDataLog 18 log4j.appender.sourceDataLog=org.apache.log4j.DailyRollingFileAppender 19 log4j.appender.sourceDataLog.File=../logs/pats-client/source_data.log 20 log4j.appender.sourceDataLog.DatePattern = '_'yyyyMMdd 21 log4j.appender.sourceDataLog.layout = org.apache.log4j.PatternLayout 22 log4j.appender.sourceDataLog.layout.ConversionPattern = [%d]%m%x%n 23 log4j.additivity.com.pats.client.log.datalog.SourceDataLog = false 24 25 #websocket数据日志 26 log4j.logger.com.pats.client.log.AskPriceModelDataLog = DEBUG,AskPriceDataLog 27 log4j.appender.AskPriceDataLog=org.apache.log4j.DailyRollingFileAppender 28 log4j.appender.AskPriceDataLog.File=../logs/pats-client/askPrice_data.log 29 log4j.appender.AskPriceDataLog.DatePattern = '_'yyyyMMdd 30 log4j.appender.AskPriceDataLog.layout=org.apache.log4j.PatternLayout 31 log4j.appender.AskPriceDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n 32 log4j.additivity.com.pats.client.log.AskPriceModelDataLog = false 33 34 35 #websocket数据日志 36 log4j.logger.com.pats.client.log.BidPriceModelDataLog = DEBUG,BidPriceDataLog 37 log4j.appender.BidPriceDataLog=org.apache.log4j.DailyRollingFileAppender 38 log4j.appender.BidPriceDataLog.File=../logs/pats-client/bidPrice_data.log 39 log4j.appender.BidPriceDataLog.DatePattern = '_'yyyyMMdd 40 log4j.appender.BidPriceDataLog.layout=org.apache.log4j.PatternLayout 41 log4j.appender.BidPriceDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n 42 log4j.additivity.com.pats.client.log.BidPriceModelDataLog = false 43 44 #websocket数据日志 45 log4j.logger.com.pats.client.log.DealLogModelDataLog = DEBUG,DealLogDataLog 46 log4j.appender.DealLogDataLog=org.apache.log4j.DailyRollingFileAppender 47 log4j.appender.DealLogDataLog.File=../logs/pats-client/dealLog_data.log 48 log4j.appender.DealLogDataLog.DatePattern = '_'yyyyMMdd 49 log4j.appender.DealLogDataLog.layout=org.apache.log4j.PatternLayout 50 log4j.appender.DealLogDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n 51 log4j.additivity.com.pats.client.log.DealLogModelDataLog = false 52 53 #websocket数据日志 54 log4j.logger.com.pats.client.log.FullModelDataLog = DEBUG,FullDataLog 55 log4j.appender.FullDataLog=org.apache.log4j.DailyRollingFileAppender 56 log4j.appender.FullDataLog.File=../logs/pats-client/full_data.log 57 log4j.appender.FullDataLog.DatePattern = '_'yyyyMMdd 58 log4j.appender.FullDataLog.layout=org.apache.log4j.PatternLayout 59 log4j.appender.FullDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n 60 log4j.additivity.com.pats.client.log.FullModelDataLog = false 61 62 #websocket数据日志 63 log4j.logger.com.pats.client.log.PositionModelDataLog = DEBUG,PositionDataLog 64 log4j.appender.PositionDataLog=org.apache.log4j.DailyRollingFileAppender 65 log4j.appender.PositionDataLog.File=../logs/pats-client/position_data.log 66 log4j.appender.PositionDataLog.DatePattern = '_'yyyyMMdd 67 log4j.appender.PositionDataLog.layout=org.apache.log4j.PatternLayout 68 log4j.appender.PositionDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n 69 log4j.additivity.com.pats.client.log.PositionModelDataLog = false 70 71 #websocket数据日志 72 log4j.logger.com.pats.client.log.VwapModelDataLog = DEBUG,VwapDataLog 73 log4j.appender.VwapDataLog=org.apache.log4j.DailyRollingFileAppender 74 log4j.appender.VwapDataLog.File=../logs/pats-client/vwap_data.log 75 log4j.appender.VwapDataLog.DatePattern = '_'yyyyMMdd 76 log4j.appender.VwapDataLog.layout=org.apache.log4j.PatternLayout 77 log4j.appender.VwapDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n 78 log4j.additivity.com.pats.client.log.VwapModelDataLog = false 79 80 #websocket数据日志 81 log4j.logger.com.pats.client.log.ProductDataLog = DEBUG,ProductDataLog 82 log4j.appender.ProductDataLog=org.apache.log4j.DailyRollingFileAppender 83 log4j.appender.ProductDataLog.File=../logs/pats-client/product_data.log 84 log4j.appender.ProductDataLog.DatePattern = '_'yyyyMMdd 85 log4j.appender.ProductDataLog.layout=org.apache.log4j.PatternLayout 86 log4j.appender.ProductDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n 87 log4j.additivity.com.pats.client.log.ProductDataLog = false 88 89 #websocket数据日志 90 log4j.logger.com.pats.client.log.ProductErrorDataLog = DEBUG,ProductErrorDataLog 91 log4j.appender.ProductErrorDataLog=org.apache.log4j.DailyRollingFileAppender 92 log4j.appender.ProductErrorDataLog.File=../logs/pats-client/productError_data.log 93 log4j.appender.ProductErrorDataLog.DatePattern = '_'yyyyMMdd 94 log4j.appender.ProductErrorDataLog.layout=org.apache.log4j.PatternLayout 95 log4j.appender.ProductErrorDataLog.layout.ConversionPattern=%d %p %c %L %M - %m%x%n 96 log4j.additivity.com.pats.client.log.ProductErrorDataLog = false 97 98 #jdbc日志 99 #log4j.logger.org.springframework.jdbc=debug 100 #log4j.logger.org.mybatis.spring=debug 101 #log4j.logger.com.pats.client.dao=debug
单元测试
DemoApplicationTests.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.example.demo; 2 3 import org.junit.After; 4 import org.junit.Assert; 5 import org.junit.Before; 6 import org.junit.Test; 7 import org.junit.runner.RunWith; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.boot.test.context.SpringBootTest; 10 import org.springframework.test.context.junit4.SpringRunner; 11 import org.springframework.test.context.web.WebAppConfiguration; 12 import org.springframework.test.web.servlet.MockMvc; 13 import org.springframework.test.web.servlet.MvcResult; 14 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 15 import org.springframework.test.web.servlet.setup.MockMvcBuilders; 16 import org.springframework.web.context.WebApplicationContext; 17 18 /** 19 * 通用单元测试类 20 */ 21 @RunWith(SpringRunner.class) 22 @SpringBootTest 23 //由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。 24 @WebAppConfiguration 25 public class DemoApplicationTests { 26 27 @Test 28 public void testDemo(){ 29 String tr = "512Y-T-20181114124429854843"; 30 System.out.println(tr.indexOf("-X-",1)); 31 } 32 33 /** 34 * 模拟mvc测试对象 35 */ 36 private MockMvc mockMvc; 37 38 public MockMvc getMockMvc() { 39 return mockMvc; 40 } 41 42 /** 43 * web项目上下文 44 */ 45 @Autowired 46 private WebApplicationContext webApplicationContext; 47 48 /** 49 * 所有测试方法执行之前执行该方法 50 */ 51 @Before 52 public void before() { 53 System.out.println("---&&&&&---test ---%%%%%---*****--- start ---######---" + "\n"); 54 //获取mockmvc对象实例 55 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); 56 } 57 58 @After 59 public void after() { 60 System.out.println("\n" + "---&&&&&--- test ---%%%%%---*****--- end ----######---"); 61 } 62 }