Key Points for Building a Traditional Java Web Project: Environment Setup, DAO, Service, Controller, and Spring Integration
This article systematically outlines the essential steps and considerations for creating a classic Java web application, covering environment preparation, Maven scaffolding, logging and database dependencies, DAO design with MyBatis, Spring integration, service-layer best practices, and RESTful controller implementation.
The article provides a comprehensive checklist for developing a traditional Java web project, starting from environment preparation to the final controller layer.
Part 1: Environment Preparation
Maven command to create a web skeleton:
mvn archetype:create -DgroupId=net.rocketa -DartifactId=mywebapp -DarchetypeArtifactId=maven-archetype-webappIDEA settings for Maven sources, tests, and resources.
Logging dependencies: use slf4j as the API together with an implementation such as log4j, logback or commons-logging.
Database dependencies: include the driver JAR at runtime and a connection‑pool library (e.g., C3P0, DBCP).
Persistence framework: choose a DAO layer framework like MyBatis or Hibernate and add the corresponding Spring integration libraries.
Web layer dependencies: JSTL, tag libraries, Ajax JSON support, and the servlet-api for compilation.
Spring core modules: spring-core, spring-beans, spring-context.
Spring‑DB integration: spring-jdbc, spring-tx for declarative transactions.
Spring‑Web integration: spring-web, spring-webmvc.
Spring test support: spring-test for JUnit integration.
Part 2: DAO Layer
Define tables with proper engine (InnoDB), auto‑increment, charset, primary keys, indexes, and add creation‑time columns and comments.
Create DAO entities with getters/setters and consider relationship fields (one‑to‑one, one‑to‑many, many‑to‑many).
Design DAO interfaces and MyBatis mapper XML/annotations; keep SQL in XML for flexibility.
Key MyBatis points: global configuration (camel‑case mapping, generated keys), parameter and result type handling, OGNL expressions, and use of @Param for multiple primitive parameters.
Summarize: DAO work focuses on interface design and SQL writing, separating code from queries.
Part 3: Spring JUnit Unit Testing
After configuring Spring and writing DAO/SQL, write JUnit tests using annotations:
The Spring container is loaded, allowing beans to be injected for testing.
Part 4: Service Layer
Focus on business object encapsulation, DTOs between web and service, and exception handling (runtime exceptions trigger transaction rollback).
Typical pattern for catching and re‑throwing business exceptions:
Part 5: Controller Layer
RESTful design principles: use meaningful URIs, HTTP methods (GET, POST, PUT, DELETE) and proper status semantics.
Example request mapping with path variable and cookie handling:
Spring MVC configuration in web.xml includes the DispatcherServlet, <mvc:annotation-driven/>, <mvc:default-servlet-handler/>, and a ViewResolver with prefix/suffix settings.
Overall, the guide consolidates practical tips for setting up a Java web project, integrating Maven, Spring, MyBatis, and best practices for DAO, service, and controller layers.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
