Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Dependency Management System),和用来运行定义在生命周期阶段(phase)中插件(plugin)目标(goal)的逻辑。当你使用Maven的时候,你用一个明确定义的项目对象模型来描述你的项目,然后Maven可以应用横切的逻辑,这些逻辑来自一组共享的(或者自定义的)插件。
Maven 有一个生命周期,当你运行 mvn install 的时候被调用。这条命令告诉 Maven 执行一系列的有序的步骤,直到到达你指定的生命周期。遍历生命周期旅途中的一个影响就是,Maven 运行了许多默认的插件目标,这些目标完成了像编译和创建一个 JAR 文件这样的工作。

——————————————————————————————————————————

创建maven工程:
maven是一个项目管理,使用它可以很方便的管理依赖的jar,管理项目的生命周期。
在工程栏右键->New->Maven Project

创建maven项目1

Next直到

创建maven项目2

选中maven-archetype-webapp类型,

点击Finish就完成了。

配置ssi环境:
当我们直接使用servlet开发web功能时,每个新的连接需要在web.xml中配置,获取参数时需要使用request.getParameter(“name”)去获取数据,为了解决这些类似的任务,很多解决这些问题的工程被开发出来,我们称之为框架,我们这里使用Spirng MVC做为前端的框架。
在数据库访问层,入参和出参的映射也是一些类似而繁琐的功能,借助mybatis可以跟方便的完成这些功能。
在StudentListServlet中为了返回学生列表,我们在方法中创建业务对象StudentInfoBo,那么每次请求都会创建新的对象,这些直接创建的对象,是依赖的一种,StudentListServlet直接依赖StudentInfoBo,为了解除这种直接依赖实现类的方式,我们引入Spring管理对象的生命周期和依赖关系。
下面我们把这3个框架配置的项目中

1、使用maven引入依赖的jar包
在pom.xml的dependencies标签中添加:

[code lang=”xml”]
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
[/code]

因为网络限制,可能有些jar包不能完全下载,所以下载http://pan.baidu.com/s/1mhwMYrm 中的maven配置文件,使用中国境内的maven仓库。
下载完成后,放置任何目录,Window->Preferences->Maven->User Settings选中刚才现在的文件,刷新项目所有依赖的jar都可以下载了。
2、web.xml中web-app标签内添加

[code lang=”xml”]
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:configs/spring-configs.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/css/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/js/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/img/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
[/code]

3、spring配置
在web.xml中引入了spring-configs.xml是spring的主要配置文件,文件在main/resources/configs/spring-configs.xml中,内容如下:

[code lang=”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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!– 处理中文编码方式 –>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8"/>

<!– 注解扫描包路径 –>
<context:component-scan base-package="com.sunhaojie.imlearntest" />
<!– 配置文件占位符 –>
<context:property-placeholder location="classpath:properties/configs.properties"/>
<!– 开启启动自动扫描 –>
<mvc:annotation-driven />
</beans>
[/code]

4、添加spring mvc的配置,在WEB-INFO下添加spring-servlet.xml文件

[code lang=”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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list><ref bean="mappingJacksonHttpMessageConverter" /></list>
</property>
</bean>

<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list><value>text/html;charset=UTF-8</value></list>
</property>
</bean>

<!– velocity模板配置 –>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="contentType"
value="text/html;charset=UTF-8" />
<property name="suffix" value=".vm" />
<property name="prefix" value="/WEB-INF/vm/" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
</bean>

<!– velocity引擎 –>
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">
<value>/</value>
</property>

<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
</props>
</property>
</bean>
</beans>
[/code]

5、展示helloworld
创建web层处理类HelloWorldController

[code lang=”java”]
package com.sunhaojie.imlearntest.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("hi")
public class HelloWorldController {
@RequestMapping("helloWorld")
public String index(HttpServletRequest request, HttpServletResponse response) {
request.setAttribute("helloworld", "你好,世界");
return "helloworld";
}
}
[/code]

@Controller注解的类在启动时会被Spring初始化,@RequestMapping(“hi”)表示映射路径,可修饰类和方法
方法index的访问路径为/hi/helloWorld(注意添加站点路径)
创建界面展示vm模板

[code lang=”html”]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>helloworld</title>
</head>
<body>
${helloworld}
</body>
</html>
[/code]

访问路径http://localhost:8080/imlearntest/hi/helloWorld  页面显示”你好,世界”说明spring mvc和spring已经配置成功了。
5、配置mybatis框架
在spring-configs.xml中添加引入dao层配置如下所示

[code lang=”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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!– 处理中文编码方式 –>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8"/>

<!– 注解扫描包路径 –>
<context:component-scan base-package="com.sunhaojie.imlearntest" />
<!– 配置文件占位符 –>
<context:property-placeholder location="classpath:properties/configs.properties"/>
<!– 开启启动自动扫描 –>
<mvc:annotation-driven />
<!–引入其他的配置文件 –>
<import resource="classpath:configs/spring-dao.xml"/>
</beans>
<!– spring-dao.xml配置如下: –>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<!– 数据库链接 –>
<bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!– jdbc驱动包 –>
<property name="driverClassName">
<value>${driver_class_name}</value>
</property>
<!– 链接字符串 –>
<property name="url">
<value>${jdbc_url}</value>
</property>
<!– 用户名 –>
<property name="username">
<value>${jdbc_username}</value>
</property>
<!– 密码 –>
<property name="password">
<value>${jdbc_password}</value>
</property>
</bean>
<!– 链接回话工厂,用于生成链接 –>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jdbcDataSource" />
<!– 配置文件 –>
<property name="configLocation" value="classpath:mybatis/mybatis-configs.xml"></property>
<!– 映射文件 –>
<property name="mapperLocations" value="classpath:mybatis/sqlmaps/*.xml"></property>
<!– 映射对象的基础包 –>
<property name="typeAliasesPackage" value="com.sunhaojie.imlearntest.po" />
</bean>
<!– 数据访问层接口扫描包 –>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sunhaojie.imlearntest.dao" />
</bean>
<!– 数据事务管理 –>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="jdbcDataSource"></property>
</bean>
</beans>
${jdbc_url}这些符号使用的是properties/configs.properties中的内容,因此我们引入这个文件
路径为main/resources/properties/configs.properties 内容如下
driver_class_name=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://127.0.0.1:3306/im?useUnicode=true&characterEncoding=UTF-8
jdbc_username=root
jdbc_password=

<!– 文件中还引入了一个数据库常用的配置的文件mybatis/mybatis-configs.xml –>
<!– 添加main/resources/mybatis/mybatis-configs.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="cacheEnabled" value="true" />
<setting name="lazyLoadingEnabled" value="true" />
<setting name="multipleResultSetsEnabled" value="true" />
<setting name="useColumnLabel" value="true" />
<setting name="useGeneratedKeys" value="false" />
<setting name="defaultExecutorType" value="SIMPLE" />
<setting name="defaultStatementTimeout" value="25000" />
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
[/code]

6、配合添加的spring mvc我们完成数据库中的问候语在页面端显示
创建数据库imlearntest,并创建表hello_world

[code lang=”sql”]
CREATE TABLE `hello_world` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`update_time` timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’ ON UPDATE CURRENT_TIMESTAMP,
`version` int(11) NOT NULL DEFAULT ‘0’,
`hello` varchar(64) NOT NULL DEFAULT ‘hi’,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
[/code]

创建Po对象

[code lang=”java”]
package com.sunhaojie.imlearntest.po;
import java.util.Date;
public class HelloWorldPo {
private int id;
private Date createTime;
private Date updateTime;
private int version;
private String hello;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
}
[/code]

创建访问层接口,提供获取最近的问候语

[code lang=”java”]
package com.sunhaojie.imlearntest.dao;
import com.sunhaojie.imlearntest.po.HelloWorldPo;
public interface HelloWorldDao {
public HelloWorldPo getLastOne();
}
[/code]

在main/resources/mybatis/sqlmaps添加隐射文件HelloWorld.xml

[code lang=”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.sunhaojie.imlearntest.dao.HelloWorldDao">
<select id="getLastOne" resultType="HelloWorldPo">
select * from hello_world
order by id desc
limit 1
</select>
</mapper>
[/code]

以上mybatis已经配置好了,为了在前端调用我们使controller层和dao层有调用关系,添加Bo对象

[code lang=”java”]
package com.sunhaojie.imlearntest.bo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sunhaojie.imlearntest.dao.HelloWorldDao;
import com.sunhaojie.imlearntest.po.HelloWorldPo;
@Service
public class HelloWorldBo {

@Autowired
private HelloWorldDao helloWorldDao;

public String getHello() {
HelloWorldPo lastOne = helloWorldDao.getLastOne();
return lastOne.getHello();
}
}
[/code]

注解@Service  的类在启动时会被Spring初始化,@Autowired会为修饰的属性赋值
修改Controller层,调用上面的业务对象HelloWorldBo

[code lang=”java”]
@Controller
@RequestMapping("hi")
public class HelloWorldController {

@Autowired
private HelloWorldBo helloWorldBo;
@RequestMapping("helloWorld")
public String index(HttpServletRequest request, HttpServletResponse response) {
String hello = helloWorldBo.getHello();
request.setAttribute("helloworld", hello);
return "helloworld";
}
}
[/code]

数据库中添加数据
INSERT INTO `hello_world` VALUES (‘1’, ‘2016-03-11 14:32:14’, ‘2016-03-11 14:32:14’, ‘0’, ‘你好世界-来自数据库’);
7、启动工程
访问链接:http://localhost:8080/imlearntest/hi/helloWorld
页面显示:”你好世界-来自数据库”
本节课我们创建了maven工程,并配置了常用的前端框架SpringMVC,容器框架Spring和数据库访问框架Mybatis。虽然实现了很简单的hello world功能,但是我们仍然使用了分层的代码结构web->bo->dao,良好的结构使代码更清晰,更易于维护。
小练习:
把Hello World表中的前10条的数据在页面展示。