1、【File】->【new】->【Project】,选择【maven】,勾选【Create from archetype】,选择【maven-archetype-webapp】选项,点击Next。
2、填写【GroupId】和【Artifactld】等信息,点击next。
3、填写Maven配置文件路径。
4、得到如下项目结构。
5、在/src/main目录下创建java和test目录,并标记为Sources文件。
至此,一个完整的Web项目创建完成。
6、集成Spring。
首先在pom文件中添加相关依赖。具体代码如下:
<properties> <spring.version>5.0.4.RELEASE</spring.version> </properties> <dependencies> <!--spring start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!--spring end --> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
其次,在/src/main/resources目录下创建applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.hengx" /> </beans>
<context:component-scan base-package="com.hengx"/> 注解:扫描base-package包或者子包下面所有Java类,并把匹配的Java类注册成为Bean。接着我们配置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> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
<context-param>:整个项目的全局变量,相当于设定一个固定值。param-name是键,相当于就是参数名,param-value就是值,相当于参数值。ContextLoaderListener :ContextLoaderListener监听器实现了ServletContextListener接口,其作用就是启动Web容器时,自动装配ApplicationContext的配置信息。在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
7、集成Spring MVC框架
首先把集成Spring MVC所需要的Maven依赖添加到pom.xml文件中。
<properties> <spring.version>5.0.4.RELEASE</spring.version> <javax.servlet.version>4.0.0</javax.servlet.version> <jstl.version>1.2</jstl.version> </properties> <!--springmvc start --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.servlet.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!--springmvc end -->
其次,在web.xml配置文件中添加DispatcherServlet配置。
<!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> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>spring-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置SpringMVC需要加载的配置文件 spring-mvc.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring-dispatcher</servlet-name> <!-- 默认匹配所有的请求--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
DispatcherServlet类:DispatcherServlet是前置控制器,主要用于拦截匹配的请求,拦截匹配规则要自己定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理,是配置Spring的第一步。
<load-on-startup>:表示启动容器时初始化该Servlet。当值为0或者大于0时,表示容器在应用启动时加载并初始化这个Servlet。如果小于0或未指定时,则指示容器在该Servlet被选择时才加载。正值越小,Servlet的优先级越高。
接下来我们在/src/main/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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 扫描controller(后端控制器),并且扫描其中的注解--> <context:component-scan base-package="com.hengx.controller" /> <!-- 设置配置方案--> <mvc:annotation-driven /> <!-- 配置 JSP 显示ViewResolver(视图解析器)-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
测试一下
8、集成Mybatis框架
导入依赖。
<properties> <spring.version>5.0.4.RELEASE</spring.version> <javax.servlet.version>4.0.0</javax.servlet.version> <jstl.version>1.2</jstl.version> <mybatis.version>3.4.0</mybatis.version> <mysql.connector.java.version>5.1.38</mysql.connector.java.version> <druid.version>1.1.9</druid.version> <mybatis.spring.version>1.3.1</mybatis.spring.version> </properties> <!--mybatis start --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.connector.java.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!--mybatis end -->
其次,在/src/main/resources 目录下创建jdbc.properties配置文件
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/数据库名字?serverTimezone=GMT jdbc.username=用户名 jdbc.password=密码
在applicationContext.xml中添加如下配置:
<!--1、配置数据库相关参数--> <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/> <!--2.数据源 druid --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--3、配置SqlSessionFactory对象--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--注入数据库连接池--> <property name="dataSource" ref="dataSource"/> <!--扫描sql配置文件:mapper需要的xml文件--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <!-- mybatis配置文件的位置 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- 配置分页插件 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value> helperDialect=mysql reasonable=true </value> </property> </bean> </array> </property> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!-- 扫描basePackage下所有以@MyBatisDao注解的接口 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.hengx.dao"/> </bean>
所有配置都已完成,那么下面我们开发相关代码。
在Mysql创建表hero。
CREATE TABLE `hero` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(32) DEFAULT NULL, `hp` float DEFAULT NULL, `damage` int(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8
数据库表创建完之后,在/src/main/java/com.hengx.model 目录下创建数据库表对应的实体类对象Hero。
public class Hero { private int id; private String name; private float hp; private int damage; //省略get、set方法 }
实体类对象Hero创建完成之后,在/src/main/java/com.hengx.dao 目录下创建对应的DAO对象HeroDao,HeroDao是一个接口,提供了findAll方法来查询所有的用户。
package com.hengx.dao; import com.hengx.model.Hero; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface HeroDao { List<Hero> findAll(); }
接口类HeroDao 创建完成之后,在/src/main/java/com.hengx.service目录下创建对应服务层接口HeroService,服务层接口HeroService 只提供了一个查询所有用户的方法findAll()。
package com.hengx.service; import com.hengx.model.Hero; import java.util.List; public interface HeroService { List<Hero> findAll(); }
然后开发对应实现类HeroServiceImpl。
package com.hengx.service.impl; import com.hengx.dao.HeroDao; import com.hengx.model.Hero; import com.hengx.service.HeroService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class HeroServiceImpl implements HeroService { @Resource private HeroDao heroDao; @Override public List<Hero> findAll() { return heroDao.findAll(); } }
之后创建,HeroController。
package com.hengx.controller; import com.hengx.model.Hero; import com.hengx.service.HeroService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import java.util.List; @Controller @RequestMapping("/user") public class HeroController { @Resource private HeroService heroService; @GetMapping("/findAll") public String findAll(Model model){ List<Hero> heroList = heroService.findAll(); for (Hero hero:heroList) { System.out.println("id=:"+hero.getId()); System.out.println("name=:"+hero.getName()); } return "hello"; } }
最后,在/src/main/resources/mapper目录下创建HeroMapper.xml 。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.hengx.dao.HeroDao"> <select id="findAll" resultType="com.hengx.model.Hero"> SELECT * FROM hero </select> </mapper>
OK,访问/user/findAll
成功查询到数据。整合成功。
9.1、集成Log4j日志框架
首先导入依赖
<properties> <slf4j.version>1.7.25</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency>
之后在 /src/main/java/resources/下创建配置文件 log4j.properties
### set log levels ### log4j.rootLogger=DEBUG,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=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### 输出到日志文件 ### #log4j.appender.File=org.apache.log4j.RollingFileAppender #log4j.appender.File.File=${project}/WEB-INF/logs/app.log #log4j.appender.File.DatePattern=_yyyyMMdd'.log' #log4j.appender.File.MaxFileSize=10MB #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,SSS}][%c]%m%n
9.2、集成Junit测试框架
导入依赖
<!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
至此结束!