目录
http://maven.apache.org/download.cgi
哈喽~~大家好!结束了我们会议OA项目之后,我们这期给大家分一个构建工具!!
一、maven简介
Apache Maven是个项目管理和自动构建工具,基于项目对象模型(POM)的概念。
作用:完成项目的相关操作,如:编译,构建,单元测试,安装,网站生成和基于Maven部署项目。
xxx.class
百度—>xxx.jar
在百度上进行下载
将xxx.jar导入工程中
私服
工程中的某一个xml文件中写入一个字符串,达到jar下载的作用
二,官网下载
http://maven.apache.org/download.cgi
我们进入官网之后
去选择下载先对应的压缩包
下载之后放入非中文目录下面进行解压
三,配置Maven
配置环境变量
选择此电脑,右键属性,打开系统:
选择高级系统
选择环境变量
这里按以下图片操作即可
三、eclipse中配置maven
1、点击window->perferences
点击add后点击Directory,找到安装maven的根目录即可
四、案例
1.实体类
package com.zking.entity;
public class User {
private Long id;
private String name;
private String loginName;
private String pwd;
private Long rid;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Long getRid() {
return rid;
}
public void setRid(Long rid) {
this.rid = rid;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", loginName=" + loginName + ", pwd=" + pwd + ", rid=" + rid + "]";
}
}
2、dao方法
package com.zking.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.zking.entity.User;
import com.zking.util.DBAccess;
import com.zking.util.DBHelper;
public class UserDao {
// 登录
public User login(User u){
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
con=DBAccess.getConnection();
String sql="select * from t_oa_user where loginName=? and pwd=?";
ps = con.prepareStatement(sql);
ps.setString(1, u.getLoginName());
ps.setString(2, u.getPwd());
rs=ps.executeQuery();
if(rs.next()) {
u.setId(rs.getLong(1));
u.setName(rs.getString(2));
u.setLoginName(rs.getString(3));
u.setPwd(rs.getString(4));
u.setRid(rs.getLong(5));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return u;
}
}
3、action层
package com.zking.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.dao.UserDao;
import com.zking.entity.User;
@WebServlet("/login.do")
public class UserAction extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码方式
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html; charset=UTF-8");
//拿out
PrintWriter out = resp.getWriter();
UserDao ud=new UserDao();
//接收表单传值
String sname = req.getParameter("name");
String pwd = req.getParameter("pwd");
User u=new User();
u.setLoginName(sname);
u.setPwd(pwd);
User u1 = ud.login(u);
if(u1!=null) {
out.print("<script>alert('登录成功');</script>");
}else{
out.print("<script>alert('登录失败');</script>");
}
}
}
4、util包
package com.zking.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* 提供了一组获得或关闭数据库对象的方法
*
*/
public class DBHelper {
private static String driver;
private static String url;
private static String user;
private static String password;
static {// 静态块执行一次,加载 驱动一次
try {
InputStream is = DBHelper.class
.getResourceAsStream("config.properties");
Properties properties = new Properties();
properties.load(is);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("pwd");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 获得数据连接对象
*
* @return
*/
public static Connection getConnection() {
try {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void close(ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Statement stmt) {
if (null != stmt) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection conn) {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void myClose(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(rs!=null) {
rs.close();
}
if(ps!=null) {
ps.close();
}
if(con!=null&&!con.isClosed()) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
close(rs);
close(stmt);
close(conn);
}
public static boolean isOracle() {
return "oracle.jdbc.driver.OracleDriver".equals(driver);
}
public static boolean isSQLServer() {
return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
}
public static boolean isMysql() {
return "com.mysql.jdbc.Driver".equals(driver);
}
public static void main(String[] args) {
Connection conn = DBHelper.getConnection();
DBHelper.close(conn);
System.out.println("isOracle:" + isOracle());
System.out.println("isSQLServer:" + isSQLServer());
System.out.println("isMysql:" + isMysql());
System.out.println("数据库连接(关闭)成功");
}
}
②config.peoperties
#oracle9i
#driver=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@localhost:1521:ora9
#user=test
#pwd=test
#sql2005
#driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
#url=jdbc:sqlserver://localhost:1423;DatabaseName=test
#user=sa
#pwd=sa
#sql2000
#driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
#url=jdbc:microsoft:sqlserver://localhost:1433;databaseName=unit6DB
#user=sa
#pwd=888888
#mysql5
#driver=com.mysql.jdbc.Driver
#url=jdbc:mysql://127.0.0.1:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
#user=mybatis_ssm
#pwd=xiaoli
#mysql8
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/t280?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=true
user=root
pwd=123456
运行结果: