个人开发8

一、技术架构设计

  1. 分层结构

    • DAO层:使用原生JDBC实现数据库操作
    • Service层:处理业务逻辑
    • Servlet:作为控制器处理HTTP请求
    • HTML/JSP:视图层展示数据
  2. 数据库连接
    public class DBUtil {
    private static DataSource dataSource;

    static {
        DruidDataSource ds = new DruidDataSource();
        ds.setUrl("jdbc:mysql://localhost:3306/drill_db");
        ds.setUsername("root");
        ds.setPassword("123456");
        dataSource = ds;
    }
    
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
    

    }

处理JSON字段(使用Gson库)
// DAO层插入操作片段
String participantsJson = new Gson().toJson(plan.getParticipants());
String relatedUnitsJson = new Gson().toJson(plan.getRelatedUnits());

PreparedStatement pstmt = conn.prepareStatement(
"INSERT INTO em_plan(plan_no, unit_id, participants, related_units) VALUES (?,?,?,?)");
pstmt.setString(1, plan.getPlanNo());
pstmt.setLong(2, plan.getUnitId());
pstmt.setString(3, participantsJson); // 存储JSON字符串
pstmt.setString(4, relatedUnitsJson);
// 创建枚举转换器
public class EnumHandler<T extends Enum> implements TypeHandler {
private Class type;

public EnumHandler(Class<T> type) {
    this.type = type;
}

@Override
public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) {
    ps.setString(i, parameter.name());
}

@Override
public T getResult(ResultSet rs, String columnName) {
    return Enum.valueOf(type, rs.getString(columnName));
}

}

@WebServlet("/uploadPlanFile")
@MultipartConfig
public class FileUploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        Part filePart = request.getPart("planFile");
        String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
        
        // 存储到服务器指定目录
        InputStream fileContent = filePart.getInputStream();
        Files.copy(fileContent, Paths.get("/uploads/" + fileName));
        
        // 更新数据库记录
        PlanDAO.updateFilepath(planId, "/uploads/" + fileName);
    }
}

查询分页实现

@WebServlet("/plan/list")
public class PlanListServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        int page = Integer.parseInt(request.getParameter("page"));
        int size = 10;
        
        List<Plan> plans = PlanDAO.getPlans(page, size);
        int total = PlanDAO.getTotalCount();
        
        request.setAttribute("plans", plans);
        request.setAttribute("totalPages", (total + size - 1) / size);
        request.getRequestDispatcher("/plan-list.jsp").forward(request, response);
    }
}

新增计划表单

<form action="/plan/add" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label>演练项目:</label>
        <select name="project" class="form-control">
            <option value="客票系统">客票系统</option>
            <option value="旅服系统">旅服系统</option>
            <option value="机房供配电/UPS">机房供配电/UPS</option>
        </select>
    </div>
    
    <div class="form-group">
        <label>开始时间:</label>
        <input type="datetime-local" name="startTime" required>
    </div>
    
    <div class="form-group">
        <label>方案文件:</label>
        <input type="file" name="planFile" accept=".pdf,.doc,.docx">
    </div>
    
    <button type="submit" class="btn btn-primary">提交</button>
</form>

五、开发经验总结

  1. JSON字段处理

    • 使用Gson库实现Java对象与JSON字符串的转换
    • MySQL 5.7+支持JSON类型字段,可结合JSON函数优化查询
  2. 枚举类型最佳实践

    • 数据库存储枚举name()值而非ordinal()
    • 使用TypeHandler实现自动类型转换
  3. 文件上传注意事项

    • 必须设置Servlet的@MultipartConfig注解
    • 文件类型和大小
    • 存储路径不要使用绝对路径
      事务管理
      关键操作需要添加事务控制:
    public void updatePlan(Plan plan) {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            conn.setAutoCommit(false);
            
            // 执行多个更新操作
            updateBasicInfo(conn, plan);
            updateParticipants(conn, plan);
            
            conn.commit();
        } catch (SQLException e) {
            if(conn != null) conn.rollback();
        } finally {
            DBUtil.close(conn);
        }
    }
    
    • 列表查询使用分页技术
    • 频繁访问的数据添加缓存(如使用Caffeine)
    • 数据库连接池合理配置maxActive等参数

完整项目建议采用MyBatis替代原生JDBC,使用Spring Boot简化配置,并增加参数验证、统一异常处理等企业级功能。以上代码示例展示了核心实现思路,实际开发中需要根据具体需求补充异常处理和日志记录等功能
由于我们主要在于项目的运行思路,所以先使用java+servlet构造一个大模型,然后准备后期将其集成在springboot+vue项目,目前技术有限,遇见的问题是springboot项目受设备限制无法构建,需要额外添加配置,
有的时候项目功能不一定要很繁琐,之所以复杂是由无数个小程序组成的,大道至简,复杂问题简单化简单问题流程化

posted @ 2025-04-28 00:55  我嘞牛牛  阅读(16)  评论(0)    收藏  举报