• maven的安装即案例


    目录

    一、maven简介

    二,官网下载 

     http://maven.apache.org/download.cgi

    三,配置Maven

             配置环境变量

    三、eclipse中配置maven

    1、点击window->perferences

    四、案例

    1.实体类

    2、dao方法

    3、action层

    4、util包


    哈喽~~大家好!结束了我们会议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.实体类

    1. package com.zking.entity;
    2. public class User {
    3. private Long id;
    4. private String name;
    5. private String loginName;
    6. private String pwd;
    7. private Long rid;
    8. public Long getId() {
    9. return id;
    10. }
    11. public void setId(Long id) {
    12. this.id = id;
    13. }
    14. public String getName() {
    15. return name;
    16. }
    17. public void setName(String name) {
    18. this.name = name;
    19. }
    20. public String getLoginName() {
    21. return loginName;
    22. }
    23. public void setLoginName(String loginName) {
    24. this.loginName = loginName;
    25. }
    26. public String getPwd() {
    27. return pwd;
    28. }
    29. public void setPwd(String pwd) {
    30. this.pwd = pwd;
    31. }
    32. public Long getRid() {
    33. return rid;
    34. }
    35. public void setRid(Long rid) {
    36. this.rid = rid;
    37. }
    38. public User() {
    39. super();
    40. // TODO Auto-generated constructor stub
    41. }
    42. @Override
    43. public String toString() {
    44. return "User [id=" + id + ", name=" + name + ", loginName=" + loginName + ", pwd=" + pwd + ", rid=" + rid + "]";
    45. }
    46. }

    2、dao方法

    1. package com.zking.dao;
    2. import java.sql.Connection;
    3. import java.sql.PreparedStatement;
    4. import java.sql.ResultSet;
    5. import com.zking.entity.User;
    6. import com.zking.util.DBAccess;
    7. import com.zking.util.DBHelper;
    8. public class UserDao {
    9. // 登录
    10. public User login(User u){
    11. Connection con=null;
    12. PreparedStatement ps=null;
    13. ResultSet rs=null;
    14. try {
    15. con=DBAccess.getConnection();
    16. String sql="select * from t_oa_user where loginName=? and pwd=?";
    17. ps = con.prepareStatement(sql);
    18. ps.setString(1, u.getLoginName());
    19. ps.setString(2, u.getPwd());
    20. rs=ps.executeQuery();
    21. if(rs.next()) {
    22. u.setId(rs.getLong(1));
    23. u.setName(rs.getString(2));
    24. u.setLoginName(rs.getString(3));
    25. u.setPwd(rs.getString(4));
    26. u.setRid(rs.getLong(5));
    27. }
    28. } catch (Exception e) {
    29. e.printStackTrace();
    30. }finally {
    31. DBHelper.close(con, ps, rs);
    32. }
    33. return u;
    34. }
    35. }

    3、action层

    1. package com.zking.web;
    2. import java.io.IOException;
    3. import java.io.PrintWriter;
    4. import javax.servlet.ServletException;
    5. import javax.servlet.annotation.WebServlet;
    6. import javax.servlet.http.HttpServlet;
    7. import javax.servlet.http.HttpServletRequest;
    8. import javax.servlet.http.HttpServletResponse;
    9. import com.zking.dao.UserDao;
    10. import com.zking.entity.User;
    11. @WebServlet("/login.do")
    12. public class UserAction extends HttpServlet{
    13. @Override
    14. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    15. doPost(req, resp);
    16. }
    17. @Override
    18. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    19. //设置编码方式
    20. req.setCharacterEncoding("utf-8");
    21. resp.setCharacterEncoding("utf-8");
    22. resp.setContentType("text/html; charset=UTF-8");
    23. //拿out
    24. PrintWriter out = resp.getWriter();
    25. UserDao ud=new UserDao();
    26. //接收表单传值
    27. String sname = req.getParameter("name");
    28. String pwd = req.getParameter("pwd");
    29. User u=new User();
    30. u.setLoginName(sname);
    31. u.setPwd(pwd);
    32. User u1 = ud.login(u);
    33. if(u1!=null) {
    34. out.print("");
    35. }else{
    36. out.print("");
    37. }
    38. }
    39. }

    4、util包

    ①DBHelper

    1. package com.zking.util;
    2. import java.io.InputStream;
    3. import java.sql.Connection;
    4. import java.sql.DriverManager;
    5. import java.sql.PreparedStatement;
    6. import java.sql.ResultSet;
    7. import java.sql.SQLException;
    8. import java.sql.Statement;
    9. import java.util.Properties;
    10. /**
    11. * 提供了一组获得或关闭数据库对象的方法
    12. *
    13. */
    14. public class DBHelper {
    15. private static String driver;
    16. private static String url;
    17. private static String user;
    18. private static String password;
    19. static {// 静态块执行一次,加载 驱动一次
    20. try {
    21. InputStream is = DBHelper.class
    22. .getResourceAsStream("config.properties");
    23. Properties properties = new Properties();
    24. properties.load(is);
    25. driver = properties.getProperty("driver");
    26. url = properties.getProperty("url");
    27. user = properties.getProperty("user");
    28. password = properties.getProperty("pwd");
    29. Class.forName(driver);
    30. } catch (Exception e) {
    31. e.printStackTrace();
    32. throw new RuntimeException(e);
    33. }
    34. }
    35. /**
    36. * 获得数据连接对象
    37. *
    38. * @return
    39. */
    40. public static Connection getConnection() {
    41. try {
    42. Connection conn = DriverManager.getConnection(url, user, password);
    43. return conn;
    44. } catch (SQLException e) {
    45. e.printStackTrace();
    46. throw new RuntimeException(e);
    47. }
    48. }
    49. public static void close(ResultSet rs) {
    50. if (null != rs) {
    51. try {
    52. rs.close();
    53. } catch (SQLException e) {
    54. e.printStackTrace();
    55. throw new RuntimeException(e);
    56. }
    57. }
    58. }
    59. public static void close(Statement stmt) {
    60. if (null != stmt) {
    61. try {
    62. stmt.close();
    63. } catch (SQLException e) {
    64. e.printStackTrace();
    65. throw new RuntimeException(e);
    66. }
    67. }
    68. }
    69. public static void close(Connection conn) {
    70. if (null != conn) {
    71. try {
    72. conn.close();
    73. } catch (SQLException e) {
    74. e.printStackTrace();
    75. throw new RuntimeException(e);
    76. }
    77. }
    78. }
    79. public static void myClose(Connection con,PreparedStatement ps,ResultSet rs) {
    80. try {
    81. if(rs!=null) {
    82. rs.close();
    83. }
    84. if(ps!=null) {
    85. ps.close();
    86. }
    87. if(con!=null&&!con.isClosed()) {
    88. con.close();
    89. }
    90. } catch (Exception e) {
    91. e.printStackTrace();
    92. }
    93. }
    94. public static void close(Connection conn, Statement stmt, ResultSet rs) {
    95. close(rs);
    96. close(stmt);
    97. close(conn);
    98. }
    99. public static boolean isOracle() {
    100. return "oracle.jdbc.driver.OracleDriver".equals(driver);
    101. }
    102. public static boolean isSQLServer() {
    103. return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
    104. }
    105. public static boolean isMysql() {
    106. return "com.mysql.jdbc.Driver".equals(driver);
    107. }
    108. public static void main(String[] args) {
    109. Connection conn = DBHelper.getConnection();
    110. DBHelper.close(conn);
    111. System.out.println("isOracle:" + isOracle());
    112. System.out.println("isSQLServer:" + isSQLServer());
    113. System.out.println("isMysql:" + isMysql());
    114. System.out.println("数据库连接(关闭)成功");
    115. }
    116. }

    ②config.peoperties

    1. #oracle9i
    2. #driver=oracle.jdbc.driver.OracleDriver
    3. #url=jdbc:oracle:thin:@localhost:1521:ora9
    4. #user=test
    5. #pwd=test
    6. #sql2005
    7. #driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
    8. #url=jdbc:sqlserver://localhost:1423;DatabaseName=test
    9. #user=sa
    10. #pwd=sa
    11. #sql2000
    12. #driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
    13. #url=jdbc:microsoft:sqlserver://localhost:1433;databaseName=unit6DB
    14. #user=sa
    15. #pwd=888888
    16. #mysql5
    17. #driver=com.mysql.jdbc.Driver
    18. #url=jdbc:mysql://127.0.0.1:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
    19. #user=mybatis_ssm
    20. #pwd=xiaoli
    21. #mysql8
    22. driver=com.mysql.cj.jdbc.Driver
    23. url=jdbc:mysql://127.0.0.1:3306/t280?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=true
    24. user=root
    25. pwd=123456

    运行结果:

  • 相关阅读:
    科普系列:AUTOSAR与OSEK网络管理比较(上)
    【Godot4.1】Godot实现闪烁效果(Godot使用定时器实现定时触发的效果)
    Autosar基础——DLT(DiagnosticLogAndTrace)
    新建Maui工程运行到IiOS物理设备提示 Could not find any available provisioning profiles for iOS 处理办法
    Dockerfile(6) - EXPOSE 指令详解
    龙蜥开发者说:开源是场马拉松!来自广州大学姚同学的开源成长记 | 第 13 期
    外汇天眼:美国10年期国债收益率创下2011年以来新高
    Spring源码解析—— IOC默认标签解析(下)
    致谢每一位ChunJun Contributor!这里有一份礼物等你领取!
    C++中public、protected及private用法
  • 原文地址:https://blog.csdn.net/weixin_63544775/article/details/126156257