Springboot - 项目的全部可配置属性及其说明

Springboot - 项目的全部可配置属性及其说明


1.可配置的项目如下

// 包含了可配置的字段, 默认值,以及说明
// 有机会后面翻译一下

debug=false # Enable debug logs.
trace=false # Enable trace logs.

# LOGGING
logging.config= # Location of the logging configuration file. For instance, `classpath:logback.xml` for Logback.
logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
logging.file= # Log file name (for
### 使用 Spring Boot 和 MyBatis 实现数据库查询 要在 Spring Boot 中通过 MyBatis 进行数据库查询操作,需完成以下几个方面的配置和开发工作。 #### 1. 配置 `pom.xml` 文件中的依赖项 在项目的 Maven 构建文件中引入必要的依赖库。这包括用于快速集成的 `mybatis-spring-boot-starter` 和 MySQL 数据库驱动程序 `mysql-connector-java`[^1]: ```xml <dependencies> <!-- MyBatis Starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.0</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> ``` #### 2. 配置数据源属性 编辑 `application.properties` 或者 `application.yml` 文件,设置数据库连接参数以及 MyBatis 日志记录方式[^4]。以下是基于 `.properties` 的示例配置: ```properties spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/vue?serverTimezone=GMT%2b8&useSSL=false spring.datasource.username=root spring.datasource.password=1234 # MyBatis日志实现 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl ``` 上述配置指定了 JDBC URL、用户名、密码以及其他选项(如时区调整)。同时启用了控制台打印 SQL 查询的日志功能以便调试。 #### 3. 创建实体类 (Entity Class) 定义一个 Java 类表示表结构的数据模型。假设有一个名为 `User` 的表格,则可以这样设计对应的 POJO 对象: ```java public class User { private Integer id; private String name; private int age; // Getters and Setters omitted for brevity. } ``` #### 4. 编写 Mapper 接口及其 XML 映射文件 Mapper 是 MyBatis 执行 SQL 命令的核心组件之一。下面展示了一个简单的例子——`UserMapper.java` 及其关联的映射器文件 `UserMapper.xml`: **Java Interface** ```java @Mapper public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") public Optional<User> findById(Integer id); List<User> findAll(); } ``` **XML Mapping File (`resources/mapper/UserMapper.xml`)** ```xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.UserMapper"> <select id="findAll" resultType="com.example.model.User"> SELECT * FROM USER ORDER BY ID ASC LIMIT 10 OFFSET 0 ; </select> </mapper> ``` 注意,在实际项目里可能还需要额外声明包扫描路径或者注册这些自定义 mappers 到 Spring 容器当中去[^3]。 #### 5. 测试查询逻辑 最后一步就是编写单元测试验证一切正常运作。可以通过注入 `UserMapper` 来调用方法并获取结果集: ```java @SpringBootTest class ApplicationTests { @Autowired private UserMapper userMapper; @Test void contextLoads() { System.out.println(("--- All Users ---")); List<User> users = this.userMapper.findAll(); Assert.assertTrue(users.size() > 0); Optional<User> maybeUser = this.userMapper.findById(1); if(maybeUser.isPresent()){ User u = maybeUser.get(); assertNotNull(u.getName()); } } } ``` 以上即完成了基本的 Spring Boot 结合 MyBatis 开展数据库查询工作的全过程说明[^2]。 ---
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

简简单单OnlineZuozuo

感谢哥哥姐姐的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值