JAVA Class14
Posted Observer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA Class14相关的知识,希望对你有一定的参考价值。
学习内容:
分包(分层):
通过定义不同的包来将工程的各个功能细分,一般包含如下几部分:
view层作用: 视图层,即项目中的界面
controller层作用: 控制层, 获取界面上的数据,为界面设置数据; 将要实现的功能交给业务层处理
service层作用: 业务层, 功能的实现, 与controller控制层和数据访问层DAO交互, 将对数据库的操作交给DAO数据访问层来处理
dao层作用: 数据访问层, 用来操作数据库表的数据
db数据库: 这里指mysql
domain 实体包: 存放JavaBean
tools工具包:存放项目中使用到的工具类
test 测试包: 存放项目功能测试的代码
编写顺序:
以下是一个简单的超市管理系统:
1.tools包,创建工具方法,建立数据库连接,关闭数据库连接
package com.store.tools; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class DButil { private DButil() {}; public static Connection getConn() { try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://127.0.0.1:3306/demo_JDBC?characterEncoding=UTF-8"; String username="root"; String password="123456"; Connection conn=DriverManager.getConnection(url,username,password); return conn; } catch (Exception e) { throw new RuntimeException(e+"数据库连接失败"); } } public static void close(Statement sta,Connection conn) { if(sta!=null) { try { sta.close(); } catch (Exception e) { e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } } public static void close(ResultSet rs,Statement sta,Connection conn) { if(rs!=null) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } } close(sta,conn); } }
2.domain包,创建实体类,定义Store类的构造方法、属性等
package com.store.domain; public class Store { private int sid; private String brands; private int size; private double price; private int counts; public Store(){}; public Store(int sid,String brands,int size,double price,int counts) { this.sid = sid; this.brands = brands; this.size = size; this.price = price; this.counts = counts; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getBrands() { return brands; } public void setBrands(String brands) { this.brands = brands; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getCounts() { return counts; } public void setCounts(int counts) { this.counts = counts; } }
3.view包,创建操作页面
package com.store.view; import java.util.ArrayList; import java.util.Scanner; import com.store.controller.StoreController; import com.store.domain.Store; import com.store.tools.DButil; public class StoreView { StoreController storeController = new StoreController(); Scanner sc = new Scanner(System.in); public void run() { while(true) { System.out.println("------------商城管理系统------------"); System.out.println("1.查询所有商品信息"); System.out.println("2.新增商品信息"); System.out.println("3.修改商品信息"); System.out.println("4.删除商品信息"); System.out.println("5.退出"); System.out.println("请选择功能序号:"); int choose = sc.nextInt(); switch(choose) { case 1: find(); break; case 2: find(); add(); break; case 3: find(); update(); break; case 4: find(); delete(); break; case 5: System.out.println("欢迎下次使用!"); return; default: System.out.println("选择有误,请重新选择功能!"); } } } public void find() { ArrayList<Store> list = storeController.find(); System.out.println("商品编号\\t商品品牌\\t商品尺寸\\t商品价格\\t商品数量"); for(int i=0;i<list.size();i++) { System.out.printf("%d\\t%s\\t%d\\t%f\\t%d%n" ,list.get(i).getSid(),list.get(i).getBrands(),list.get(i).getSize(),list.get(i).getPrice(),list.get(i).getCounts()); } } public void add() { System.out.println("请输入商品品牌:"); String brands = sc.next(); System.out.println("请输入商品尺寸"); int size = sc.nextInt(); System.out.println("请输入商品价格:"); double price = sc.nextDouble(); System.out.println("请输入商品数量:"); int counts= sc.nextInt(); Store store = new Store(); store.setBrands(brands); store.setSize(size); store.setPrice(price); store.setCounts(counts); storeController.add(store); } public void update() { System.out.println("请输入要修改商品的ID"); int sid = sc.nextInt(); System.out.println("请输入修改后的商品品牌:"); String brands = sc.next(); System.out.println("请输入修改后的商品尺寸"); int size = sc.nextInt(); System.out.println("请输入修改后的商品价格:"); double price = sc.nextDouble(); System.out.println("请输入修改后的商品数量:"); int counts= sc.nextInt(); Store store = new Store(); store.setSid(sid); store.setBrands(brands); store.setSize(size); store.setPrice(price); store.setCounts(counts); storeController.update(store); } public void delete() { System.out.println("请输入要删除的商品的ID"); int sid = sc.nextInt(); Store store = new Store(); store.setSid(sid); storeController.delete(store); } }
4.dao包,定义操作数据库的方法
package com.store.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import com.store.domain.Store; import com.store.tools.DButil; public class StoreDAO { public ArrayList<Store> find() { try { Connection conn = DButil.getConn(); Statement sta = conn.createStatement(); String sql = "select * from store"; ResultSet rs = sta.executeQuery(sql); ArrayList<Store> store = new ArrayList<Store>(); while(rs.next()) { Store s = new Store(rs.getInt("sid"),rs.getString("brands"),rs.getInt("size"),rs.getDouble("price"),rs.getInt("counts")); store.add(s); } DButil.close(rs, sta, conn); return store; } catch (Exception e) { System.out.println(e); throw new RuntimeException("商品查询失败"); } } public int add(Store store) { try { Connection conn = DButil.getConn(); String sql = "insert into store(brands,size,price,counts) values(?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, store.getBrands()); ps.setInt(2, store.getSize()); ps.setDouble(3, store.getPrice()); ps.setInt(4,store.getCounts()); int row = ps.executeUpdate(); DButil.close(ps, conn); return row; } catch (Exception e) { System.out.println(e); throw new RuntimeException("商品添加失败"); } } public int update(Store store) { try { Connection conn = DButil.getConn(); String sql = "update store set brands=?,size=?,price=?,counts=? where sid=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, store.getBrands()); ps.setInt(2, store.getSize()); ps.setDouble(3, store.getPrice()); ps.setInt(4,store.getCounts()); ps.setInt(5,store.getSid()); int row = ps.executeUpdate(); DButil.close(ps, conn); return row; } catch (Exception e) { System.out.println(e); throw new RuntimeException("商品修改失败"); } } public int delete(Store store) { try { Connection conn = DButil.getConn(); String sql = "delete from store where sid=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, store.getSid()); int row = ps.executeUpdate(); DButil.close(ps, conn); return row; } catch (Exception e) { System.out.println(e); throw new RuntimeException("商品删除失败"); } } }
5.service包,业务类,实现controller与dao包间的数据交换
package com.store.service; import java.util.ArrayList; import com.store.dao.StoreDAO; import com.store.domain.Store; public class StoreService { private StoreDAO storeDAO = new StoreDAO(); public ArrayList<Store> find(){ return storeDAO.find(); } public int add(Store store) { return storeDAO.add(store); } public int update(Store store) { return storeDAO.update(store); } public int delete(Store store) { return storeDAO.delete(store); } }
6.controller包,捕获view包传递的数据
package com.store.controller; import java.util.ArrayList; import com.store.domain.Store; import com.store.service.StoreService; public class StoreController { private StoreService storeService = new StoreService(); public ArrayList<Store> find(){ return storeService.find(); } public void add(Store store) { int mes = storeService.add(store); if(mes>0) { System.out.println("商品添加成功"); }else { System.out.println("商品添加失败"); } } public void update(Store store) { int mes = storeService.update(store); if(mes>0) { System.out.println("商品修改成功"); }else { System.out.println("商品修改失败"); } } public void delete(Store store) { int mes = storeService.delete(store); if(mes>0) { System.out.println("商品删除成功"); }else { System.out.println("商品删除失败"); } } }
7.test包,测试功能
package com.store.test; import com.store.view.StoreView; public class TestStore { public static void main(String[] args) { new StoreView().run(); } }
以上是关于JAVA Class14的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段14——Vue的axios网络请求封装
VSCode自定义代码片段14——Vue的axios网络请求封装
VSCode自定义代码片段14——Vue的axios网络请求封装