java maven、springmvc、mybatis 搭建简单Web项目学笔记

前言:

  空余的时刻,学学 Java,没按哪天用的顶;

  环境多建筑折腾了几许天,总算将顺了,也做个上学笔记,以防后面会忘记;

 

一样、安装文件及介绍

  JDK:jdk1.8.0
77

  eclipse-maven:eclipse-maven

  tomcat:apache-tomcat-8.0.33

  整合资料参考:http://www.iteye.com/blogs/subjects/ssm_demo

 

  这个的布过程比较简单,搞明白关系就是得了。

 

次、maven、springmvc、mybatis 简单介绍

  maven:.NET 下的 NuGet,dll
管理库,也可好多建筑起来用,程序界的思维都是均等之;

  springmvc:就是同效仿好炎热的都实现了 ioc、di 的同一仿照 mvc 框架;

    看看开涛大神博客对 springmvc
的说;

  mybatis:数据库访问的一致学框架,我也未亮堂该不该这么的叙说他,ORM
框架;

    全说:自定义SQL、存储过程以及高级映射的持久层框架;

    一般的教程都是带下逆向工程的章程去炫耀所有表的 xml
文件,个人建议 xml 文件要要好写于好一点,

    但是这为要是根据不同之工作来定;相对来讲 MyBatis
灵活性还是对的;

 

老三、基础条件搭建

  1、JDK 安装好了以后配置下网环境变量;

  2、eclipse 配置一下 mavn;

  3、eclipse 修改一下大局编码集、以及编译的 jdk 版本;

  案例:http://www.cnblogs.com/alunchen/p/5632497.html

 

季、向世界问个好

  在新建落成后的 maven 项目遭到,跟目录下会发出一个 pom.xml 文件,

  文件是 maven 默认的布局文件,配置时路所用之 jar 包;

  参考如下:

  pom.xml

图片 1图片 2

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.cjt</groupId>
  5     <artifactId>MyMaven</artifactId>
  6     <packaging>war</packaging>
  7     <version>0.0.1-SNAPSHOT</version>
  8     <name>MacRobot Maven Webapp</name>
  9     <url>http://maven.apache.org</url>
 10     <properties>
 11         <!-- spring版本号 -->
 12         <spring.version>4.0.2.RELEASE</spring.version>
 13         <!-- mybatis版本号 -->
 14         <mybatis.version>3.2.6</mybatis.version>
 15         <!-- log4j日志文件管理包版本 -->
 16         <slf4j.version>1.7.7</slf4j.version>
 17         <log4j.version>1.2.17</log4j.version>
 18     </properties>
 19 
 20     <dependencies>
 21         <dependency>
 22             <groupId>junit</groupId>
 23             <artifactId>junit</artifactId>
 24             <version>3.8.1</version>
 25             <scope>test</scope>
 26         </dependency>
 27  
 28         <dependency>   
 29               <groupId>dom4j</groupId>    
 30               <artifactId>dom4j</artifactId>    
 31               <version>1.6</version>   
 32         </dependency>  
 33         
 34         <!-- 导入java ee jar 包 -->
 35         <dependency>
 36             <groupId>javax</groupId>
 37             <artifactId>javaee-api</artifactId>
 38             <version>7.0</version>
 39         </dependency>
 40         <!-- spring核心包 -->
 41         <dependency>
 42             <groupId>org.springframework</groupId>
 43             <artifactId>spring-core</artifactId>
 44             <version>${spring.version}</version>
 45         </dependency>
 46 
 47         <dependency>
 48             <groupId>org.springframework</groupId>
 49             <artifactId>spring-web</artifactId>
 50             <version>${spring.version}</version>
 51         </dependency>
 52         <dependency>
 53             <groupId>org.springframework</groupId>
 54             <artifactId>spring-oxm</artifactId>
 55             <version>${spring.version}</version>
 56         </dependency>
 57         <dependency>
 58             <groupId>org.springframework</groupId>
 59             <artifactId>spring-tx</artifactId>
 60             <version>${spring.version}</version>
 61         </dependency>
 62 
 63         <dependency>
 64             <groupId>org.springframework</groupId>
 65             <artifactId>spring-jdbc</artifactId>
 66             <version>${spring.version}</version>
 67         </dependency>
 68 
 69         <dependency>
 70             <groupId>org.springframework</groupId>
 71             <artifactId>spring-webmvc</artifactId>
 72             <version>${spring.version}</version>
 73         </dependency>
 74         <dependency>
 75             <groupId>org.springframework</groupId>
 76             <artifactId>spring-aop</artifactId>
 77             <version>${spring.version}</version>
 78         </dependency>
 79 
 80         <dependency>
 81             <groupId>org.springframework</groupId>
 82             <artifactId>spring-context-support</artifactId>
 83             <version>${spring.version}</version>
 84         </dependency>
 85 
 86         <dependency>
 87             <groupId>org.springframework</groupId>
 88             <artifactId>spring-test</artifactId>
 89             <version>${spring.version}</version>
 90         </dependency>
 91         <!-- mybatis核心包 -->
 92         <dependency>
 93             <groupId>org.mybatis</groupId>
 94             <artifactId>mybatis</artifactId>
 95             <version>${mybatis.version}</version>
 96         </dependency>
 97         <!-- mybatis/spring包 -->
 98         <dependency>
 99             <groupId>org.mybatis</groupId>
100             <artifactId>mybatis-spring</artifactId>
101             <version>1.2.2</version>
102         </dependency>
103         <!-- 导入java ee jar 包 -->
104         <dependency>
105             <groupId>javax</groupId>
106             <artifactId>javaee-api</artifactId>
107             <version>7.0</version>
108         </dependency>
109         <!-- 导入Mysql数据库链接jar包 -->
110         <dependency>
111             <groupId>mysql</groupId>
112             <artifactId>mysql-connector-java</artifactId>
113             <version>5.1.30</version>
114         </dependency>
115         <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
116         <dependency>
117             <groupId>commons-dbcp</groupId>
118             <artifactId>commons-dbcp</artifactId>
119             <version>1.2.2</version>
120         </dependency>
121         <!-- JSTL标签类 -->
122         <dependency>
123             <groupId>jstl</groupId>
124             <artifactId>jstl</artifactId>
125             <version>1.2</version>
126         </dependency>
127         <!-- 日志文件管理包 -->
128         <!-- log start -->
129         <dependency>
130             <groupId>log4j</groupId>
131             <artifactId>log4j</artifactId>
132             <version>${log4j.version}</version>
133         </dependency>
134 
135 
136         <!-- 格式化对象,方便输出日志 -->
137         <dependency>
138             <groupId>com.alibaba</groupId>
139             <artifactId>fastjson</artifactId>
140             <version>1.1.41</version>
141         </dependency>
142 
143 
144         <dependency>
145             <groupId>org.slf4j</groupId>
146             <artifactId>slf4j-api</artifactId>
147             <version>${slf4j.version}</version>
148         </dependency>
149 
150         <dependency>
151             <groupId>org.slf4j</groupId>
152             <artifactId>slf4j-log4j12</artifactId>
153             <version>${slf4j.version}</version>
154         </dependency>
155         <!-- log end -->
156         <!-- 映入JSON -->
157         <dependency>
158             <groupId>org.codehaus.jackson</groupId>
159             <artifactId>jackson-mapper-asl</artifactId>
160             <version>1.9.13</version>
161         </dependency>
162         <!-- 上传组件包 -->
163         <dependency>
164             <groupId>commons-fileupload</groupId>
165             <artifactId>commons-fileupload</artifactId>
166             <version>1.3.1</version>
167         </dependency>
168         <dependency>
169             <groupId>commons-io</groupId>
170             <artifactId>commons-io</artifactId>
171             <version>2.4</version>
172         </dependency>
173         <dependency>
174             <groupId>commons-codec</groupId>
175             <artifactId>commons-codec</artifactId>
176             <version>1.9</version>
177         </dependency>
178 
179     </dependencies>
180     <build>
181         <finalName>HelloWorld</finalName>
182         <plugins>
183             <plugin>
184                 <groupId>org.apache.maven.plugins</groupId>
185                 <artifactId>maven-compiler-plugin</artifactId>
186                 <version>2.3.2</version>
187                 <configuration>
188                     <source>1.8</source>
189                     <target>1.8</target>
190                 </configuration>
191             </plugin>
192         </plugins>
193     </build>
194 </project>

View Code

  PS:修改完 pom.xml 后,建议先 Maven Update
一下,将需之包文件下充斥下来,

  对许 eclipse 中吗堪查阅到下载后底 jar
包文件,路径为当里头可改,默认在 文档目录下之 .m2
文书夹,建议不要改动;

  

  web.xml 了,web 项目主配置文件

图片 3图片 4

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 3     xmlns="http://java.sun.com/xml/ns/javaee"  
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
 5     version="3.0">  
 6     
 7     <display-name>Archetype Created Web Application</display-name>
 8 
 9     <!-- Spring和mybatis的配置文件 -->
10     <context-param>
11         <param-name>contextConfigLocation</param-name>
12         <param-value>classpath:spring-mybatis.xml</param-value>
13     </context-param>
14 
15     <!-- 编码过滤器 -->
16     <filter>
17         <filter-name>characterEncodingFilter</filter-name>
18         <filter-class>org.springframework.web.filter.CharacterEncodingFilter
19         </filter-class>
20         <async-supported>true</async-supported>
21         <init-param>
22             <param-name>encoding</param-name>
23             <param-value>UTF-8</param-value>
24         </init-param>
25     </filter>
26     
27     <filter-mapping>
28         <filter-name>characterEncodingFilter</filter-name>
29         <url-pattern>/*</url-pattern>
30     </filter-mapping>
31 
32     <!-- Spring监听器 -->
33     <listener>
34         <listener-class>org.springframework.web.context.ContextLoaderListener
35         </listener-class>
36     </listener>
37     <!-- 防止Spring内存溢出监听器 -->
38     <listener>
39         <listener-class>org.springframework.web.util.IntrospectorCleanupListener
40         </listener-class>
41     </listener>
42 
43     <!-- Spring MVC servlet -->
44     <servlet>
45         <servlet-name>SpringMVC</servlet-name>
46         <servlet-class>org.springframework.web.servlet.DispatcherServlet
47         </servlet-class>
48         <init-param>
49             <param-name>contextConfigLocation</param-name>
50             <param-value>classpath:spring-mvc.xml</param-value>
51         </init-param>
52         <load-on-startup>1</load-on-startup>
53         <async-supported>true</async-supported>
54     </servlet>
55 
56     <servlet-mapping>
57         <servlet-name>SpringMVC</servlet-name>
58         <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
59         <url-pattern>/</url-pattern>
60     </servlet-mapping>
61 
62     <welcome-file-list>
63         <welcome-file>/index.jsp</welcome-file>
64     </welcome-file-list>
65 
66 </web-app>  

View Code

  PS:配置型和 springmvc 的依赖关系,就是据档动 springmvc
的入口配置;

  当 web 项目启动时,会依据安排文件下之节点信息找到呼应的布置文件就
springmvc 的着力力量;

  仔细看节点,会寻找 spring-mvc、spring-mybatis 两只 xml 文件;

 

  spring-mvc.xml

图片 5图片 6

 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" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans    
 7                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
 8                         http://www.springframework.org/schema/context    
 9                         http://www.springframework.org/schema/context/spring-context-3.1.xsd    
10                         http://www.springframework.org/schema/mvc    
11                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
12                         
13     <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
14     <context:component-scan base-package="com.helloworld.controller" />
15     <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
16     <bean id="mappingJacksonHttpMessageConverter"
17         class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
18         <property name="supportedMediaTypes">
19             <list>
20                 <value>text/html;charset=UTF-8</value>
21             </list>
22         </property>
23     </bean>
24     <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
25     <bean
26         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
27         <property name="messageConverters">
28             <list>
29                 <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
30             </list>
31         </property>
32     </bean>
33     <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
34     <bean
35         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
36         <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
37         <property name="prefix" value="/WEB-INF/" />
38         <property name="suffix" value=".jsp" />
39     </bean>
40           
41     <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
42     <bean id="multipartResolver"
43         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
44         <!-- 默认编码 -->
45         <property name="defaultEncoding" value="utf-8" />
46         <!-- 文件大小最大值 -->
47         <property name="maxUploadSize" value="10485760000" />
48         <!-- 内存中的最大值 -->
49         <property name="maxInMemorySize" value="40960" />
50     </bean>
51     
52     <!-- 
53     <mvc:annotation-driven>  
54             <mvc:message-converters>  
55                 <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
56                     <property name="objectMapper">  
57                         <bean class="com.fasterxml.jackson.databind.ObjectMapper">  
58                             <property name="dateFormat">  
59                                 <bean class="java.text.SimpleDateFormat">  
60                                     <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />  
61                                 </bean>  
62                             </property>  
63                         </bean>  
64                     </property>  
65                 </bean>  
66             </mvc:message-converters>  
67         </mvc:annotation-driven>  
68       -->
69       
70       <!-- 在实际开发中通常都需配置 mvc:annotation-driven标签,这个标签是开启注解 -->  
71       <mvc:annotation-driven></mvc:annotation-driven>  
72        <!-- 拦截器 -->  
73        <mvc:interceptors>  
74             <!-- 多个拦截器,顺序执行 -->  
75             <mvc:interceptor>  
76                 <mvc:mapping path="/**"/>  
77                 <bean class="com.helloworld.controller.BaseController"></bean>  
78             </mvc:interceptor>
79       </mvc:interceptors> 
80 </beans>

View Code

 

  PS:配置 springmvc 的主干项,核心就是以此保险扫描,

  当 web 项目时会找到  base-package=”com.macrobotinc.controller”
下的有着控制器;

  

  spring-mybatis.xml

图片 7图片 8

 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" xmlns:p="http://www.springframework.org/schema/p"  
 4     xmlns:context="http://www.springframework.org/schema/context"  
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"  
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans    
 7                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
 8                         http://www.springframework.org/schema/context    
 9                         http://www.springframework.org/schema/context/spring-context-3.1.xsd    
10                         http://www.springframework.org/schema/mvc    
11                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
12     <!-- 自动扫描 -->  
13     <context:component-scan base-package="com.helloworld.service" />  
14         
15     <!-- 引入配置文件 -->  
16     <bean id="propertyConfigurer"  
17         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
18         <property name="location" value="classpath:jdbc.properties" />  
19     </bean>  
20   
21     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
22         destroy-method="close">  
23         <property name="driverClassName" value="${driver}" />  
24         <property name="url" value="${url}" />  
25         <property name="username" value="${username}" />  
26         <property name="password" value="${password}" />  
27         <!-- 初始化连接大小 -->  
28         <property name="initialSize" value="${initialSize}"></property>  
29         <!-- 连接池最大数量 -->  
30         <property name="maxActive" value="${maxActive}"></property>  
31         <!-- 连接池最大空闲 -->  
32         <property name="maxIdle" value="${maxIdle}"></property>  
33         <!-- 连接池最小空闲 -->  
34         <property name="minIdle" value="${minIdle}"></property>  
35         <!-- 获取连接最大等待时间 -->  
36         <property name="maxWait" value="${maxWait}"></property>  
37     </bean>  
38   
39     <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
40     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
41         <property name="dataSource" ref="dataSource" />  
42         <!-- 自动扫描mapping.xml文件 -->  
43         <property name="mapperLocations" value="classpath:com/helloworld/mapping/*.xml"></property>  
44     </bean>  
45   
46     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
47     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
48         <property name="basePackage" value="com.helloworld.dao" />  
49         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
50     </bean>  
51   
52     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
53     <bean id="transactionManager"  
54         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
55         <property name="dataSource" ref="dataSource" />  
56     </bean>  
57   
58 </beans> 

View Code

  PS:同齐,找到 base-package=”com.helloworld.service”
下的兼具数据库操作接口;

  意思我理解,但是描述不出;

 

  jdbc.properties

图片 9图片 10

 1 driver=com.mysql.jdbc.Driver
 2 url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8
 3 username=root
 4 password=111111
 5 #定义初始连接数  
 6 initialSize=1
 7 #定义最大连接数  
 8 maxActive=20
 9 #定义最大空闲  
10 maxIdle=20
11 #定义最小空闲  
12 minIdle=1
13 #定义最长等待时间  
14 maxWait=60000

View Code

  log4j.properties

图片 11图片 12

 1 ### set log levels ###
 2 #log4j.rootLogger = debug , stdout , D , E
 3 log4j.rootLogger = debug , stdout , D
 4 
 5 ###  output to the console ###
 6 log4j.appender.stdout = org.apache.log4j.ConsoleAppender
 7 log4j.appender.stdout.Target = System.out
 8 log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
 9 #log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
10 log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
11 
12 ### Output to the log file ###
13 log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
14 log4j.appender.D.File = ${springmvc.root}/WEB-INF/logs/log.log
15 log4j.appender.D.Append = true
16 log4j.appender.D.Threshold = DEBUG 
17 log4j.appender.D.layout = org.apache.log4j.PatternLayout
18 log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
19 
20 ### Save exception information to separate file ###
21 log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
22 log4j.appender.D.File = ${springmvc.root}/WEB-INF/logs/error.log 
23 log4j.appender.D.Append = true
24 log4j.appender.D.Threshold = ERROR 
25 log4j.appender.D.layout = org.apache.log4j.PatternLayout
26 log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
27 
28 log4j.logger.com.macrobotinc.dao=debug

View Code

 

  这个时基本的配备都起了,和 .NET MVC 比起来安排确实非常费时间,

  但是同效会好安排了以后,以后就是知晓了,其实为十分简短。

网站地图xml地图