iOS转Java环境搭建
------------------------------分割线------------------------------ 2023-03-02
一、目录结构
二、源码
1.bean、connection、dao、service、servlet
1 package com.zlj.bean; 2 3 import java.util.Date; 4 5 public class Order { 6 7 private int proid; 8 private int count; 9 private int singleprice; 10 private int sffk; 11 private int visible; 12 13 private String userId; 14 private String tel; 15 private String address; 16 private String senddept; 17 18 private String date; 19 20 21 22 public Order() { 23 super(); 24 } 25 26 public Order(int proid, int count, int singleprice, String userId, int sffk, String tel, String address, String date, String senddept, int visible) { 27 this.proid = proid; 28 this.count = count; 29 this.singleprice = singleprice; 30 this.userId = userId; 31 this.sffk = sffk; 32 this.tel = tel; 33 this.address = address; 34 this.date = date; 35 this.senddept = senddept; 36 this.visible = visible; 37 } 38 39 public int getProid() { 40 return proid; 41 } 42 43 public void setProid(int proid) { 44 this.proid = proid; 45 } 46 47 public int getCount() { 48 return count; 49 } 50 51 public void setCount(int count) { 52 this.count = count; 53 } 54 55 public int getSingleprice() { 56 return singleprice; 57 } 58 59 public void setSingleprice(int singleprice) { 60 this.singleprice = singleprice; 61 } 62 63 public String getUserId() { 64 return userId; 65 } 66 67 public void setUserId(String userId) { 68 this.userId = userId; 69 } 70 71 public int getSffk() { 72 return sffk; 73 } 74 75 public void setSffk(int sffk) { 76 this.sffk = sffk; 77 } 78 79 public String getTel() { 80 return tel; 81 } 82 83 public void setTel(String tel) { 84 this.tel = tel; 85 } 86 87 public String getAddress() { 88 return address; 89 } 90 91 public void setAddress(String address) { 92 this.address = address; 93 } 94 95 public String getDate() { 96 return date; 97 } 98 99 public void setDate(String date) { 100 this.date = date; 101 } 102 103 public String getSenddept() { 104 return senddept; 105 } 106 107 public void setSenddept(String senddept) { 108 this.senddept = senddept; 109 } 110 111 public int getVisible() { 112 return visible; 113 } 114 115 public void setVisible(int visible) { 116 this.visible = visible; 117 } 118 119 @Override 120 public String toString() { 121 return "Order{" + 122 "proid=" + proid + 123 ", count=" + count + 124 ", singleprice=" + singleprice + 125 ", userId='" + userId + '\'' + 126 ", sffk=" + sffk + 127 ", tel='" + tel + '\'' + 128 ", address='" + address + '\'' + 129 ", date=" + date + 130 ", senddept='" + senddept + '\'' + 131 ", visible=" + visible + 132 '}'; 133 } 134 }
1 package com.zlj.connection; 2 3 public interface connection { 4 5 public void LinkDB();//连接数据库 6 }
1 package com.zlj.connection; 2 3 import java.sql.DriverManager; 4 import java.sql.SQLException; 5 import java.sql.Connection; 6 7 8 public class LinkDb implements connection { 9 10 11 public Connection conn; 12 private String DB_URL="jdbc:mysql://localhost:3306/sale?useUnicode=true&characterEncoding=UTF-8"; 13 private String USER="root"; 14 private String PASS="long123456"; 15 16 @Override 17 public void LinkDB() { //实现连接书库的方法 18 try { 19 Class.forName("com.mysql.cj.jdbc.Driver"); 20 conn = DriverManager.getConnection(DB_URL,USER,PASS); 21 }catch (ClassNotFoundException e){ 22 e.printStackTrace(); 23 } catch (SQLException throwables){ 24 throwables.printStackTrace(); 25 } 26 } 27 }
1 package com.zlj.dao; 2 3 import com.zlj.bean.Order; 4 5 import java.util.List; 6 7 public interface OrderDao { 8 9 List<Order> queryOrder(); 10 }
1 package com.zlj.dao; 2 3 import com.mysql.cj.protocol.Resultset; 4 import com.zlj.bean.Order; 5 import com.zlj.connection.LinkDb; 6 7 import java.sql.PreparedStatement; 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 import java.util.LinkedList; 11 import java.util.List; 12 13 public class OrderDaoImpl implements OrderDao { 14 @Override 15 public List<Order> queryOrder() { 16 17 18 LinkDb linkDb = new LinkDb();//获取数据库 19 linkDb.LinkDB();//链接数据库 20 String sql = "select * from order1"; 21 List<Order> resultList = new LinkedList<>(); 22 23 try { 24 PreparedStatement pst = linkDb.conn.prepareStatement(sql); 25 ResultSet resultset = pst.executeQuery(); 26 while (resultset.next() == true){ 27 Order order = new Order(); 28 order.setProid(resultset.getInt("proid")); 29 order.setCount(resultset.getInt("count")); 30 order.setSingleprice(resultset.getInt("singleprice")); 31 order.setSffk(resultset.getInt("sffk")); 32 order.setVisible(resultset.getInt("visible")); 33 34 order.setUserId(resultset.getString("userId")); 35 order.setTel(resultset.getString("tel")); 36 order.setAddress(resultset.getString("address")); 37 order.setSenddept(resultset.getString("senddept")); 38 order.setDate(resultset.getString("date")); 39 40 resultList.add(order); 41 } 42 43 } catch (SQLException e) { 44 e.printStackTrace(); 45 } 46 47 return resultList; 48 } 49 }
1 package com.zlj.service; 2 3 import com.zlj.bean.Order; 4 5 import java.util.List; 6 7 public interface OrderSearch { 8 9 List<Order> searchOrder(); 10 }
1 package com.zlj.service; 2 import com.zlj.bean.Order; 3 import com.zlj.dao.OrderDao; 4 import com.zlj.dao.OrderDaoImpl; 5 import java.util.List; 6 7 public class OrderSearchImpl implements OrderSearch{ 8 9 @Override 10 public List<Order> searchOrder() { 11 12 OrderDao orderDao = new OrderDaoImpl(); 13 return orderDao.queryOrder(); 14 } 15 }
1 package com.zlj.servlet; 2 3 import com.zlj.bean.Order; 4 import com.zlj.service.OrderSearch; 5 import com.zlj.service.OrderSearchImpl; 6 import net.sf.json.JSONArray; 7 import net.sf.json.JSONObject; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.annotation.WebServlet; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 import java.io.IOException; 15 import java.io.PrintWriter; 16 import java.text.SimpleDateFormat; 17 import java.util.List; 18 19 @WebServlet("/order") 20 public class OrderServlet extends HttpServlet { 21 @Override 22 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 23 resp.setContentType("application/json;charset=utf-8"); 24 resp.setCharacterEncoding("UTF-8"); 25 OrderSearch orderSearch = new OrderSearchImpl(); 26 List<Order> resultList = orderSearch.searchOrder(); 27 JSONObject jsonObject = new JSONObject(); 28 JSONArray listArr = new JSONArray(); 29 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 30 simpleDateFormat.setLenient(false); 31 for (Order order : resultList) { 32 jsonObject.put("proid",order.getProid()); 33 jsonObject.put("count",order.getCount()); 34 jsonObject.put("singleprice",order.getSingleprice()); 35 jsonObject.put("userid",order.getUserId()); 36 jsonObject.put("sffk",order.getSffk()); 37 jsonObject.put("tel",order.getTel()); 38 jsonObject.put("address",order.getAddress()); 39 jsonObject.put("date",order.getDate()); 40 jsonObject.put("senddept",order.getSenddept()); 41 listArr.add(jsonObject); 42 } 43 JSONObject jsonObj = new JSONObject(); 44 jsonObj.put("code",200); 45 jsonObj.put("status",true); 46 jsonObj.put("msg","获取成功"); 47 jsonObj.put("list",listArr); 48 PrintWriter out = resp.getWriter(); 49 out.print(jsonObj); 50 out.flush(); 51 out.close(); 52 } 53 }
这么多东西,真心没哟Xcode方便,Java没学好,就已经累死在了学Java的路上!!!
jdk安装
官网:https://www.oracle.com/java/technologies/javase-downloads.html
下载地址:https://www.oracle.com/java/technologies/javase-jdk15-downloads.html
终端:java -version 查看是否安装
idea/eclipse + tomcat + mysql
eclipse 安装 官网:https://www.eclipse.org/downloads/ 下载地址:https://www.eclipse.org/downloads/download.php?file=/oomph/epp/2020-06/R/eclipse-inst-mac64.dmg
idea 安装 www.baidu.com
tomcat: 参考网站 https://www.jianshu.com/p/db08d23049ce https://www.cnblogs.com/yocichen/p/10478431.html 官网:http://tomcat.apache.org/ 下载地址:https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-9/v9.0.37/bin/apache-tomcat-9.0.37.tar.gz
mysql 参考:https://juejin.im/post/6844903831298375693 官网:https://www.mysql.com/downloads/ 下载地址:https://downloads.mysql.com/archives/community/
客户端:sequel pro (需要查下下载github上面的版本)
关注公众号,获取源码(gitee)回复servlet