概述
1.应用各种jar包
2.编译
3.单元测试
4.整合
核心功能一、依赖管理:maven工程对jar包的管理过程
项目的一键构建:mvn tomcat:run
二、maven安装
maven下载:https://maven.apache.org/download.cgi
解压
环境变量配置:
系统变量:新建
编辑path
<mirrors>
<mirror>
//该镜像的id
<id>nexus-aliyun</id>
//该镜像用来取代的远程仓库,central是中央仓库的id
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
//该镜像的仓库地址,这里是用的阿里的仓库
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
仓库种类和彼此关系
仓库分三类:本地仓库,远程仓库(私服), 中央仓库
maven标准目录
src/main/java 目录 核心代码部分
src/main/resources 配置文件部分
src/test/java 测试代码部分
src/test/resources测试配置文件
src/main/webapp页面资源,js,css,图片等等
maven常用命令
mvn clean 把target目录删掉(删除本地编译的代码)
mvn compile 生成tartget(重新生成本地编译)
mvn test 生成tartget中的test
mvn clean
mvn package war包
mvn clean
mvn install 同时安装到了本地仓库
idea集成maven插件
使用骨架创建maven的java工程
①new project
②Maven
③ maven-archetype-quickstart
④next -
没有resource自己创建
推荐:不使用骨架创建maven的java工程
①new project
② 填写名字等
有resource
使用骨架创建maven的web工程
①new project
②Maven
③ maven-archetype-webapp
目录
maven工程servlet示例
file - project structure - modules -
在目录下创建jsp
取消,删除即可
maven工程servlet实例之导入项目
引入依赖pom.xml
pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
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>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.dou.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
MyServlet
package com.dou.servlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/hello.jsp").forward(request, response);
}
}
jar包冲突
编译和运行的jar包冲突
在依赖中添加作用域
maven的java工程mysql数据库
创建表
创建实体类
package com.dou.domain;
public class Items {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
添加依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
ItemsDao
package com.dou.dao;
import com.dou.domain.Items;
import java.sql.SQLException;
import java.util.List;
public interface ItemsDao {
List<Items> findAll() throws SQLException, Exception;
}
package com.dou.dao.impl;
import com.dou.dao.ItemsDao;
import com.dou.domain.Items;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ItemsDaoImpl implements ItemsDao {
public List<Items> findAll() throws SQLException {
List<Items> list = new ArrayList<Items>();
//先获取connection对象
Connection connection= null;
PreparedStatement pst = null;
ResultSet rs = null;
try{
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql:///maven","root","1");
pst = connection.prepareCall("select * from items");
rs = pst.executeQuery();
while (rs.next()){
Items item = new Items();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
list.add(item);
}
}catch (Exception e){
e.printStackTrace();
}finally {
connection.close();
pst.close();
rs.close();
}
return list;
}
}
测试
package com.dou.test;
import com.dou.dao.ItemsDao;
import com.dou.dao.impl.ItemsDaoImpl;
import com.dou.domain.Items;
import org.junit.Test;
import java.util.List;
public class ItemsTest {
@Test
public void findAll() throws Exception {
ItemsDao itemsDao = new ItemsDaoImpl();
List<Items> list = itemsDao.findAll();
for (Items items : list) {
System.out.println(items.getName());
}
}
}