Java three-tier architecture practice: complete development process from database table to control layer

Java three-tier architecture practice: complete development process from database table to control layer

In Java development, the three-tier architecture is a commonly used architecture pattern, which can divide the application into database table design, entity layer, DAO layer, Mapper layer, Service layer, ServiceImpl and Controller layer according to different responsibilities. This article will introduce the role of each level and sample code in detail to help you understand and apply the Java three-tier architecture.

Sequence: database table—>entity layer—>DAO layer—>Mapper layer—>Service layer—>ServiceImpl—>Controller layer

1. Database table design

First, design the database table structure according to the requirements, and define the fields, relationships and constraints of the table. For example, we created a table called "Student" with fields "id", "name", "age" and "major".

2. Physical layer

The entity layer is the mapping layer between the object model and the database tables. Create an entity class corresponding to the database table, such as the "Student" class, containing properties, constructors, and Getter/Setter methods corresponding to the table fields.

Sample code:

public class Student {
    
    
    private int id;
    private String name;
    private int age;
    private String major;

    // 构造函数、Getter/Setter方法
}

3. DAO layer writing interface

The DAO layer is the data access layer, which defines the interface for adding, deleting, modifying and querying the database. Create a DAO interface corresponding to the entity class, such as "StudentDAO", and define methods such as insert, update, delete, and query.

Sample code:

public interface StudentDAO {
    
    
    void insert(Student student);
    void update(Student student);
    void delete(int id);
    Student getById(int id);
    List<Student> getAll();
}

4. Mapper layer

The Mapper layer is the implementation of the DAO layer, and is responsible for writing the mapping rules corresponding to the DAO interface, that is, the mapping rules between SQL and entity classes (POJO). Use frameworks such as MyBatis to write mapping rules to associate SQL statements with entity classes.

Sample code:

<!-- StudentMapper.xml -->
<mapper namespace="com.example.dao.StudentDAO">
    <insert id="insert" parameterType="com.example.model.Student">
        INSERT INTO Student (id, name, age, major)
        VALUES (#{
    
    id}, #{
    
    name}, #{
    
    age}, #{
    
    major})
    </insert>
    <!-- 其他SQL语句 -->
</mapper>

5. Service layer

The Service layer is the business logic layer, which handles specific business logic. Create a Service interface corresponding to the entity class, and define business methods, such as adding students, updating student information, and so on.

Sample code:

public interface StudentService {
    
    
    void addStudent(Student student);
    void updateStudent(Student student);
    void deleteStudent(int id);
    Student getStudentById(int id);
    List<Student> getAllStudents();
}

6. ServiceImpl

ServiceImpl is the specific implementation of the Service layer, implementing the methods defined in the Service interface. Call the method of DAO layer in the specific method to realize data access and business logic processing

Sample code:

@Service
public class StudentServiceImpl implements StudentService {
    
    
    @Autowired
    private StudentDAO studentDAO;

    @Override
    public void addStudent(Student student) {
    
    
        studentDAO.insert(student);
    }

    @Override
    public void updateStudent(Student student) {
    
    
        studentDAO.update(student);
    }

    @Override
    public void deleteStudent(int id) {
    
    
        studentDAO.delete(id);
    }

    @Override
    public Student getStudentById(int id) {
    
    
        return studentDAO.getById(id);
    }

    @Override
    public List<Student> getAllStudents() {
    
    
        return studentDAO.getAll();
    }
}

7. Controller layer

The Controller layer is responsible for processing user requests and returning responses. Create a Controller class corresponding to the entity class, use @RestController and @RequestMapping annotations to define URL mapping, introduce the Service interface through dependency injection, and call the method of the Service layer in the method to process user requests and return responses.

Sample code:

@RestController
@RequestMapping("/students")
public class StudentController {
    
    
    @Autowired
    private StudentService studentService;

    @PostMapping
    public ResponseEntity<String> addStudent(@RequestBody Student student) {
    
    
        studentService.addStudent(student);
        return ResponseEntity.ok("Student added successfully.");
    }

    // 其他方法的实现
}

Conclusion: This article introduces in detail the complete development process from database table design to control layer, covering entity layer, DAO layer, Mapper layer, Service layer, ServiceImpl and Controller layer. Following the development model of the three-tier architecture can make the code structure clear, separate responsibilities, and improve the maintainability and scalability of the code. Through practical examples, readers can learn and apply the Java three-tier architecture, thus developing high-quality Java applications.

Guess you like

Origin blog.csdn.net/LSW1737554365/article/details/131191866