team小项目
技术选型
1. 数据库: MySQL5.7
2. 数据源: Druid
3. Spring技术中的 JdbcTemplate

druid.properties:
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/study?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8 username=root password=root initialSize=5 maxActive=10 maxWait=3000
架构:
实体类:
Dao开发:
package com.gton.dao; import com.gton.emuns.TeamConst; import com.gton.entity.Employee; import com.gton.entity.Equipment; import com.gton.utils.DruidUtil; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; import java.util.stream.Collectors; /** * @program: Jdbc-start * @description: employee * @author: GuoTong * @create: 2020-09-02 10:21 **/ public class EmployeeDao { //JdbcTemplate private JdbcTemplate template= new JdbcTemplate(DruidUtil.getDataSource()); public Employee getMemberId(Integer tid) { return template.queryForObject("select * from employee where memberId=?",new Object[]{tid}, new BeanPropertyRowMapper<>(Employee.class)); } //查询所有员工 public List<Employee> findAll(){ //new BeanPropertyRowMapper<>(Employee.class) List<Employee> query = template.query("select * from employee", new BeanPropertyRowMapper<>(Employee.class)); /*query.stream().map(e->{ //将设备的实体类添加到员工当中 return e; }).collect(Collectors.toList());*/ return query; } //根据id查询 public Employee getFindById(Integer id){ //template.query("select * from equipment where id =?",) List<Employee> collect = findAll().stream().filter(e -> e.getId().equals(id)).collect(Collectors.toList()); if (collect.size()==0) return null; return collect.get(0); } //返回团队成员 public List<Employee> getTeam(){ //new BeanPropertyRowMapper<>(Employee.class) List<Employee> query = template.query("select * from employee where memberId is not null order by memberId asc", new BeanPropertyRowMapper<>(Employee.class)); /*query.stream().map(e->{ //将设备的实体类添加到员工当中 return e; }).collect(Collectors.toList());*/ return query; } //添加团队,字段menmber 设置值 //状态也要改变。 public void addMenber(Employee employee) { //status --->修改为 1 ;Status :状态 0 :休闲 1 :已经在团队中 2 : 休假中 //获取当前团队的人数 + 1 就是当前队员的 tid 字段menmber int tid = getTeam().size() + 1; template.update("update employee set memberId=?,status=? where id=?", tid, TeamConst.GONG_ZUO,employee.getId()); } public void removeTeam(Employee employee) { //移除队员就是修改状态 template.update("update employee set memberId=null,status=? where id=?", TeamConst.XIU_XIAN,employee.getId()); } public void updateTid(Employee e) { //重新编排tid template.update("update employee set memberId=memberId-1 where id=?",e.getId()); } }
package com.gton.dao; import com.gton.entity.Equipment; import com.gton.utils.DruidUtil; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; import java.util.stream.Collectors; /** * @program: Jdbc-start * @description: equipmentDao * @author: GuoTong * @create: 2020-09-02 09:55 **/ public class EquipmentDao { //JdbcTemplate private JdbcTemplate template= new JdbcTemplate(DruidUtil.getDataSource()); //查询所有 public List<Equipment> getFindAll(){ List<Equipment> query = template.query("select * from equipment", new BeanPropertyRowMapper<>(Equipment.class)); return query; } //根据ID查询 public Equipment getFindById(Integer id){ //template.query("select * from equipment where id =?",) List<Equipment> collect = getFindAll().stream().filter(e -> e.getId().equals(id)).collect(Collectors.toList()); if (collect.size()==0) return null; return collect.get(0); } //增加设备 public void add(Equipment equipment){ int update = template.update("insert into equipment values(null,?,?,?,?)", equipment.getPrice(), equipment.getModel(), equipment.getDisplay(), equipment.getType()); if (update>=0){ System.out.println("添加成功"); System.out.println(getFindById(equipment.getId())); }else { System.out.println("添加失败"); } } //修改设备 根据ID public void updateById(Equipment equipment){ System.out.println("修改之前"); System.out.println(getFindById(equipment.getId())); int update = template.update("update equipment set price=?,model=?,display=?,type=?where id=?", equipment.getPrice(), equipment.getModel(), equipment.getDisplay(), equipment.getType(), equipment.getId()); if (update>=0){ System.out.println("修改成功"); System.out.println(getFindById(equipment.getId())); }else { System.out.println("修改失败"); } } //根据ID 删除设备 public void deleteById(Integer id){ int update =template.update("delete from equipment where id=?",id); if (update>=0){ System.out.println("删除成功"); }else { System.out.println("删除失败"); } } }
Dao测试
public class EmployeeDaoTest { //测试对象 private EmployeeDao employeeDao; @Before public void init(){ employeeDao=new EmployeeDao(); } @Test public void TestFindAll(){ employeeDao.findAll().forEach(System.out::println); } @Test public void TestFindById(){ Employee findById = employeeDao.getFindById(1); System.out.println(findById); } }
public class EquipmentTest {
//测试对象
private EquipmentDao equipmentDao;
@Before
public void init(){
equipmentDao=new EquipmentDao();
}
@Test
public void TestFindAll(){
equipmentDao.getFindAll().forEach(System.out::println);
}
@Test
public void insertTest(){
Equipment equipment = new Equipment(null, 6666.00, "IQOO New 3", "6.75寸", 1);
equipmentDao.add(equipment);
}
}
Service:
package com.gton.service; import com.gton.dao.EmployeeDao; import com.gton.dao.EquipmentDao; import com.gton.emuns.TeamConst; import com.gton.entity.Employee; import com.gton.entity.Equipment; import java.util.*; import java.util.stream.Collectors; /** * @program: Jdbc-start * @description: * @author: GuoTong * @create: 2020-09-02 10:36 **/ public class EmployeeService { private EmployeeDao employeeDao = new EmployeeDao(); private EquipmentDao equipmentDao =new EquipmentDao(); public static Map<Integer,Employee> empDEV = new HashMap<>(); public static int tid=0; public List<Employee> findAll() { return employeeDao.findAll(); } //根据id得到Equipment,Employee----->MAP public Map<Employee, Equipment> getByALl(){ List<Employee> all = findAll(); Map<Employee, Equipment> mapList = new HashMap<>(); for (int i=0;i<all.size();i++){ Employee employee = all.get(i); Integer equipmentId = employee.getEquipmentId(); Equipment findById = equipmentDao.getFindById(equipmentId); mapList.put(employee,findById); } return mapList; } // 显示开发团队成员列表 public Map<Integer,Employee> getDEV(){ return empDEV; } //添加到团队 public void addToDEVByEMPId(Integer id) { Employee employee = employeeDao.getFindById(id); if (empDEV.size()!=0){ Set<Map.Entry<Integer, Employee>> entries = empDEV.entrySet(); for (Map.Entry<Integer, Employee> entry : entries) { if (entry.getValue().getId()==id){ System.out.println("该用户已经存在"); return; } } } int size = empDEV.size(); empDEV.put(++tid, employee); if (empDEV.size()<=size){ System.out.println("添加失败"); }else { System.out.println("添加成功"); } } //从团队中删除指定tid的成员 public void deleteByTid(Integer id){ Employee remove = empDEV.remove(id); if (remove!=null){ System.out.println("成功删除"); }else { System.out.println("删除失败"); } } //获取团队成员 public List<Employee> getTeamlist(){ return employeeDao.getTeam(); } //添加团队成员 public void addMenber(Integer id){ List<Employee> teamlist = getTeamlist(); //团队成员已满,,无法添加 if (teamlist.size()== TeamConst.MAX_COUNT){ System.out.println("团队成员已满,无法添加"); return; } //该成员是否满足要求 Employee findById = employeeDao.getFindById(id); //如果不存在 if (findById==null){ System.out.println("该员工不存在,无法添加"); return; } //架构师,设计师,程序员 if (!(findById.getType().equals(TeamConst.JIA_GOU)||findById.getType().equals(TeamConst.SHE_JI)||findById.getType().equals(TeamConst.CHENG_XU_YUAN))){ System.out.println("该成员不是开发人员,,无法添加"); return; } //判断该成员是不是已经在团队中 /* boolean anyMatch = teamlist.stream().anyMatch(e -> e.getId().equals(findById.getId())); * if (anyMatch){ System.out.println("该员工已经在团队中,无法添加。。"); return; }*/ //getStatus :状态 0 :休闲 1 :已经在团队中 2 : 休假中 if (findById.getStatus().equals(TeamConst.GONG_ZUO)){ System.out.println("该员工已经在团队中,无法添加。。"); return; } if (findById.getStatus().equals(TeamConst.DU_JIA)){ System.out.println("该员工正在休假,无法添加。。"); return; } //getType 1:ceo 2.架构师 3 :设计师 4 程序员 //找出架构的人数,最多一名。。。。。 long JiaGou = teamlist.stream().filter(e -> e.getType().equals(TeamConst.JIA_GOU)).count(); if (JiaGou==TeamConst.MAX_JIA_GOU && findById.getType().equals(TeamConst.JIA_GOU)){ System.out.println("架构师已满,无法添加"); return; } //找出设计师 long SheJi = teamlist.stream().filter(e -> e.getType().equals(TeamConst.SHE_JI)).count(); if (SheJi==TeamConst.MAX_SHE_JI && findById.getType().equals(TeamConst.SHE_JI)){ System.out.println("设计师已满,无法添加"); return; } //找出程序员 long ChenXu = teamlist.stream().filter(e -> e.getType().equals(TeamConst.CHENG_XU_YUAN)).count(); if (SheJi==TeamConst.MAX_CHENG_XU_YUAN && findById.getType().equals(TeamConst.CHENG_XU_YUAN)){ System.out.println("程序员已满,无法添加"); return; } employeeDao.addMenber(findById); System.out.println("添加成功"); } //删除队伍人员 public void removeMember(Integer tid){ //根据tid查询到这个员工 Employee employee= employeeDao.getMemberId(tid); if (employee==null){ System.out.println("该队员不存在,,,"); return; } //根据这个员工信息修改数据 状态。。。member employeeDao.removeTeam(employee); //tid需要重新排序,,获取后面的 List<Employee> collect = getTeamlist().stream().filter(e -> e.getMemberId() > tid).collect(Collectors.toList()); collect.forEach(e->{ employeeDao.updateTid(e); }); } }
视图层展示调用:
package com.gton.view; import com.alibaba.druid.sql.visitor.functions.If; import com.gton.entity.Employee; import com.gton.entity.Equipment; import com.gton.service.EmployeeService; import com.gton.utils.TSUtility; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; public class TeamView { private EmployeeService service =new EmployeeService(); public void enterMainMenu() { boolean loopFlag = true; char key = 0; do { if (key != '1') { listAllEmployees(); } System.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):"); key = TSUtility.readMenuSelection(); System.out.println(); switch (key) { case '1': listTeam(); break; case '2': addMember(); break; case '3': deleteMember(); break; case '4': System.out.print("确认是否退出(Y/N):"); char yn = TSUtility.readConfirmSelection(); if (yn == 'Y') loopFlag = false; break; } } while (loopFlag); } // 显示所有的员工成员 private void listAllEmployees() { System.out.println("\n-------------------------------开发团队调度软件--------------------------------\n"); Map<Employee, Equipment> byALl = service.getByALl(); if (byALl.size() == 0) { System.out.println("没有客户记录!"); } else { System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备"); } Set<Map.Entry<Employee, Equipment>> entries = byALl.entrySet(); for (Map.Entry<Employee, Equipment> entry : entries) { System.out.println(entry.getKey()+entry.getValue().getModel()); } System.out.println("-------------------------------------------------------------------------------"); } // 显示开发团队成员列表 private void listTeam() { System.out.println("\n--------------------团队成员列表---------------------\n"); List<Employee> teamlist = service.getTeamlist(); if (teamlist.size()==0){ System.out.println("开发团队目前没有成员"); }else{ System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票"); } for (Employee employee : teamlist) { System.out.println(employee.getMemberId()+"/"+employee.getId()+"\t" +employee.getName()+"\t"+employee.getAge()+"\t"+employee.getSalary()+"\t"+employee.getBonus()+"\t" +employee.getStock()); } /*Map<Integer,Employee> dev = service.getDEV(); if (dev.size() == 0) { System.out.println("开发团队目前没有成员!"); } else { System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票"); } Set<Map.Entry<Integer, Employee>> entries = dev.entrySet(); for (Map.Entry<Integer, Employee> entry : entries) { System.out.println(entry.getKey()+"/"+entry.getValue().getId()+"\t"+ entry.getValue().getName()+"\t"+ entry.getValue().getAge()+"\t"+ entry.getValue().getSalary()+"\t"+ entry.getValue().getViewType()+"\t"+ entry.getValue().getBonus()+"\t"+ entry.getValue().getStock()); }*/ System.out.println("-----------------------------------------------------"); } // 添加成员到团队 private void addMember() { System.out.println("---------------------添加成员---------------------"); System.out.print("请输入要添加的员工ID:"); int id = TSUtility.readInt(); service.addMenber(id); // 按回车键继续... TSUtility.readReturn(); } // 从团队中删除指定id的成员 private void deleteMember() { System.out.println("---------------------删除成员---------------------"); System.out.print("请输入要删除员工的TID:"); int id = TSUtility.readInt(); System.out.print("确认是否删除(Y/N):"); char yn = TSUtility.readConfirmSelection(); if (yn == 'N') return; service.removeMember(id); // 按回车键继续... TSUtility.readReturn(); } }
键盘输入工具类:druid数据库管理工具类:
package com.gton.utils; import java.util.*; public class TSUtility { private static Scanner scanner = new Scanner(System.in); public static char readMenuSelection() { char c; for (; ; ) { String str = readKeyBoard(1, false); c = str.charAt(0); if (c != '1' && c != '2' && c != '3' && c != '4') { System.out.print("选择错误,请重新输入:"); } else break; } return c; } public static void readReturn() { System.out.print("按回车键继续..."); readKeyBoard(100, true); } public static int readInt() { int n; for (; ; ) { String str = readKeyBoard(2, false); try { n = Integer.parseInt(str); break; } catch (NumberFormatException e) { System.out.print("数字输入错误,请重新输入:"); } } return n; } public static char readConfirmSelection() { char c; for (; ; ) { String str = readKeyBoard(1, false).toUpperCase(); c = str.charAt(0); if (c == 'Y' || c == 'N') { break; } else { System.out.print("选择错误,请重新输入:"); } } return c; } private static String readKeyBoard(int limit, boolean blankReturn) { String line = ""; while (scanner.hasNextLine()) { line = scanner.nextLine(); if (line.length() == 0) { if (blankReturn) return line; else continue; } if (line.length() < 1 || line.length() > limit) { System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:"); continue; } break; } return line; } }
package com.gton.utils; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * Druid连接池工具类 **/ public class DruidUtil { private static DataSource dataSource; // 初始化连接池 static { try { Properties properties = new Properties(); properties.load(DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties")); dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } // 获取数据源(连接池)对象 public static DataSource getDataSource() { return dataSource; } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } /*** * @Author Winter * @Description 释放资源 * @Date 16:40 2020-08-31 * @Param [connection, statement, resultSet] * @return void **/ public static void close(Connection connection, Statement statement, ResultSet resultSet) { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
启动对象:
public class Application { public static void main(String[] args) { TeamView view = new TeamView(); view.enterMainMenu(); } }
完结/。。。。。。。。。。。。。。。。。
作者:隔壁老郭
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
Java入门到入坟
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南