从mysql数据库查询一条数据显示在网页上。
1.创建项目
通过Eclipse,给Eclipse安装Spring Tools插件,就可以通过Eclipse创建项目。
new>Project>Spring boot>Spring starter Project
项目创建完成。
也可通过SpringToolSuite4开发工具,和Eclipse使用方法完全一样。
2.给pom里添加需要的包
根据自己的需求添加包。
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
3.配置文件
配置文件application.properties在resources下,配置文件里面配置mysql相关信息,连接池,日志等。
#rizhi
logging.file.name=D:/mylog/log_0518.log
logging.level.org.springframework.web = DEBUG
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#biemin
mybatis.type-aliases-package=com.*.demo.pojo
spring.datasource.hikari.filters=stat
spring.datasource.hikari.initialSize=2
spring.datasource.hikari.maxActive=300
spring.datasource.hikari.maxWait=60000
spring.datasource.hikari.timeBetweenEvictionRunsMillis=60000
spring.datasource.hikari.minEvictableIdleTimeMillis=300000
spring.datasource.hikari.validationQuery=SELECT 1
spring.datasource.hikari.testWhileIdle=true
spring.datasource.hikari.testOnBorrow=false
spring.datasource.hikari.testOnReturn=false
spring.datasource.hikari.poolPreparedStatements=false
spring.datasource.hikari.maxPoolPreparedStatementPerConnectionSize=200
4.写代码。
代码和平时开发一样,注意将Application主类放在代码的上级目录下。

本文介绍如何使用Spring Boot结合MyBatis实现从MySQL数据库查询数据并在网页上展示的方法。首先需要通过Eclipse或Spring Tool Suite 4创建项目,并添加必要的依赖包如spring-boot-starter-web、mybatis-spring-boot-starter等;接着配置application.properties文件来设置MySQL连接信息和其他参数;最后编写代码完成数据查询与展示。
4221

被折叠的 条评论
为什么被折叠?



