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=1010241024(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 8、generatorconfig.xml 9、web.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>
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>
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>/
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
上一篇: 第一个博客_测试用
推荐阅读
-
干货:.net core实现读取自定义配置文件,有源代码哦
-
数组和字符串的方法整合
-
干货:.net core实现读取appsettings.json配置文件(建议收藏)
-
SpingBoot:整合Elasticsearch7.2.0
-
让 babel webpack vue 配置文件支持智能提示的方法
-
使用Yii整合的pjax(pushstate+ajax)实现无刷新加载页面
-
详解vue2.0脚手架的webpack 配置文件分析
-
SpringBoot整合dubbo(yml格式配置)
-
SpringBoot2 整合 Zookeeper组件,管理架构中服务协调
-
Spring Boot2.X整合消息中间件RabbitMQ原理简浅探析