使用工具:maven、idea、jdk8、mysql、tomcat9.0
初学ssm框架,配置文件的配置目录:
其中generator.xml是用Mybatis从数据库中表逆向生成pojo、dao、mapper下此表的相关信息。
配置文件如下:
1、pom.xml
1 <properties>
2 <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
3 <maven.compiler.source>1.8maven.compiler.source>
4 <maven.compiler.target>1.8maven.compiler.target>
5
6
7 <spring.version>4.3.14.RELEASEspring.version>
8
9 <mybatis.version>3.4.0mybatis.version>
10
11 <slf4j.version>1.6.6slf4j.version>
12 <log4j.version>1.2.12log4j.version>
13 properties>
14
15
16 <dependencies>
17
18
19 <dependency>
20 <groupId>mysqlgroupId>
21 <artifactId>mysql-connector-javaartifactId>
22 <version>5.1.6version>
23 dependency>
24
25
26
27 <dependency>
28 <groupId>junitgroupId>
29 <artifactId>junitartifactId>
30 <version>4.12version>
31 <scope>testscope>
32 dependency>
33
34
35
36 <dependency>
37 <groupId>org.mybatisgroupId>
38 <artifactId>mybatisartifactId>
39 <version>${mybatis.version}version>
40 dependency>
41
42 <dependency>
43 <groupId>org.mybatisgroupId>
44 <artifactId>mybatis-springartifactId>
45 <version>1.3.0version>
46 dependency>
47
48
49
50 <dependency>
51 <groupId>org.springframeworkgroupId>
52 <artifactId>spring-testartifactId>
53 <version>${spring.version}version>
54 <scope>testscope>
55 dependency>
56
57
58 <dependency>
59 <groupId>org.springframeworkgroupId>
60 <artifactId>spring-webmvcartifactId>
61 <version>${spring.version}version>
62 dependency>
63
64
65 <dependency>
66 <groupId>org.springframeworkgroupId>
67 <artifactId>spring-coreartifactId>
68 <version>${spring.version}version>
69 dependency>
70
71 <dependency>
72 <groupId>org.springframeworkgroupId>
73 <artifactId>spring-beansartifactId>
74 <version>${spring.version}version>
75 dependency>
76
77 <dependency>
78 <groupId>org.springframeworkgroupId>
79 <artifactId>spring-contextartifactId>
80 <version>${spring.version}version>
81 dependency>
82
83 <dependency>
84 <groupId>org.springframeworkgroupId>
85 <artifactId>spring-context-supportartifactId>
86 <version>${spring.version}version>
87 dependency>
88
89 <dependency>
90 <groupId>org.springframeworkgroupId>
91 <artifactId>spring-txartifactId>
92 <version>${spring.version}version>
93 dependency>
94
95 <dependency>
96 <groupId>org.springframeworkgroupId>
97 <artifactId>spring-webartifactId>
98 <version>${spring.version}version>
99 dependency>
100
101 <dependency>
102 <groupId>org.springframeworkgroupId>
103 <artifactId>spring-jdbcartifactId>
104 <version>${spring.version}version>
105 dependency>
106
107 <dependency>
108 <groupId>javax.servletgroupId>
109 <artifactId>javax.servlet-apiartifactId>
110 <version>3.0.1version>
111 <scope>providedscope>
112 dependency>
113
114 <dependency>
115 <groupId>javax.servlet.jspgroupId>
116 <artifactId>jsp-apiartifactId>
117 <version>2.2version>
118 <scope>providedscope>
119 dependency>
120
121
122 <dependency>
123 <groupId>org.apache.commonsgroupId>
124 <artifactId>commons-dbcp2artifactId>
125 <version>2.1.1version>
126 dependency>
127
128 ;
129
134
135
136 <dependency>
137 <groupId>com.fasterxml.jackson.coregroupId>
138 <artifactId>jackson-databindartifactId>
139 <version>2.9.8version>
140 dependency>
141
142
143 <dependency>
144 <groupId>com.fasterxml.jackson.coregroupId>
145 <artifactId>jackson-coreartifactId>
146 <version>2.9.8version>
147 dependency>
148
149
150 <dependency>
151 <groupId>com.fasterxml.jackson.coregroupId>
152 <artifactId>jackson-annotationsartifactId>
153 <version>2.9.8version>
154 dependency>
155
156
157
158 <dependency>
159 <groupId>org.hibernate.validatorgroupId>
160 <artifactId>hibernate-validatorartifactId>
161 <version>6.0.16.Finalversion>
162 dependency>
163
164
165
166 <dependency>
167 <groupId>jstlgroupId>
168 <artifactId>jstlartifactId>
169 <version>1.2version>
170 dependency>
171
172 <dependency>
173 <groupId>log4jgroupId>
174 <artifactId>log4jartifactId>
175 <version>1.2.17version>
176 dependency>
177
178
179 <dependency>
180 <groupId>commons-fileuploadgroupId>
181 <artifactId>commons-fileuploadartifactId>
182 <version>1.3.1version>
183 dependency>
184 <dependency>
185 <groupId>commons-iogroupId>
186 <artifactId>commons-ioartifactId>
187 <version>2.4version>
188 dependency>
189 <dependency>
190 <groupId>commons-codecgroupId>
191 <artifactId>commons-codecartifactId>
192 <version>1.9version>
193 dependency>
194
195
196 <dependency>
197 <groupId>com.github.pagehelpergroupId>
198 <artifactId>pagehelperartifactId>
199 <version>5.1.10version>
200 dependency>
201
202
203
204 <dependency>
205 <groupId>org.mybatis.generatorgroupId>
206 <artifactId>mybatis-generator-coreartifactId>
207 <version>1.3.7version>
208 dependency>
209
215
216 dependencies>
217
218 <build>
219 <finalName>ssm-crudfinalName>
220 <plugins>
221 <plugin>
222 <groupId>org.mybatis.generatorgroupId>
223 <artifactId>mybatis-generator-maven-pluginartifactId>
224 <version>1.3.7version>
225 plugin>
226 plugins>
227 <pluginManagement>
228 <plugins>
229 <plugin>
230 <artifactId>maven-clean-pluginartifactId>
231 <version>3.1.0version>
232 plugin>
233
234 <plugin>
235 <artifactId>maven-resources-pluginartifactId>
236 <version>3.0.2version>
237 plugin>
238 <plugin>
239 <artifactId>maven-compiler-pluginartifactId>
240 <version>3.8.0version>
241 plugin>
242 <plugin>
243 <artifactId>maven-surefire-pluginartifactId>
244 <version>2.22.1version>
245 plugin>
246 <plugin>
247 <artifactId>maven-war-pluginartifactId>
248 <version>3.2.2version>
249 plugin>
250 <plugin>
251 <artifactId>maven-install-pluginartifactId>
252 <version>2.5.2version>
253 plugin>
254 <plugin>
255 <artifactId>maven-deploy-pluginartifactId>
256 <version>2.8.2version>
257 plugin>
258 plugins>
259 pluginManagement>
260 build>
261 project>
View Code
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
View Code
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
View Code
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
13 <context:component-scan base-package="com.xwh"/>
14
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
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
38
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
View Code
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
25 <mvc:default-servlet-handler/>
26
27
28
29 <mvc:resources mapping="/static/**" location="static/" />
30
31
32
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>
View Code
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
12
13 <setting name="useGeneratedKeys" value="false"/>
14
15 <setting name="mapUnderscoreToCamelCase" value="true"/>
16 settings>
17
18 <typeAliases>
19 <package name="com.xwh.entity"/>
20 typeAliases>
21
22
23
24
25
26 <plugins>
27 <plugin interceptor="com.github.pagehelper.PageInterceptor">
28
29 <property name="reasonable" value="true"/>
30 plugin>
31 plugins>
32 configuration>
View Code
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>
View Code
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>
View Code
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-cruddisplay-name>
10
11 <welcome-file-list>
12 <welcome-file>index2.jspwelcome-file>
13 welcome-file-list>
14
15 <listener>
16 <listener-class>
17 org.springframework.web.context.ContextLoaderListener
18 listener-class>
19 listener>
20
21
22 <context-param>
23 <param-name>contextConfigLocationparam-name>
24 <param-value>classpath:spring/applicationContext.xmlparam-value>
25 context-param>
26
27 <context-param>
28 <param-name>log4jConfigLocationparam-name>
29 <param-value>classpath:log4j.propertiesparam-value>
30 context-param>
31
32
33 <servlet>
34 <servlet-name>dispatcherServletservlet-name>
35 <servlet-class>
36 org.springframework.web.servlet.DispatcherServlet
37 servlet-class>
38 <init-param>
39 <param-name>contextConfigLocationparam-name>
40 <param-value>classpath:springmvc/springmvc.xmlparam-value>
41 init-param>
42 <load-on-startup>1load-on-startup>
43 servlet>
44 <servlet-mapping>
45 <servlet-name>dispatcherServletservlet-name>
46 <url-pattern>/url-pattern>
47 servlet-mapping>
48
49
50 <filter>
51 <filter-name>encodingFilterfilter-name>
52 <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
53 <init-param>
54 <param-name>encodingparam-name>
55 <param-value>utf-8param-value>
56 init-param>
57 <init-param>
58 <param-name>forceRequestEncodingparam-name>
59 <param-value>trueparam-value>
60 init-param>
61 <init-param>
62 <param-name>forceResponseEncodingparam-name>
63 <param-value>trueparam-value>
64 init-param>
65 filter>
66 <filter-mapping>
67 <filter-name>encodingFilterfilter-name>
68 <url-pattern>/url-pattern>
69 filter-mapping>
70
71
72
73 <filter>
74 <filter-name>HiddenHttpMethodFilterfilter-name>
75 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
76 filter>
77 <filter-mapping>
78 <filter-name>HiddenHttpMethodFilterfilter-name>
79 <url-pattern>/*url-pattern>
80 filter-mapping>
81
82 web-app>
83
84
85
View Code