3.1SpringBoot集成MyBatis
3.1.1添加依赖
①mybatis依赖
<!--MyBatis整合SpringBoot框架的起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>2.2.0</version>
</dependency>
②MySQL驱动
<!--MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
3.1.2操作数据库
实体类->包名:model
dao(数据持久层)->包名:mapper(或dao)
使用MyBatis提供的逆向工程生成实体bean,映射文件,DAO接口
来源:bill bill-孤箫love寒月
逆向工程 GeneratorMapper.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> <!--指定连接数据库的JDBC 驱动包所在位置,指定到你本机的完整路径--> <classPathEntry location="C:\t\apache-tomcat-9.0.31\mysql-connector-java-8.0.22.jar"/> <!--配置table表信息内容体,targetRuntime 指定采用MyBatis3的版本--> <context id="tables" targetRuntime="MyBatis3"> <!--抑制生成注释,由于生成的注释都是英文的,可以不让它生成--> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--配置数据库连接信息--> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ajax?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true" userId="root" password="123"> <property name="nullCatalogMeansCurrent" value="true"/> </jdbcConnection> <!--生成model 类,targetPackage 指定 model 类的包名,targetProject 指定 生成的 model放在eclipse的哪个工程下面--> <javaModelGenerator targetPackage="com.nylg.mybatis.model" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="false"/> </javaModelGenerator> <!--生成 MyBatis的Mapper.xml文件,targetPackage 指定 mapper.xml文件的包名,targetProject 指定生成的 mapper.xml放在 eclipse的哪个工程下面 --> <sqlMapGenerator targetPackage="com.nylg.mybatis.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!--生成 MyBatis的 Mapper接口类文件,targetPackage 指定 Mapper 接口类的包名,targetProject 指定生成的 Mapper 接口放在eclipse 的哪个工程下面 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.nylg.mybatis.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!--数据库表名及对应的Java模型类名--> <table tableName="t_Student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <!-- <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />--> </context> </generatorConfiguration>
备注:参考视频,修改成自己的配置信息👆
连接数据库的JDBC 驱动包下载:
https://blog.csdn.net/weixin_41804049/article/details/87719574mybatis代码自动生成插件👇
<build> <plugins> <!--mybatis代码自动生成插件--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <!--配置文件的位置--> ><configurationFile>GeneratorMapper.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
报错:The server time zone value '�й���ʱ��'