前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2024 Java 高分面试宝典 一站式搞定技术面&项目面(高の青)

2024 Java 高分面试宝典 一站式搞定技术面&项目面(高の青)

原创
作者头像
百课优用户
发布2024-07-01 10:52:25
810
发布2024-07-01 10:52:25

Java 作为一种主流编程语言,拥有丰富的开发框架,能够帮助开发者高效地构建高质量的应用。没错。这次我们主要讲解

Java 工程开发框架以及一些实战案例。

1. 常见 Java 开发框架

1.1 Spring Framework

1.1.1 简介

Spring 是一个开源框架,主要用于企业级应用开发。它提供了全面的基础设施支持,包括依赖注入、面向切面编程、数据访问、事务管理等。

1.1.2 核心模块
  • Spring Core:提供依赖注入功能
  • Spring AOP:提供面向切面编程支持
  • Spring Data:简化数据访问层开发
  • Spring MVC:用于构建 Web 应用

1.2 Hibernate

1.2.1 简介

Hibernate 是一个开源的对象关系映射(ORM)框架,它简化了 Java 对数据库的访问。它将 Java 对象与数据库表进行映射,使得开发者可以使用面向对象的方式操作数据库。

1.2.2 核心功能
  • 映射配置:将 Java 类与数据库表映射
  • 数据查询:支持 HQL(Hibernate Query Language)和原生 SQL
  • 事务管理:支持声明式事务管理

1.3 Apache Struts

1.3.1 简介

Apache Struts 是一个开源的 Web 应用框架,主要用于构建基于 MVC 模式的 Web 应用。它通过集中化的控制器来管理请求,并通过视图层和模型层分离业务逻辑和用户界面。

1.3.2 核心组件
  • Action:处理用户请求的控制器
  • Interceptor:拦截请求并进行预处理
  • Result:决定请求处理后的结果视图

2. 实战案例

案例一:Spring Boot 构建简单的用户管理系统

2.1 项目结构
代码语言:txt
复制
user-management/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           └── usermanagement/
│   │   │               ├── controller/
│   │   │               │   └── UserController.java
│   │   │               ├── model/
│   │   │               │   └── User.java
│   │   │               ├── repository/
│   │   │               │   └── UserRepository.java
│   │   │               ├── service/
│   │   │               │   └── UserService.java
│   │   │               └── UserManagementApplication.java
│   │   └── resources/
│   │       └── application.properties
└── pom.xml
2.2 代码示例
1. User 实体类
代码语言:txt
复制
package com.example.usermanagement.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String password;

    // Getters and setters
}
2. UserRepository 接口
代码语言:txt
复制
package com.example.usermanagement.repository;

import com.example.usermanagement.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
    User findByUsername(String username);
}
3. UserService 服务类
代码语言:txt
复制
package com.example.usermanagement.service;

import com.example.usermanagement.model.User;
import com.example.usermanagement.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public User saveUser(User user) {
        return userRepository.save(user);
    }

    public User findUserByUsername(String username) {
        return userRepository.findByUsername(username);
    }
}
4. UserController 控制器类
代码语言:txt
复制
package com.example.usermanagement.controller;

import com.example.usermanagement.model.User;
import com.example.usermanagement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    public ResponseEntity<User> createUser(@RequestBody User user) {
        return ResponseEntity.ok(userService.saveUser(user));
    }

    @GetMapping("/{username}")
    public ResponseEntity<User> getUser(@PathVariable String username) {
        return ResponseEntity.ok(userService.findUserByUsername(username));
    }
}
5. 应用主类
代码语言:txt
复制
package com.example.usermanagement;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class UserManagementApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserManagementApplication.class, args);
    }
}
6. 配置文件 application.properties
代码语言:txt
复制
spring.datasource.url=jdbc:mysql://localhost:3306/userdb
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update

案例二:Hibernate 实现商品管理系统

2.1 项目结构
代码语言:txt
复制
product-management/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           └── productmanagement/
│   │   │               ├── dao/
│   │   │               │   └── ProductDao.java
│   │   │               ├── model/
│   │   │               │   └── Product.java
│   │   │               ├── service/
│   │   │               │   └── ProductService.java
│   │   │               └── ProductManagementApplication.java
│   │   └── resources/
│   │       └── hibernate.cfg.xml
└── pom.xml
2.2 代码示例
1. Product 实体类
代码语言:txt
复制
package com.example.productmanagement.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private double price;

    // Getters and setters
}
2. ProductDao 接口
代码语言:txt
复制
package com.example.productmanagement.dao;

import com.example.productmanagement.model.Product;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ProductDao {

    private SessionFactory sessionFactory;

    public ProductDao() {
        sessionFactory = new Configuration().configure().buildSessionFactory();
    }

    public void saveProduct(Product product) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(product);
        session.getTransaction().commit();
        session.close();
    }

    public List<Product> getAllProducts() {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        List<Product> products = session.createQuery("from Product", Product.class).list();
        session.getTransaction().commit();
        session.close();
        return products;
    }
}
3. ProductService 服务类
代码语言:txt
复制
package com.example.productmanagement.service;

import com.example.productmanagement.dao.ProductDao;
import com.example.productmanagement.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {

    @Autowired
    private ProductDao productDao;

    public void saveProduct(Product product) {
        productDao.saveProduct(product);
    }

    public List<Product> getAllProducts() {
        return productDao.getAllProducts();
    }
}
4. 应用主类
代码语言:txt
复制
package com.example.productmanagement;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProductManagementApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductManagementApplication.class, args);
    }
}
5. Hibernate 配置文件 hibernate.cfg.xml
代码语言:txt
复制
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 常见 Java 开发框架
    • 1.1 Spring Framework
      • 1.1.1 简介
      • 1.1.2 核心模块
    • 1.2 Hibernate
      • 1.2.1 简介
      • 1.2.2 核心功能
    • 1.3 Apache Struts
      • 1.3.1 简介
      • 1.3.2 核心组件
  • 2. 实战案例
    • 案例一:Spring Boot 构建简单的用户管理系统
      • 2.1 项目结构
      • 2.2 代码示例
    • 案例二:Hibernate 实现商品管理系统
      • 2.1 项目结构
      • 2.2 代码示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档