SSM整合-配置文件
使用工具:maven、idea、jdk8、mysql、tomcat9.0
初学ssm框架,配置文件的配置目录:
其中generator.xml是用Mybatis从数据库中表逆向生成pojo、dao、mapper下此表的相关信息。
配置文件如下:
1、pom.xml
1 <properties> 2 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 3 <maven.compiler.source>1.8</maven.compiler.source> 4 <maven.compiler.target>1.8</maven.compiler.target> 5 6 <!--srping版本--> 7 <spring.version>4.3.14.RELEASE</spring.version> 8 <!--mybatis版本--> 9 <mybatis.version>3.4.0</mybatis.version> 10 <!-- log4j日志文件管理包版本 --> 11 <slf4j.version>1.6.6</slf4j.version> 12 <log4j.version>1.2.12</log4j.version> 13 </properties> 14 15 16 <dependencies> 17 18 <!-- 导入Mysql数据库链接jar包 --> 19 <dependency> 20 <groupId>mysql</groupId> 21 <artifactId>mysql-connector-java</artifactId> 22 <version>5.1.6</version> 23 </dependency> 24 25 26 <!-- https://mvnrepository.com/artifact/junit/junit --> 27 <dependency> 28 <groupId>junit</groupId> 29 <artifactId>junit</artifactId> 30 <version>4.12</version> 31 <scope>test</scope> 32 </dependency> 33 34 35 <!-- mybatis核心包 --> 36 <dependency> 37 <groupId>org.mybatis</groupId> 38 <artifactId>mybatis</artifactId> 39 <version>${mybatis.version}</version> 40 </dependency> 41 <!-- mybatis/spring包 --> 42 <dependency> 43 <groupId>org.mybatis</groupId> 44 <artifactId>mybatis-spring</artifactId> 45 <version>1.3.0</version> 46 </dependency> 47 48 49 <!--spring单元测试依赖 --> 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-test</artifactId> 53 <version>${spring.version}</version> 54 <scope>test</scope> 55 </dependency> 56 57 <!-- springMVC核心包 --> 58 <dependency> 59 <groupId>org.springframework</groupId> 60 <artifactId>spring-webmvc</artifactId> 61 <version>${spring.version}</version> 62 </dependency> 63 64 <!-- spring核心包 --> 65 <dependency> 66 <groupId>org.springframework</groupId> 67 <artifactId>spring-core</artifactId> 68 <version>${spring.version}</version> 69 </dependency> 70 71 <dependency> 72 <groupId>org.springframework</groupId> 73 <artifactId>spring-beans</artifactId> 74 <version>${spring.version}</version> 75 </dependency> 76 77 <dependency> 78 <groupId>org.springframework</groupId> 79 <artifactId>spring-context</artifactId> 80 <version>${spring.version}</version> 81 </dependency> 82 83 <dependency> 84 <groupId>org.springframework</groupId> 85 <artifactId>spring-context-support</artifactId> 86 <version>${spring.version}</version> 87 </dependency> 88 89 <dependency> 90 <groupId>org.springframework</groupId> 91 <artifactId>spring-tx</artifactId> 92 <version>${spring.version}</version> 93 </dependency> 94 95 <dependency> 96 <groupId>org.springframework</groupId> 97 <artifactId>spring-web</artifactId> 98 <version>${spring.version}</version> 99 </dependency> 100 101 <dependency> 102 <groupId>org.springframework</groupId> 103 <artifactId>spring-jdbc</artifactId> 104 <version>${spring.version}</version> 105 </dependency> 106 107 <dependency> 108 <groupId>javax.servlet</groupId> 109 <artifactId>javax.servlet-api</artifactId> 110 <version>3.0.1</version> 111 <scope>provided</scope> 112 </dependency> 113 114 <dependency> 115 <groupId>javax.servlet.jsp</groupId> 116 <artifactId>jsp-api</artifactId> 117 <version>2.2</version> 118 <scope>provided</scope> 119 </dependency> 120 <!--mysql数据库链接包--> 121 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 --> 122 <dependency> 123 <groupId>org.apache.commons</groupId> 124 <artifactId>commons-dbcp2</artifactId> 125 <version>2.1.1</version> 126 </dependency> 127 128 <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib--> 129 <!--<dependency> 130 <groupId>net.sf.json-lib</groupId> 131 <artifactId>json-lib</artifactId> 132 <version>2.4</version> 133 </dependency>--> 134 135 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> 136 <dependency> 137 <groupId>com.fasterxml.jackson.core</groupId> 138 <artifactId>jackson-databind</artifactId> 139 <version>2.9.8</version> 140 </dependency> 141 142 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> 143 <dependency> 144 <groupId>com.fasterxml.jackson.core</groupId> 145 <artifactId>jackson-core</artifactId> 146 <version>2.9.8</version> 147 </dependency> 148 149 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> 150 <dependency> 151 <groupId>com.fasterxml.jackson.core</groupId> 152 <artifactId>jackson-annotations</artifactId> 153 <version>2.9.8</version> 154 </dependency> 155 156 <!--JSR303数据校验支持,支持tomcat7及以上版本--> 157 <!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator --> 158 <dependency> 159 <groupId>org.hibernate.validator</groupId> 160 <artifactId>hibernate-validator</artifactId> 161 <version>6.0.16.Final</version> 162 </dependency> 163 164 165 <!-- JSTL标签类 --> 166 <dependency> 167 <groupId>jstl</groupId> 168 <artifactId>jstl</artifactId> 169 <version>1.2</version> 170 </dependency> 171 <!-- 日志文件管理包 --> 172 <dependency> 173 <groupId>log4j</groupId> 174 <artifactId>log4j</artifactId> 175 <version>1.2.17</version> 176 </dependency> 177 178 <!-- 上传组件包 --> 179 <dependency> 180 <groupId>commons-fileupload</groupId> 181 <artifactId>commons-fileupload</artifactId> 182 <version>1.3.1</version> 183 </dependency> 184 <dependency> 185 <groupId>commons-io</groupId> 186 <artifactId>commons-io</artifactId> 187 <version>2.4</version> 188 </dependency> 189 <dependency> 190 <groupId>commons-codec</groupId> 191 <artifactId>commons-codec</artifactId> 192 <version>1.9</version> 193 </dependency> 194 195 <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> 196 <dependency> 197 <groupId>com.github.pagehelper</groupId> 198 <artifactId>pagehelper</artifactId> 199 <version>5.1.10</version> 200 </dependency> 201 202 203 <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> 204 <dependency> 205 <groupId>org.mybatis.generator</groupId> 206 <artifactId>mybatis-generator-core</artifactId> 207 <version>1.3.7</version> 208 </dependency> 209 <!-- <dependency> 210 <groupId>org.junit.jupiter</groupId> 211 <artifactId>junit-jupiter-api</artifactId> 212 <version>RELEASE</version> 213 <scope>compile</scope> 214 </dependency>--> 215 216 </dependencies> 217 218 <build> 219 <finalName>ssm-crud</finalName> 220 <plugins> 221 <plugin> 222 <groupId>org.mybatis.generator</groupId> 223 <artifactId>mybatis-generator-maven-plugin</artifactId> 224 <version>1.3.7</version> 225 </plugin> 226 </plugins> 227 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 228 <plugins> 229 <plugin> 230 <artifactId>maven-clean-plugin</artifactId> 231 <version>3.1.0</version> 232 </plugin> 233 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> 234 <plugin> 235 <artifactId>maven-resources-plugin</artifactId> 236 <version>3.0.2</version> 237 </plugin> 238 <plugin> 239 <artifactId>maven-compiler-plugin</artifactId> 240 <version>3.8.0</version> 241 </plugin> 242 <plugin> 243 <artifactId>maven-surefire-plugin</artifactId> 244 <version>2.22.1</version> 245 </plugin> 246 <plugin> 247 <artifactId>maven-war-plugin</artifactId> 248 <version>3.2.2</version> 249 </plugin> 250 <plugin> 251 <artifactId>maven-install-plugin</artifactId> 252 <version>2.5.2</version> 253 </plugin> 254 <plugin> 255 <artifactId>maven-deploy-plugin</artifactId> 256 <version>2.8.2</version> 257 </plugin> 258 </plugins> 259 </pluginManagement> 260 </build> 261 </project>
2、db.properties
1 db.driverClassName=com.mysql.jdbc.Driver 2 db.url=jdbc:mysql://localhost:3306/ssm_crud?useUnicode=true&characterEncoding=utf-8 3 db.username=root 4 db.password=123456 5 #定义初始连接数NewsManagement 6 db.initialSize=0 7 #定义最大连接数 8 db.maxActive=20 9 #定义最长等待时间 10 db.maxWait=60000
3、log4j.properties
1 # Global logging configuration 2 log4j.rootLogger=DEBUG, stdout 3 # Console output... 4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
4、applicationContext.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:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 8 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 12 <!-- 扫描service包,让service的注解起作用 --> 13 <context:component-scan base-package="com.xwh"/> 14 <!-- 加载db.propertites --> 15 <context:property-placeholder location="classpath:db.properties"/> 16 <bean id="dataSource" 17 class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> 18 <property name="driverClassName" value="${db.driverClassName}"/> 19 <property name="url" value="${db.url}"/> 20 <property name="username" value="${db.username}"/> 21 <property name="password" value="${db.password}"/> 22 <property name="initialSize" value="${db.initialSize}"/> 23 <property name="maxWaitMillis" value="${db.maxWait}"/> 24 <property name="maxTotal" value="${db.maxActive}"/> 25 </bean> 26 <bean id="sqlSessionFactory" 27 class="org.mybatis.spring.SqlSessionFactoryBean"> 28 <property name="dataSource" ref="dataSource"></property> 29 <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> 30 <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"></property> 31 </bean> 32 <!--配置一个可以执行批量的sqlSession--> 33 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> 34 <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> 35 <constructor-arg name="executorType" value="BATCH"></constructor-arg> 36 </bean> 37 <!-- mapper扫描器 --> 38 <!-- 会自动生成一个标识为mapper接口类型首字母小写的bean --> 39 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 40 <property name="basePackage" value="com.xwh.dao"></property> 41 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 42 </bean> 43 <!--事务控制的配置--> 44 <bean id="txManager" 45 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 46 <property name="dataSource" ref="dataSource"></property> 47 </bean> 48 <tx:annotation-driven transaction-manager="txManager"/> 49 </beans> 50
5、springmvc.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:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.1.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 13 14 <context:component-scan base-package="com.xwh.controller" /> 15 16 <mvc:annotation-driven /> 17 18 <!-- 处理请求转发 --> 19 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 20 <property name="prefix" value="/WEB-INF/views/" /> 21 <property name="suffix" value=".jsp" /> 22 </bean> 23 24 <!--将springmvc不能处理的请求交给tomcat--> 25 <mvc:default-servlet-handler/> 26 27 <!-- 对静态资源放行 --> 28 <!-- 映射到根目录下,文件引用静态资源时就加上static.. --> 29 <mvc:resources mapping="/static/**" location="static/" /> 30 31 32 <!-- 上传文件拦截,设置最大上传文件大小 10M=10*1024*1024(B)=10485760 bytes --> 33 <bean id="multipartResolver" 34 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 35 <!-- 最大文件大小限制 --> 36 <property name="maxUploadSize" value="10485760" /> 37 <property name="defaultEncoding" value="UTF-8" /> 38 </bean> 39 40 </beans>
6、mybatis-config.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 6 <configuration> 7 8 <settings> 9 <setting name="logImpl" value="LOG4J"/> 10 <!-- 使用列标签代替列名 --> 11 <!--<setting name="useColumnLabel" value="false"/>--> 12 <!--允许 JDBC 支持自动生成主键--> 13 <setting name="useGeneratedKeys" value="false"/> 14 <!--是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射。 --> 15 <setting name="mapUnderscoreToCamelCase" value="true"/> 16 </settings> 17 18 <typeAliases> 19 <package name="com.xwh.entity"/> 20 </typeAliases> 21 <!-- 数据源的配置交给spring --> 22 <!-- 关联映射文件交给spring的mapper扫描器 --> 23 24 25 <!-- 配置mybatis分页插件PageHelper --> 26 <plugins> 27 <plugin interceptor="com.github.pagehelper.PageInterceptor"> 28 <!--分页参数合理化--> 29 <property name="reasonable" value="true"/> 30 </plugin> 31 </plugins> 32 </configuration>
7、*Mapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="com.xwh.dao.EmployeeMapper"> 4 5 </mapper>
8、generatorConfig.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 6 <generatorConfiguration> 7 <classPathEntry location="E:\reposi\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar" /> 8 9 <context id="DB2Tables" targetRuntime="MyBatis3"> 10 <!-- 去掉注释--> 11 <commentGenerator> 12 <property name="suppressDate" value="true"/> 13 <property name="suppressAllComments" value="true" /> 14 </commentGenerator> 15 16 <jdbcConnection driverClass="com.mysql.jdbc.Driver" 17 connectionURL="jdbc:mysql://localhost:3306/ssm_crud" 18 userId="root" 19 password="123456"> 20 </jdbcConnection> 21 22 <javaTypeResolver > 23 <property name="forceBigDecimals" value="false" /> 24 </javaTypeResolver> 25 26 <javaModelGenerator targetPackage="com.xwh.entity" targetProject="src\main\java"> 27 <property name="enableSubPackages" value="true" /> 28 <property name="trimStrings" value="true" /> 29 </javaModelGenerator> 30 31 <sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources\mybatis"> 32 <property name="enableSubPackages" value="true" /> 33 </sqlMapGenerator> 34 35 <javaClientGenerator type="XMLMAPPER" targetPackage="com.xwh.dao" targetProject="src\main\java"> 36 <property name="enableSubPackages" value="true" /> 37 </javaClientGenerator> 38 39 <table tableName="employee" domainObjectName="Employee" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" 40 enableSelectByExample="false" selectByExampleQueryId="false"> 41 </table> 42 43 <table tableName="department" domainObjectName="Department" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" 44 enableSelectByExample="false" selectByExampleQueryId="false"> 45 </table> 46 </context> 47 </generatorConfiguration>
9、web.xml
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 <web-app version="2.4" 5 xmlns="http://java.sun.com/xml/ns/j2ee" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 8 9 <display-name>ssm-crud</display-name> 10 11 <welcome-file-list> 12 <welcome-file>index2.jsp</welcome-file> 13 </welcome-file-list> 14 <!-- 加载spring容器配置 --> 15 <listener> 16 <listener-class> 17 org.springframework.web.context.ContextLoaderListener 18 </listener-class> 19 </listener> 20 21 <!-- 设置Spring容器加载所有的配置文件的路径 --> 22 <context-param> 23 <param-name>contextConfigLocation</param-name> 24 <param-value>classpath:spring/applicationContext.xml</param-value> 25 </context-param> 26 27 <context-param> 28 <param-name>log4jConfigLocation</param-name> 29 <param-value>classpath:log4j.properties</param-value> 30 </context-param> 31 32 <!-- 配置springMVC核心控制器 --> 33 <servlet> 34 <servlet-name>dispatcherServlet</servlet-name> 35 <servlet-class> 36 org.springframework.web.servlet.DispatcherServlet 37 </servlet-class> 38 <init-param> 39 <param-name>contextConfigLocation</param-name> 40 <param-value>classpath:springmvc/springmvc.xml</param-value> 41 </init-param> 42 <load-on-startup>1</load-on-startup> 43 </servlet> 44 <servlet-mapping> 45 <servlet-name>dispatcherServlet</servlet-name> 46 <url-pattern>/</url-pattern> 47 </servlet-mapping> 48 49 <!-- 解决中文乱码 --> 50 <filter> 51 <filter-name>encodingFilter</filter-name> 52 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 53 <init-param> 54 <param-name>encoding</param-name> 55 <param-value>utf-8</param-value> 56 </init-param> 57 <init-param> 58 <param-name>forceRequestEncoding</param-name> 59 <param-value>true</param-value> 60 </init-param> 61 <init-param> 62 <param-name>forceResponseEncoding</param-name> 63 <param-value>true</param-value> 64 </init-param> 65 </filter> 66 <filter-mapping> 67 <filter-name>encodingFilter</filter-name> 68 <url-pattern>/*</url-pattern> 69 </filter-mapping> 70 71 <!--使用Rest风格的URI--> 72 <!--将页面普通的post请求转为指定的delete或者put请求--> 73 <filter> 74 <filter-name>HiddenHttpMethodFilter</filter-name> 75 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 76 </filter> 77 <filter-mapping> 78 <filter-name>HiddenHttpMethodFilter</filter-name> 79 <url-pattern>/*</url-pattern> 80 </filter-mapping> 81 82 </web-app> 83 84 85
如有不对之处,望多多指教!