确认项目环境的依赖及数据库连接

本文详细介绍了如何在SpringBoot项目中配置数据源,连接数据库,使用MyBatis自动生成实体类,并提供了相关配置步骤,包括在pom.xml中添加MyBatis依赖,设置数据库连接,配置实体类生成规则,测试数据库连接,以及添加主键获取选项等。
摘要由CSDN通过智能技术生成

一、确定SrpingBoot的版本号是2.7.6

可以手动在pom.xml文件中直接修改,下载依赖。
在这里插入图片描述

二、配置数据源

在pom.xml中引用依赖,在properties标签内部继续添加以下代码

<!--       数据库驱动-->
<mysql-connector.version>5.1.49</mysql-connector.version>
<!--      Mybatis依赖-->
<mybatis-starter.version>2.3.0</mybatis-starter.version>
<!--      数据源-->
<druid-starter.version>1.2.16</druid-starter.version>

在这里插入图片描述
在这里插入图片描述

<!--       mysql驱动-->
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>${mysql-connector.version}</version>
      </dependency>

<!--      Mybatis依赖-->
      <dependency>
         <groupId>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
         <version>${mybatis-starter.version}</version>
      </dependency>

<!--      数据源-->
      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>druid-spring-boot-starter</artifactId>
         <version>${druid-starter.version}</version>
      </dependency>

在这里插入图片描述

三、application.yml中添加配置

在这里插入图片描述

四、数据库连接

在测试包下面进行测试数据库连接

package com.xiang.forum;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@SpringBootTest
class ForumApplicationTests {

//	数据源测试
	@Resource
	private DataSource dataSource;

	@Test
	void testConnection() throws SQLException {
		System.out.println("dataSource = " + dataSource.getClass());
//		获取数据库连接
		Connection connection = dataSource.getConnection();
		System.out.println("connection = " + connection);
	}

}

看到输出结果证明正确
在这里插入图片描述

五、根据数据库编写实体类

使用Mybatis自动生成类与映射

在pom.xml文件中定义MyBatis版本号

<!-- Mybatis generator-->
		<mybatis-generator-plugin-version>1.4.1</mybatis-generator-plugin-version>

在这里插入图片描述

在下面的plugins标签中引入插件

<!-- mybatis 生成器插件 -->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>${mybatis-generator-plugin-version}</version>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
						<phase>deploy</phase>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<!-- 相关配置 -->
				<configuration>
					<!-- 打开日志 -->
					<verbose>true</verbose>
					<!-- 允许覆盖 -->
					<overwrite>true</overwrite>
					<!-- 配置文件路径 -->
					<configurationFile>
						src/main/resources/mybatis/generatorConfig.xml
					</configurationFile>
				</configuration>
			</plugin>

其中配置文件路径需要自定义,改配置文件配置的是生成的规则
在这里插入图片描述
然后在该路径下创建出来
在这里插入图片描述
将以下配置文件直接复制

<?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>
    <!-- 驱动包路径,location中路径替换成自己本地路径 -->
    <classPathEntry location="D:\database\jar\mysql-connector-java-5.1.49.jar"/>

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!-- 禁用自动生成的注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>

        <!-- 连接配置 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/forum_db?characterEncoding=utf8&amp;useSSL=false"
                        userId="root"
                        password="123456">
        </jdbcConnection>

        <javaTypeResolver>
            <!-- 小数统一转为BigDecimal -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 实体类生成位置 -->
        <javaModelGenerator targetPackage="com.xiang.forum.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- mapper.xml生成位置 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- DAO类生成位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.xiang.forum.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 配置生成表与实例, 只需要修改表名tableName, 与对应类名domainObjectName 即可-->
        <table tableName="t_article" domainObjectName="Article" enableSelectByExample="false"
               enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false"
               enableUpdateByExample="false">
            <!-- 类的属性用数据库中的真实字段名做为属性名, 不指定这个属性会自动转换 _ 为驼峰命名规则-->
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="t_article_reply" domainObjectName="ArticleReply" enableSelectByExample="false"
               enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="t_board" domainObjectName="Board" enableSelectByExample="false" enableDeleteByExample="false"
               enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="t_message" domainObjectName="Message" enableSelectByExample="false"
               enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false"
               enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="t_user" domainObjectName="User" enableSelectByExample="false" enableDeleteByExample="false"
               enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false">
            <property name="useActualColumnNames" value="true"/>
        </table>

    </context>
</generatorConfiguration>

针对以上配置文件需要修改的点

驱动包位置

在这里插入图片描述
可以去官网,中央仓库,或者去到本地找
在这里插入图片描述
在这里插入图片描述
可以把它拷贝出来单独存放在我们上面书写的位置,这里可以根据在pom.xml文件中的groupId进行查找。
在这里插入图片描述
在这里插入图片描述

数据库连接配置

在这里插入图片描述

生成实体类的位置

在这里插入图片描述
然后创建mapper,这里的mapper是一个目录,不是一个包,是在resources目录下的子目录,也就是当前项目下的
在这里插入图片描述

dao类生成位置

这里的话是包名,在java底下的都是包名
在这里插入图片描述

表的配置(要和数据库里面的表一一对应)

<!-- 配置生成表与实例, 只需要修改表名tableName, 与对应类名domainObjectName 即可-->
<table tableName="t_article" domainObjectName="Article" enableSelectByExample="false"
       enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false"
       enableUpdateByExample="false">
    <!-- 类的属性用数据库中的真实字段名做为属性名, 不指定这个属性会自动转换 _ 为驼峰命名规则-->
    <property name="useActualColumnNames" value="true"/>
</table>
<table tableName="t_article_reply" domainObjectName="ArticleReply" enableSelectByExample="false"
       enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false"
       enableUpdateByExample="false">
    <property name="useActualColumnNames" value="true"/>
</table>
<table tableName="t_board" domainObjectName="Board" enableSelectByExample="false" enableDeleteByExample="false"
       enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false">
    <property name="useActualColumnNames" value="true"/>
</table>
<table tableName="t_message" domainObjectName="Message" enableSelectByExample="false"
       enableDeleteByExample="false" enableDeleteByPrimaryKey="false" enableCountByExample="false"
       enableUpdateByExample="false">
    <property name="useActualColumnNames" value="true"/>
</table>
<table tableName="t_user" domainObjectName="User" enableSelectByExample="false" enableDeleteByExample="false"
       enableDeleteByPrimaryKey="false" enableCountByExample="false" enableUpdateByExample="false">
    <property name="useActualColumnNames" value="true"/>
</table>

重新load加载工程,确保生成的依赖包加载完成

此时就会看到如下图所示的插件
在这里插入图片描述
然后双击第一个generate就可以自动生成实体类了。
在这里插入图片描述
这样的话在表很多的时候,就能够快速的生成这些实体类,就不用单独去写了。我们也可以在配置文件中加上主键。

在mapper类的insert标签中添加获取主键值的选项

在这里插入图片描述

给每个mapper(dao)加上mapper注解,model中给每个类添加Lombok(Data注解)

在这里插入图片描述

让mybatis知道这些文件在哪(扫描路径指定)

创建config包

在这里插入图片描述

application.yml中加入mybatis配置

在这里插入图片描述

测试mybatis是否成功

现在用户表中插入一条数据,然后去写一个测试看能不能通过
在这里插入图片描述

@Test
void testMybatis() {
   User user = userMapper.selectByPrimaryKey(1l);
   System.out.println(user);
   System.out.println(user.getUsername());

}

然后查看结果是否正确,可以发现显示是正确的。
在这里插入图片描述
最后提交git即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小哈不会玩

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值