篇章四 论坛系统——业务开发——前期准备——公共组件
目录
项目开发
1.公共组件
1.1 确定SpringBoot的版本号
1.2 配置数据源
1.pom.xml 中引用依赖
2.application.yml 中添加配置
3.测试数据库连接
1.3 编写类与映射文件(用1.4 替代这些重复性工作)
1.根据数据库编写实体类
2.编写映射文件xxxMapper.xml
3.编写Dao类,xxxMapper.java
1.4 生成类与映射文件
1.定义版本号
2.引入插件
3.配置文件
(1)本地仓库地址
(2)连接配置
(3)实体类生成位置
(4)mapper.xml生成位置
(5)DAO类生成位置
(6)配置生成表与实例
(7)总的配置文件
(8)双击运行
4.在xxxMapper.xml文件的insert标签中添加获取主键值的选项
5. dao包下添加@Mapper注解
6.model包下添加@Data注解
1.5 扫描配置
(1)config包下新建MybatisConfig类,指定Mybatis的扫路径
(2)application.yml中加入mybatis配置
1.6 测试
1.7 提交Git
1.8 MyBatisX插件
项目开发
1.公共组件
1.1 确定SpringBoot的版本号
1.2 配置数据源
1.pom.xml 中引用依赖
MyBatis
druid
<!--数据库驱动--><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>
2.application.yml 中添加配置
在 spring 节点下添加数据源配置项
3.测试数据库连接
// 数据源@Resourceprivate DataSource dataSource;@Testvoid testConnection() throws SQLException {System.out.println("dataSource = " + dataSource.getClass());// 获取数据库连接Connection connection = dataSource.getConnection();System.out.println("connection = " + connection);connection.close();}
1.3 编写类与映射文件(用1.4 替代这些重复性工作)
1.根据数据库编写实体类
2.编写映射文件xxxMapper.xml
3.编写Dao类,xxxMapper.java
1.4 生成类与映射文件
1.定义版本号
<!--mybatis-generator--><mybatis-generator-plugin-version>1.4.1</mybatis-generator-plugin-version>
2.引入插件
<!--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>
3.配置文件
(1)本地仓库地址
(2)连接配置
<!--连接配置--><jdbcConnectiondriverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://127.0.0.1:3306/forum_db?characterEncoding=utf8&useSSL=false"userId="root"password="root"></jdbcConnection>
(3)实体类生成位置
改为自己项目的包名
<!--实体类生成位置--><javaModelGenerator targetPackage="com.example.forum.model"targetProject="src/main/java"><!-- 是否允许子包,默认false --><property name="enableSubPackages" value="false" /><!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 --><property name="trimStrings" value="true" /></javaModelGenerator>
(4)mapper.xml生成位置
<!--mapper.xml生成位置--><sqlMapGenerator targetPackage="mapper"targetProject="src/main/resources"><property name="enableSubPackages" value="true" /></sqlMapGenerator>
(5)DAO类生成位置
<javaClientGenerator type="XMLMAPPER"targetPackage="om.example.forum.dao"targetProject="src/main/java"><property name="enableSubPackages" value="true" /></javaClientGenerator>
(6)配置生成表与实例
<!--配置生成表与实例, 只需要修改表名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>
(7)总的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><!--驱动包路径: location中路径替换成自己本地路径--><classPathEntry location="D:\maven-repository\mysql\mysql-connector-java\5.1.49\mysql-connector-java-5.1.49.jar"/><context id="DB2Tables" targetRuntime="MyBatis3"><!--禁用自动生成的注释--><commentGenerator><property name="suppressAllComments" value="true" /><property name="suppressDate" value="true" /></commentGenerator><!--连接配置--><jdbcConnectiondriverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://127.0.0.1:3306/forum_db?characterEncoding=utf8&useSSL=false"userId="root"password="root"></jdbcConnection><!-- 非必需,类型处理器,在数据库类型和Java类型之间的转换控制--><!-- 默认false, 把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer.为true时把JDBC DECIMAL 和NUMERIC 类型解析为java.math.BigDecimal --><javaTypeResolver><!--小数统一转为BigDecimal--><property name="forceBigDecimals" value="false" /></javaTypeResolver><!--实体类生成位置--><javaModelGenerator targetPackage="com.example.forum.model"targetProject="src/main/java"><!-- 是否允许子包,默认false --><property name="enableSubPackages" value="false" /><!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 --><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="om.example.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>
(8)双击运行
4.在xxxMapper.xml文件的insert标签中添加获取主键值的选项
useGeneratedKeys="true" keyProperty="id"
5. dao包下添加@Mapper注解
@Mapper
6.model包下添加@Data注解
@Data
1.5 扫描配置
(1)config包下新建MybatisConfig类,指定Mybatis的扫路径
package com.example.forum.config;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;/*** Created with IntelliJ IDEA* Description 配置Mybatis的扫描路径* User: 王杰* Date: 2025-06-10* Time: 18:48*/
// 配置类(加入Spring)
@Configuration
// 指定Mybatis的扫描路径
@MapperScan("com.example.forum.dao")
public class MybatisConfig {
}
(2)application.yml中加入mybatis配置
# mybatis 相关配置,单独配置,顶格写
mybatis:mapper-locations: classpath:mapper/**/*.xml # 指定xxxMapper.xml的扫描路径
1.6 测试
INSERT INTO `t_user` (`id`,`username`,`password`,`nickname`,`gender`,`salt`,`avatarUrl`,`articleCount`, `isAdmin`, `state`, `deleteState`, `createTime`, `updateTime`) VALUES (1, 'boy', '123456', '小男孩',2,'`t_article_reply`123','avatar.png',0,1,0,0,'2022-12-13 22:30:10','2022-12-13 22:30:13');SELECT * FROM `t_user`;
@Resourceprivate UserMapper userMapper;
@Testvoid testMybatis() {User user = userMapper.selectByPrimaryKey(1l);System.out.println(user);System.out.println(user.getUsername());}