SpringMVC项⽬结构
这⾥是项⽬搭建的⼤致⽬录结构
需要注意的是:×××l这个⽂件不能放在com.mvc.mapper这个包中,因为他不是java⽂件,所以不会⼀同编译,也就是说,⽣成的⽂件中并不包含这个,所以需要将这个×××l放在resources源⽂件⾥,但是⼜要求×××l所在的⽂件包名要和他所实现的mapper接⼝保持⼀致,所以可以在resources⾥直接新建⼀个和
com.mvc.mapper⼀样的⽂件夹,然后放进去,记住,⽂件夹新建时需要注意的事项,正确的输⼊:com/mvc/mapper  ,其他的也没有需要特别注意的地⽅,看着上⾯的⽬录结构建就可以了。
下⾯是l的依赖包和插件,写的有注释
1<?xml version="1.0" encoding="UTF-8"?>
2
3<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
4        xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
5<modelVersion>4.0.0</modelVersion>
6
7<groupId&</groupId>
8<artifactId>maven04</artifactId>
9<version>1.0-SNAPSHOT</version>
10<packaging>war</packaging><!-- 打包⽅式使⽤war,否则使⽤tomcat时会⾃动停⽌ -->
11
12<name>maven04 Maven Webapp</name>
13<url>ample</url>
14
15<!-- 全局属性配置 -->
16<properties>
17<project.build.name>maven04</project.build.name><!-- ⽤来定义war包名称-->
18<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--⽤来定义资源⽂件的编码格式-->
19<spring.version>4.2.6.RELEASE</spring.version><!-- spring版本号 -->
20<mybatis.version>3.3.0</mybatis.version><!-- mybatis版本号 -->
21<mavenpiler.source>1.8</mavenpiler.source>
22<mavenpiler.target>1.8</mavenpiler.target>
23</properties>
24
25<!--依赖包写在<dependencies>这⾥⾯ -->
26<dependencies>
27<!--servlet依赖-->
28<dependency>
29<groupId>javax.servlet</groupId>
30<artifactId>javax.servlet-api</artifactId>
31<version>3.0.1</version>
32<scope>provided</scope>
33</dependency>
34
35<!--Spring框架依赖-->
36<dependency>
37<groupId>org.springframework</groupId>
38<artifactId>spring-webmvc</artifactId>
39<version>${spring.version}</version>
40</dependency>
41<dependency>
42<groupId>org.springframework</groupId>
43<artifactId>spring-jdbc</artifactId>
44<version>${spring.version}</version>
45</dependency>
46<dependency>
47<groupId>org.springframework</groupId>
48<artifactId>spring-tx</artifactId>
49<version>${spring.version}</version>
50</dependency>
51<dependency>
52<groupId>org.springframework</groupId>
53<artifactId>spring-aspects</artifactId>
54<version>${spring.version}</version>
55</dependency>
56<dependency>
57<groupId>org.springframework</groupId>
58<artifactId>spring-web</artifactId>
59<version>${spring.version}</version>
60</dependency>
61
62<!-- slf4j依赖 -->
63<dependency>
64<groupId>org.slf4j</groupId>
65<artifactId>slf4j-log4j12</artifactId>
66<version>1.7.25</version>
67</dependency>
68
69<!-- 核⼼包依赖 -->
70<dependency>
71<groupId>com.</groupId>
72<artifactId>jackson-databind</artifactId>
73<version>2.3.3</version>
74</dependency>
75
76<!-- mybatis核⼼包依赖 -->
77<dependency>
78<groupId&batis</groupId>
79<artifactId>mybatis</artifactId>
80<version>${mybatis.version}</version>
81</dependency>
82
83<!-- mybatis-spring整合依赖-->
84<dependency>
85<groupId&batis</groupId>
86<artifactId>mybatis-spring</artifactId>
87<version>1.2.3</version>
88</dependency>
89
90<!--tomcat依赖-->
91<dependency>
92<groupId>at</groupId>
93<artifactId>tomcat-dbcp</artifactId>
94<version>9.0.37</version>
95</dependency>
96
97<!--junit依赖-->
98<dependency>
99<groupId>junit</groupId>
100<artifactId>junit</artifactId>
101<version>4.12</version>
102<scope>test</scope>
103</dependency>
104
105<!--Oracle jdbc6依赖包,⽬前没有-->
106<!-- ⼿动添加oracle驱动依赖 -->
107<dependency>
108<groupId&acle</groupId>
109<artifactId>ojdbc6</artifactId>
110<version>11.2.0</version>
111</dependency>
112
113<!-- 前端jstl依赖 -->
114<dependency>
115<groupId>javax.servlet</groupId>
116<artifactId>jstl</artifactId>
117<version>1.2</version>
118</dependency>
119
120<!-- ⽂件上传依赖-->
121<dependency>
122<groupId>commons-fileupload</groupId>
123<artifactId>commons-fileupload</artifactId>
124<version>1.3.2</version>
125</dependency>
126<dependency>
127<groupId>commons-io</groupId>
128<artifactId>commons-io</artifactId>
129<version>2.5</version>
130</dependency>
131
132</dependencies>
133
134<build>
135<finalName>maven04</finalName>
136
137<!-- 插件写在<plugin></plugin>⾥ -->
138<plugins>
139
140<!-- 配置jetty插件,启动web的第⼀种⽅式 jetty⽅式 -->
141<plugin>
142<groupId&lipse.jetty</groupId>
143<artifactId>jetty-maven-plugin</artifactId>
144<version>9.3.14.v20161028</version>
145<configuration>
146<webAppConfig>
147<maxFormContentSize>600000</maxFormContentSize>
148<contextPath>/</contextPath><!-- 配置jetty 的启动路径 -->
149</webAppConfig>
150<scanIntervalSeconds>10</scanIntervalSeconds>
151<webAppSourceDirectory>${basedir}/webapp/</webAppSourceDirectory>
152<stopPort>9998</stopPort>
153<stopKey>stop</stopKey>
154<jettyConfig/>
155<systemProperties>
156<systemProperty>
157<name>UrlEncoded</name>
158<value>GBK</value>
159</systemProperty>
160</systemProperties>
161</configuration>
162</plugin>
163<!-- 配置jetty插件 -->
164
165<!-- 配置Tomcat插件启动web的第⼀种⽅式 Tomcat⽅式 -->
166<plugin>
167<groupId>at.maven</groupId>
168<artifactId>tomcat7-maven-plugin</artifactId>
169<version>2.2</version>
170<configuration>
171<port>8088</port>
172<path>/</path><!-- 配置Tomcat 启动路径 -->
173<uriEncoding>UTF-8</uriEncoding>
174</configuration>
175</plugin>
176<!-- 配置Tomcat插件 -->
177
178</plugins>
179
180</build>
181</project>
如果写完后发现好多依赖都是爆红,只需要按照下⾯的步骤做就好了。等他讲依赖都下载完毕或就不会爆红了,
1<?xml version="1.0" encoding="UTF-8"?>
2<web-app xmlns="/xml/ns/javaee"
3        xmlns:xsi="/2001/XMLSchema-instance"
4        xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_4_0.xsd"
5        version="4.0">
6<!--设置⾸页-->
7<display-name>mva</display-name>
8<welcome-file-list>
9<welcome-file>lwl</welcome-file>
10</welcome-file-list>
11
12<!--配置Servlet-->
13<servlet>
14<servlet-name>SpringMVC</servlet-name>
15<!--Spring的核⼼控制器  -->
16<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
17<!-- 应⽤启动就加载 -->
18<load-on-startup>1</load-on-startup>
19</servlet>
20
21<servlet-mapping>
22<servlet-name>SpringMVC</servlet-name>
23<!--Jsp 服务器接收到所有的请求,会交给核⼼控制器-->
24<url-pattern>/</url-pattern>
25<!--拦截/lwl,把/lwl当做⾸页-->
log4j2 appender
26<url-pattern>/lwl</url-pattern>
27</servlet-mapping>
28
29<!--⽇志加载-->
30<context-param>
31<param-name>log4jConfigLocation</param-name>
32<param-value>/WEB-INF/log4j.properties</param-value>
33</context-param>
34<listener>
35<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
36</listener>
37
38</web-app>
1<beans xmlns="/schema/beans"
2      xmlns:context="/schema/context"
3      xmlns:xsi="/2001/XMLSchema-instance"
4      xmlns:mvc="/schema/mvc"
5      xmlns:aop="/schema/aop"
6      xsi:schemaLocation="/schema/beans
7  /schema/beans/spring-beans-3.0.xsd
8  /schema/context
9  /schema/context/spring-context-3.0.xsd /schema/mvc /schema/mvc/spring-mvc.xsd /schema/aop www.sprin 10
11
12<!--告诉spring,去哪⾥-->
13<context:component-scan base-package="com.mvc"/>
14
15<mvc:annotation-driven></mvc:annotation-driven>
16
17<mvc:resources location="/static/" mapping="/static/**"></mvc:resources>
18
19<!--分割-->
20<!--直接在Spring⾥配置Mybatis-->
21<bean id="dataSource" class="at.dbcp.dbcp2.BasicDataSource">
22<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
23<property name="url" value="jdbc:oracle:thin:@192.168.1.3:1521:xe"></property>
24<property name="username" value="lwl"></property>
25<property name="password" value="123456"></property>
26<property name="initialSize" value="5"></property>
27<property name="maxIdle" value="2"></property>
28</bean>
29
30<!--Session⼯⼚-->
31<bean id="sqlSessionFactory" class="batis.spring.SqlSessionFactoryBean">
32<property name="dataSource" ref="dataSource"/>
33<property name="mapperLocations" value="classpath:com/mvc/mapper/*.xml"/>
34<property name="typeAliasesPackage" value="del"></property>
35</bean>
36
37<bean class="batis.spring.mapper.MapperScannerConfigurer">
38<property name="basePackage" value="com.mvc.mapper"></property>
39<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
40</bean>
41
42<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
43
44<!--从Servlet⾥跳转页⾯(拼接) /web-inf/××.jsp-->
45<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
46<property name="prefix" value="/WEB-INF/jsp/"></property>
47<property name="suffix" value=".jsp"></property>
48</bean>
49
50
51</beans>
log4j.properties配置⽂件
1 #log level DEBUG/INFO/WERN/ERROR/FATAL
Logger=DEBUG,A1,A2
3
4 # console
5 log4j.appender.A1=org.apache.log4j.ConsoleAppender
6 log4j.appender.A1.layout=org.apache.log4j.PatternLayout
7 log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%n
8
9 # file
10 log4j.appender.A2=org.apache.log4j.FileAppender
11 log4j.appender.A2.File=d:/abc.log
12 log4j.appender.A2.Append=true
13 log4j.appender.A2.layout=org.apache.log4j.PatternLayout
14 log4j.appender.A2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%n
访问服务器可以使⽤jetty和tomcat,⾸先使⽤jetty,⽅法:
点击编辑
然后选择添加⼀个maven
然后点击右下⾓的Apply--->OK后就出现配置好的jetty
点击⼩箭头即可使⽤jetty⽅式运⾏,即可看到如下图所⽰的代码,此时打开浏览器输⼊localhost:8080即可访问。
使⽤tomcat运⾏项⽬
同样的⽅式新建⼀个Tomcat,然后点击⼩箭头后运⾏:
控制台会打印如下代码:直接打开这个地址,即可访问到项⽬的页⾯,所以从这个⽅⾯来说,Tomcat确实⽐jetty要好⽤

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。