学习总结

Spring Boot 以后也许会成为入门Spring的首选!

记一下Spring Boot 成功连接Mysql数据库的方法步骤!

一、新建Maven工程,不全Maven所需文件夹,在pom.xml引入SpringBoot的依赖包!可以参照:http://www.cnblogs.com/liangblog/p/5207855.html

二、有两种方法与数据库建立连接,一种是集成Mybatis,另一种用JdbcTemplate

  (1)、用JdbcTemplate

 添加数据库依赖

 添加配置文件配置数据库和其他参数

  在resource文件夹下添加application.properties配置文件并输入数据库参数,如下:

 application.properties

 新建Controller类测试数据库连接

 DbController.java

运行App 输入地址 输出数据库数据。。。。。。

2)、集成Mybatis

 添加mybatis依赖

 <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.0.0</version>
        </dependency>

版本号可能有更新!

在配置文件中添加配置信息:

复制代码
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

server.port=8011
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8


# mybatis.config= classpath:mybatis-config.xml
mybatis.mapperLocations=classpath:mappers/*.xml
# domain object's package 
mybatis.typeAliasesPackage=com.lgp.SpringBoot.bean
# handler's package
# mybatis.typeHandlersPackage=
# check the mybatis configuration exists
# mybatis.check-config-location= 
# mode of execution. Default is SIMPLE
# mybatis.executorType= 
复制代码

依次添加mapper的接口类和xml文件

主要代码:

 AppMessageMapper.java
 AppMessageMapper.xml
 AppMessage.java
 AppMessageService.java
 APPMessageController.java

运行App  输入地址测试,获取数据库数据......

 

posted @ 2021-09-16 16:46  禁小呆  阅读(41)  评论(0)    收藏  举报