java项目学习(一)

一、搭建空的Maven项目

环境变量配置这里就不多说了,网上很多,写这个目标是为了熟悉java开发及运行过程。

使用Intellij idea 搭建基本的maven空项目 new->project

 

点击下一步 Next

groupId

定义了项目属于哪个组,举个例子,如果你的公司是mycom,有一个项目为myapp,那么groupId就应该是com.mycom.myapp.

artifacted

定义了当前maven项目在组中唯一的ID,比如,myapp-util,myapp-domain,myapp-web等。

version

指定了myapp项目的当前版本,SNAPSHOT意为快照,说明该项目还处于开发中,是不稳定的版本。

 

继续点击下一步,finish创建成功,

构建项目基本骨架 
1,在java下新建com 
2,在com下新建springmvc 
3,在springmvc下新建controller、dao、mapping、pojo、service 
如下图:

这里写图片描述

目录解释如下 
controller:http(GET和POST等)请求的入口,调用Service,想客户端返回数据

dao:接口,定义了数据表映射成的对象,具有的操作

mapping:具体是sql语句,以xml格式存在,即mybatis的语法文件

service:controller调用的服务,service调用dao里面定义的数据库行为方法,来执行对应mapping里的mybatis,从而完成与数据库完整的交互。

二、修改pom文件

配置maven

 在pom.xml文件中添加如下内容

  1     <properties>
  2         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3         <!-- spring版本号 -->
  4         <spring.version>5.0.9.RELEASE</spring.version>
  5         <!-- mybatis版本号 -->
  6         <mybatis.version>3.4.6</mybatis.version>
  7         <!-- log4j日志文件管理包版本 -->
  8         <slf4j.version>1.7.7</slf4j.version>
  9         <log4j.version>1.2.17</log4j.version>
 10         <!-- jackson包版本 -->
 11         <jackson.version>2.5.0</jackson.version>
 12     </properties>
 13 
 14 
 15     <!-- https://mvnrepository.com/artifact/junit/junit -->
 16     <dependencies>
 17         <dependency>
 18             <groupId>junit</groupId>
 19             <artifactId>junit</artifactId>
 20             <version>4.12</version>
 21             <scope>test</scope>
 22         </dependency>
 23         <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
 24         <dependency>
 25             <groupId>org.mybatis</groupId>
 26             <artifactId>mybatis</artifactId>
 27             <version>${mybatis.version}</version>
 28         </dependency>
 29         <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
 30         <dependency>
 31             <groupId>org.mybatis</groupId>
 32             <artifactId>mybatis-spring</artifactId>
 33             <version>1.3.1</version>
 34         </dependency>
 35         <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
 36         <dependency>
 37             <groupId>org.springframework</groupId>
 38             <artifactId>spring-test</artifactId>
 39             <version>${spring.version}</version>
 40             <scope>test</scope>
 41         </dependency>
 42         <!-- springMVC核心包 -->
 43         <dependency>
 44             <groupId>org.springframework</groupId>
 45             <artifactId>spring-webmvc</artifactId>
 46             <version>${spring.version}</version>
 47         </dependency>
 48 
 49         <!-- spring核心包 -->
 50         <dependency>
 51             <groupId>org.springframework</groupId>
 52             <artifactId>spring-core</artifactId>
 53             <version>${spring.version}</version>
 54         </dependency>
 55 
 56         <dependency>
 57             <groupId>org.springframework</groupId>
 58             <artifactId>spring-beans</artifactId>
 59             <version>${spring.version}</version>
 60         </dependency>
 61 
 62         <dependency>
 63             <groupId>org.springframework</groupId>
 64             <artifactId>spring-context</artifactId>
 65             <version>${spring.version}</version>
 66         </dependency>
 67 
 68         <dependency>
 69             <groupId>org.springframework</groupId>
 70             <artifactId>spring-context-support</artifactId>
 71             <version>${spring.version}</version>
 72         </dependency>
 73 
 74         <dependency>
 75             <groupId>org.springframework</groupId>
 76             <artifactId>spring-tx</artifactId>
 77             <version>${spring.version}</version>
 78         </dependency>
 79 
 80         <dependency>
 81             <groupId>org.springframework</groupId>
 82             <artifactId>spring-web</artifactId>
 83             <version>${spring.version}</version>
 84         </dependency>
 85 
 86         <dependency>
 87             <groupId>org.springframework</groupId>
 88             <artifactId>spring-jdbc</artifactId>
 89             <version>${spring.version}</version>
 90         </dependency>
 91 
 92         <dependency>
 93             <groupId>javax.servlet</groupId>
 94             <artifactId>javax.servlet-api</artifactId>
 95             <version>3.0.1</version>
 96             <scope>provided</scope>
 97         </dependency>
 98 
 99         <dependency>
100             <groupId>javax.servlet.jsp</groupId>
101             <artifactId>jsp-api</artifactId>
102             <version>2.2</version>
103             <scope>provided</scope>
104         </dependency>
105 
106         <!-- 导入Mysql数据库链接jar包 -->
107         <dependency>
108             <groupId>mysql</groupId>
109             <artifactId>mysql-connector-java</artifactId>
110             <version>5.1.30</version>
111         </dependency>
112 
113         <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
114         <dependency>
115             <groupId>commons-dbcp</groupId>
116             <artifactId>commons-dbcp</artifactId>
117             <version>1.2.2</version>
118         </dependency>
119 
120         <!-- JSTL标签类 -->
121         <dependency>
122             <groupId>jstl</groupId>
123             <artifactId>jstl</artifactId>
124             <version>1.2</version>
125         </dependency>
126         <!-- 日志文件管理包 -->
127         <!-- log start -->
128         <dependency>
129             <groupId>log4j</groupId>
130             <artifactId>log4j</artifactId>
131             <version>${log4j.version}</version>
132         </dependency>
133 
134 
135         <!-- 格式化对象,方便输出日志 -->
136         <dependency>
137             <groupId>com.alibaba</groupId>
138             <artifactId>fastjson</artifactId>
139             <version>1.1.41</version>
140         </dependency>
141 
142 
143         <dependency>
144             <groupId>org.slf4j</groupId>
145             <artifactId>slf4j-api</artifactId>
146             <version>${slf4j.version}</version>
147         </dependency>
148 
149         <dependency>
150             <groupId>org.slf4j</groupId>
151             <artifactId>slf4j-log4j12</artifactId>
152             <version>${slf4j.version}</version>
153         </dependency>
154         <!-- log end -->
155         <!-- 映入JSON -->
156         <dependency>
157             <groupId>org.codehaus.jackson</groupId>
158             <artifactId>jackson-mapper-asl</artifactId>
159             <version>1.9.13</version>
160         </dependency>
161 
162         <dependency>
163             <groupId>com.fasterxml.jackson.core</groupId>
164             <artifactId>jackson-databind</artifactId>
165             <version>2.1.0</version>
166         </dependency>
167         <dependency>
168             <groupId>com.fasterxml.jackson.core</groupId>
169             <artifactId>jackson-annotations</artifactId>
170             <version>2.1.0</version>
171         </dependency>
172 
173         <!-- 上传组件包 -->
174         <dependency>
175             <groupId>commons-fileupload</groupId>
176             <artifactId>commons-fileupload</artifactId>
177             <version>1.3.1</version>
178         </dependency>
179         <dependency>
180             <groupId>commons-io</groupId>
181             <artifactId>commons-io</artifactId>
182             <version>2.4</version>
183         </dependency>
184         <dependency>
185             <groupId>commons-codec</groupId>
186             <artifactId>commons-codec</artifactId>
187             <version>1.9</version>
188         </dependency>
189 
190         <dependency>
191             <groupId>junit</groupId>
192             <artifactId>junit</artifactId>
193             <version>4.11</version>
194             <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
195             <scope>test</scope>
196         </dependency>
197 
198         <!--Spring test相关依赖-->
199         <dependency>
200             <groupId>org.springframework</groupId>
201             <artifactId>spring-test</artifactId>
202             <version>4.1.7.RELEASE</version>
203         </dependency>
204         <dependency>
205             <groupId>junit</groupId>
206             <artifactId>junit</artifactId>
207             <version>RELEASE</version>
208         </dependency>
209 
210         <dependency>
211             <groupId>org.jsoup</groupId>
212             <artifactId>jsoup</artifactId>
213             <version>1.8.1</version>
214         </dependency>
215 
216         <dependency>
217             <groupId>net.sourceforge.htmlunit</groupId>
218             <artifactId>htmlunit</artifactId>
219             <version>2.15</version>
220         </dependency>
221 
222 
223         <dependency>
224             <groupId>org.seleniumhq.selenium</groupId>
225             <artifactId>selenium-java</artifactId>
226             <version>2.33.0</version>
227         </dependency>
228 
229         <dependency>
230             <groupId>us.codecraft</groupId>
231             <artifactId>webmagic-core</artifactId>
232             <version>0.6.1</version>
233         </dependency>
234         <dependency>
235             <groupId>us.codecraft</groupId>
236             <artifactId>webmagic-extension</artifactId>
237             <version>0.6.1</version>
238         </dependency>
239 
240 
241     </dependencies>
242 
243     <build>
244         <finalName>com</finalName>
245         <resources>
246             <resource>
247                 <directory>src/main/java</directory>
248                 <includes>
249                     <include>**/*.xml</include>
250                 </includes>
251                 <filtering>true</filtering>
252             </resource>
253         </resources>
254     </build>

三、编写ssm配置文件

在resources下新建个配置文件jdbc.properties,内容如下:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
username=root
password=dk137046
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000

请确保你的mysql服务是开启的,且存在数据库test

在resources下新建个配置文件spring-mvc.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                    http://www.springframework.org/schema/context  
                    http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                    http://www.springframework.org/schema/mvc  
                    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.springmvc.controller" />

<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="supportedMediaTypes">
        <list>
            <value>text/html;charset=UTF-8</value>
        </list>
    </property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="mappingJacksonHttpMessageConverter" />   <!-- JSON转换器 -->
        </list>
    </property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
    <property name="prefix" value="/" />
    <property name="suffix" value=".jsp" />
</bean>

<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"  
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    <!-- 默认编码 -->
    <property name="defaultEncoding" value="utf-8" />  
    <!-- 文件大小最大值 -->
    <property name="maxUploadSize" value="10485760000" />  
    <!-- 内存中的最大值 -->
    <property name="maxInMemorySize" value="40960" />  
</bean> 

</beans>

mybatis-config.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--配置全局属性-->
    <settings>
        <setting name="logImpl" value="LOG4J" />
        <setting name="cacheEnabled" value="true"/>
    </settings>
</configuration>

spring-mybatis配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.1.xsd
                    http://www.springframework.org/schema/mvc
                    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.springmvc" />
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:com/springmvc/mapping/*.xml"></property>

    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.springmvc.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

log4j.xml

日志模块的配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "http://toolkit.alibaba-inc.com/dtd/log4j/log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://jakarta.apache.org/log4j/ ">
    <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}]-[%c]-[%t] %x - %m%n"/>
        </layout>
    </appender>

    <!-- Mybatis and SQL -->
    <logger name="org.apache.ibatis" additivity="true">
        <level value="DEBUG"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="org.mybatis.spring" additivity="true">
        <level value="ERROR"/>
        <appender-ref ref="consoleAppender"/>
    </logger>
    <logger name="java.sql" additivity="true">
        <level value="DEBUG"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="java.sql.Connection" additivity="true">
        <level value="DEBUG"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="java.sql.Statement" additivity="true">
        <level value="DEBUG"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <logger name="java.sql.PreparedStatement" additivity="true">
        <level value="DEBUG"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <!-- Spring -->
    <logger name="org.springframework" additivity="true">
        <level value="ERROR"/>
        <appender-ref ref="consoleAppender"/>
    </logger>

    <!-- ActiveMQ -->
    <logger name="org.apache.activemq" additivity="true">
        <level value="ERROR"/>
        <appender-ref ref="consoleAppender"/>
    </logger>
    <!-- ===================================================================== -->
    <!--  根日志记录器                                                          -->
    <!-- ===================================================================== -->
    <root>
        <level value="DEBUG"/>
        <appender-ref ref="consoleAppender"/>
    </root>
</log4j:configuration>

log4j.properties

定义LOG输出级别
log4j.rootLogger=INFO,Console,File
定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
指定输出目录
log4j.appender.File.File = logs/ssm.log
定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c]%m%n

至此:项目配置完成

四、配置项目工程

第一步:将配置文件添加入工程

五、配置webapp

在src下创建目录webapp 

web.xml文件

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <display-name>Archetype Created Web Application</display-name>
    <!-- Spring和mybatis的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml</param-value>
    </context-param>

    <!--Log4j配置-->
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>


    <!-- 编码过滤器 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>

    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--Spring监听器 ApplicationContext 载入 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 防止Spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>


    <!-- Spring MVC 核心配置开始 -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 可以通过contextConfigLocation来自定义SpringMVC配置文件的位置,如不指定,则默认在WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,
             此时文件名必须为[<servlet-name>]-servlet.xml,否则会出错-->
        <!--以下init-param是自定义SpringMVC的配置文件的位置 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>


    <!-- 拦截设置 -->
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- Spring MVC 核心配置结束 -->

    <!-- 配置SESSION超时,单位是分钟 -->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>


    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

把webapp添加入项目工程 

添加artifacts 

 

 配置tomcat

 

posted @ 2019-05-28 16:24  (时光)光阴飞逝  阅读(598)  评论(0编辑  收藏  举报