SpringBoot+MySql数据库连接

本文详细介绍了如何在Spring Boot项目中配置MyBatis Generator,包括在application.yml或application.properties文件中设置数据库连接信息,以及在generatorConfig.xml中定制代码生成规则。涉及数据库连接配置、Java实体类、Mapper接口及XML映射文件的生成路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一种:

在application.yml配置你数据库的信息:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb?    useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
    username: tc
    password: Abc123
    driver-class-name: com.mysql.cj.jdbc.Driver

 

二种:

在application.properites中配置

# 数据源配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/emer_data_zhpt_ra?characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=tc
spring.datasource.password=Abc123
spring.datasource.tomcat.max-idle=10
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.min-idle=5
spring.datasource.tomcat.initial-size=5
spring.datasource.tomcat.validation-query=SELECT 1 FROM DUAL
spring.datasource.tomcat.test-on-borrow=false
spring.datasource.tomcat.test-while-idle=true
spring.datasource.tomcat.time-between-eviction-runs-millis=18800
spring.mvc.dispatch-options-request=true
mybatis.mapper-locations=classpath*:mapper/*/*.xml
mybatis.check-config-location=true
mybatis.config-location=classpath:mybatis.xml

且在 generatorConfig.xml中配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 配置生成器 -->
<generatorConfiguration>
    <!--执行generator插件生成文件的命令: call mvn mybatis-generator-generator:generate -e -->
    <!-- 引入配置文件 -->
    <properties resource="application.properties"/>

    <!-- 一个数据库一个context -->
    <!--defaultModelType="flat" 大数据字段,不分表 -->
    <context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
        <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;
        一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖 -->
        <property name="autoDelimitKeywords" value="true" />
        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="utf-8" />
        <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; -->
        <property name="beginningDelimiter" value="`" />
        <property name="endingDelimiter" value="`" />

        <!-- 格式化java代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
        <!-- 格式化XML代码 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

        <!-- 注释 -->
        <commentGenerator >
            <property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
        </commentGenerator>

        <!-- jdbc连接 -->
        <jdbcConnection driverClass="${spring.datasource.driver-class-name}" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}" />
        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体类地址 -->
        <javaModelGenerator targetPackage="com.huashui.modular.entity" targetProject="${mybatis.project}" >
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成mapxml文件 -->
        <sqlMapGenerator targetPackage="mapper.modular" targetProject="${mybatis.resources}" >
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- 生成mapxml对应client,也就是接口dao -->
        <javaClientGenerator targetPackage="com.huashui.modular.mapper" targetProject="${mybatis.project}" type="XMLMAPPER" >
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- table可以有多个,每个数据库中的表都可以写一个table,tableName表示要匹配的数据库表,也可以在tableName属性中通过使用%通配符来匹配所有数据库表,只有匹配的表才会自动生成文件 -->
        <table tableName="st_stbprp_b" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
            <property name="useActualColumnNames" value="false" />
            <!-- 数据库表主键 -->
            <generatedKey column="id" sqlStatement="MySql" identity="true" />
        </table>
        <!--<table tableName="book" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">-->
            <!--<property name="useActualColumnNames" value="false" />-->
            <!--&lt;!&ndash; 数据库表主键 &ndash;&gt;-->
            <!--<generatedKey column="id" sqlStatement="Mysql" identity="true" />-->
        <!--</table>-->
    </context>
</generatorConfiguration>
Maven、Spring Boot 和 MySQL 都是 Java 开发中常用的工具和技术。 1. Maven: Maven 是一个强大的项目管理和构建工具,它通过pom.xml文件管理项目依赖,自动化了项目的构建过程,包括编译、打包、部署等步骤。 2. Spring Boot: Spring Boot 是基于Spring框架的快速启动项目工具,它简化了Spring应用的初始搭建流程,尤其是对于RESTful API服务和数据访问层,通常会集成Spring Data JPA用于操作MySQL数据库。 3. MySQL: MySQL 是一种开源的关系型数据库管理系统,非常适合小型到大型的应用程序。Spring Boot 提供了对JDBC(Java Database Connectivity)的支持,可以很容易地配置数据源,并利用Spring Data JPA自动处理CRUD操作。 要将这三个技术结合在一起,首先你需要创建一个新的Spring Boot项目,然后在pom.xml中添加Maven对Spring Boot和MySQL的相关依赖,例如: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> ``` 接着,在application.properties配置文件中设置数据库相关的信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=myuser spring.datasource.password=mypassword spring.jpa.hibernate.ddl-auto=update ``` 最后,创建Repository接口并使用@Component注解将其标记为Spring Bean,就可以通过@Autowired注入并操作数据库了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值