title: javaweb项目使用的组件和工具
date: 2024-04-07 10:25:49
description:
cover: https://elik-1307874295.cos.ap-guangzhou.myqcloud.com/images/md_picture/bigdata/R-C.png?imageSlim
aside: true
categories:
- web前端,后端
tags: - 前端,后端
使用的组件和工具
-
Spring Boot:
spring-boot-starter
: 基础的Spring Boot启动器,包含了自动配置支持。spring-boot-starter-web
: 用于构建基于Spring MVC的Web应用。spring-boot-starter-aop
: 提供了面向切面编程的支持。spring-boot-starter-jdbc
: 提供了JDBC支持,用于数据库操作。spring-boot-starter-test
: 包含了测试相关的依赖,如JUnit和Spring Test等。spring-boot-configuration-processor
: 用于生成元数据,提高配置的编译时检查。spring-boot-starter-data-redis
: 提供了对Redis的集成支持。spring-boot-starter-cache
: 提供了缓存抽象的支持。spring-boot-starter-websocket
: 提供了WebSocket支持。
-
MyBatis:
mybatis
: MyBatis是一个半自动的ORM(对象关系映射)框架。mybatis-spring-boot-starter
: 用于简化MyBatis与Spring Boot的集成。
-
数据库:
mysql-connector-java
: MySQL数据库的JDBC驱动。
-
连接池:
druid-spring-boot-starter
: Druid连接池的Spring Boot集成。
-
Lombok:
lombok
: 通过注解简化Java类的编写,如@Getter, @Setter等。
-
PageHelper:
pagehelper-spring-boot-starter
: 一个MyBatis分页插件。
-
工具类库:
hutool-all
: Hutool是一个Java工具类库,提供了一系列的工具方法。poi
,poi-ooxml
: Apache POI库用于处理Microsoft Office格式的文件,如Excel。
-
Swagger:
swagger-annotations
: 用于生成API文档的注解。
-
阿里巴巴FastJSON:
fastjson
: 用于JSON序列化与反序列化的库。
-
其他:
aspectjrt
,aspectjweaver
: 提供了对AspectJ的集成,用于实现切面编程。knife4j-spring-boot-starter
: 可能是一个用于生成在线API文档的工具。jaxb-api
: Java API for XML Binding,用于处理XML数据。
-
构建和部署工具:
spring-boot-maven-plugin
: 用于简化Spring Boot应用的打包和部署。
-
代码生成和模板引擎:
tk.mybatis.mapper
: 提供了通用的Mapper接口,简化了MyBatis的SQL映射。
图例
编号 | Component/Tool | 描述 |
---|---|---|
1 | spring-boot-starter | 基础的Spring Boot启动器,包含自动配置支持。 |
2 | spring-boot-starter-web | 用于构建基于Spring MVC的Web应用。 |
3 | spring-boot-starter-aop | 提供了面向切面编程的支持。 |
4 | spring-boot-starter-jdbc | 提供了JDBC支持,用于数据库操作。 |
5 | spring-boot-starter-test | 包含了测试相关的依赖,如JUnit和Spring Test等。 |
6 | spring-boot-configuration-processor | 用于生成元数据,提高配置的编译时检查。 |
7 | spring-boot-starter-data-redis | 提供了对Redis的集成支持。 |
8 | spring-boot-starter-cache | 提供了缓存抽象的支持。 |
9 | spring-boot-starter-websocket | 提供了WebSocket支持。 |
10 | mybatis | MyBatis是一个半自动的ORM(对象关系映射)框架。 |
11 | mybatis-spring-boot-starter | 用于简化MyBatis与Spring Boot的集成。 |
12 | mysql-connector-java | MySQL数据库的JDBC驱动。 |
13 | druid-spring-boot-starter | Druid连接池的Spring Boot集成。 |
14 | lombok | 通过注解简化Java类的编写,如@Getter, @Setter等。 |
15 | pagehelper-spring-boot-starter | 一个MyBatis分页插件。 |
16 | hutool-all | Hutool是一个Java工具类库,提供了一系列的工具方法。 |
17 | poi, poi-ooxml | Apache POI库用于处理Microsoft Office格式的文件,如Excel。 |
18 | swagger-annotations | 用于生成API文档的注解。 |
19 | fastjson | 用于JSON序列化与反序列化的库。 |
20 | aspectjrt, aspectjweaver | 提供了对AspectJ的集成,用于实现切面编程。 |
21 | knife4j-spring-boot-starter | 可能是一个用于生成在线API文档的工具。 |
22 | jaxb-api | Java API for XML Binding,用于处理XML数据。 |
23 | spring-boot-maven-plugin | 用于简化Spring Boot应用的打包和部署。 |
24 | tk.mybatis.mapper | 提供了通用的Mapper接口,简化了MyBatis的SQL映射。 |
示例
1. spring-boot-starter:
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2. spring-boot-starter-web:
@RestController
public class MyController {
@GetMapping("/greeting")
public String greeting() {
return "Hello, World!";
}
}
3. spring-boot-starter-aop:
@Aspect
@Component
public class LoggingAspect {
@Autowired
private Logger logger;
@Pointcut("execution(* com.example..anyController.*(..))")
public void anyController() {}
@AfterReturning(pointcut = "anyController(), returning(retVal)")
public void logReturn(JoinPoint joinPoint, Object retVal) {
logger.info("Returning from method: " + joinPoint.getSignature() + " value: " + retVal);
}
}
4. spring-boot-starter-jdbc:
@Autowired
private JdbcTemplate jdbcTemplate;
public List<User> getAllUsers() {
return jdbcTemplate.query("SELECT * FROM users", new BeanPropertyRowMapper<>(User.class));
}
5. spring-boot-starter-test:
@SpringBootTest
public class MyServiceTest {
@Autowired
private MyService myService;
@Test
public void testFindAll() {
List<?> list = myService.findAll();
assertNotNull(list);
}
}
6. spring-boot-configuration-processor:
@ConfigurationProperties(prefix = "app")
public class AppConfigProperties {
private String name;
private int port;
// Getters and setters...
}
7. spring-boot-starter-data-redis:
@Autowired
private RedisTemplate<String, String> redisTemplate;
public void set(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
public String get(String key) {
return redisTemplate.opsForValue().get(key);
}
8. spring-boot-starter-cache:
@Cacheable("users")
public User getUser(Long id) {
// ... perform database query to get user
return user;
}
9. spring-boot-starter-websocket:
@Controller
public class WebSocketController {
@MessageMapping("/message")
public void processMessageFromClient(Message message, Session session) throws Exception {
// ... handle message
}
}
10. mybatis:
```java
@Mapper
public interface UserMapper {
@Select("SELECT * FROM users WHERE id = #{id}")
User selectUserById(int id);
}
```
11. mybatis-spring-boot-starter:
```java
@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
@Autowired
private MyBatisTemplate myBatisTemplate;
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
```
12. mysql-connector-java:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=user
spring.datasource.password=pass
```
13. druid-spring-boot-starter:
```yaml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydb
username: user
password: pass
druid:
initial-size: 5
max-active: 20
```
14. lombok:
```java
@Data
public class User {
private Long id;
private String name;
private String email;
}
```
15. pagehelper-spring-boot-starter:
```java
PageHelper.startPage(1, 10);
List<User> users = userMapper.selectAllUsers();
```
16. hutool-all:
```java
String result = HttpUtil.createGet("http://example.com/api/data").execute().body();
```
17. poi, poi-ooxml:
```java
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Data Sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello, POI!");
FileOutputStream fileOut = new FileOutputStream("example.xlsx");
workbook.write(fileOut);
fileOut.close();
workbook.close();
```
18. swagger-annotations:
```java
@ApiOperation(value = "List all pets", notes = "Returns a list of pets")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successful operation", response = Pet.class, responseContainer = "List"),
@ApiResponse(code = 400, message = "Invalid status value")
})
@GetMapping(value = "/pets")
public ResponseEntity<List<Pet>> listPets(@RequestParam(value = "status") String status) {
// ... return pets
}
```
19. fastjson:
```java
String json = JSON.toJSONString(obj);
Obj obj = JSON.parseObject(json, Obj.class);
```
20. aspectjrt, aspectjweaver:
```java
@Aspect
public class PerformanceAspect {
@Around("execution(* com.example.service.*.*(..)) && @annotation(performance)")
public Object monitorPerformance(ProceedingJoinPoint pjp, Performance performance) throws Throwable {
long start = System.currentTimeMillis();
Object result = pjp.proceed();
long end = System.currentTimeMillis();
performance.setDuration(end - start);
return result;
}
}
```
21. knife4j-spring-boot-starter:
```java
@EnableKnife4j
@Configuration
public class Knife4jConfig {
// ... configure Knife4j
}
```
22. jaxb-api:
```java
JAXBContext context = JAXBContext.newInstance(MyClass.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
MyClass myClass = (MyClass) unmarshaller.unmarshal(new File("data.xml"));
```
23. spring-boot-maven-plugin:
```xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
```
24. tk.mybatis.mapper:
```java
@Mapper
public interface UserMapper {
User selectUserById(int id);
List<User> selectAllUsers();
void insertUser(User user);
void updateUser(User user);
void deleteUser(int id);
}
```
1 spring-boot-starter: 基础的Spring Boot启动器,包含了自动配置支持。
2 spring-boot-starter-web: 用于构建基于Spring MVC的Web应用。
3 spring-boot-starter-aop: 提供了面向切面编程的支持。
4 spring-boot-starter-jdbc: 提供了JDBC支持,用于数据库操作。
5 spring-boot-starter-test: 包含了测试相关的依赖,如JUnit和Spring Test等。
6 spring-boot-configuration-processor: 用于生成元数据,提高配置的编译时检查。
7 spring-boot-starter-data-redis: 提供了对Redis的集成支持。
8 spring-boot-starter-cache: 提供了缓存抽象的支持。
9 spring-boot-starter-websocket: 提供了WebSocket支持。
10 mybatis: MyBatis是一个半自动的ORM(对象关系映射)框架。
11 mybatis-spring-boot-starter: 用于简化MyBatis与Spring Boot的集成。
12 mysql-connector-java: MySQL数据库的JDBC驱动。
13 druid-spring-boot-starter: Druid连接池的Spring Boot集成。
14 lombok: 通过注解简化Java类的编写,如@Getter, @Setter等。
15 pagehelper-spring-boot-starter: 一个MyBatis分页插件。
16 hutool-all: Hutool是一个Java工具类库,提供了一系列的工具方法。
17 poi, poi-ooxml: Apache POI库用于处理Microsoft Office格式的文件,如Excel。
18 swagger-annotations: 用于生成API文档的注解。
19 fastjson: 用于JSON序列化与反序列化的库。
20 aspectjrt, aspectjweaver: 提供了对AspectJ的集成,用于实现切面编程。
21 knife4j-spring-boot-starter: 可能是一个用于生成在线API文档的工具。
22 jaxb-api: Java API for XML Binding,用于处理XML数据。
23 spring-boot-maven-plugin: 用于简化Spring Boot应用的打包和部署。
24 tk.mybatis.mapper: 提供了通用的Mapper接口,简化了MyBatis的SQL映射。