网站开发强制使用急速内核,外发加工网站源码下载,网站备案转服务器,wordpress控制仪目录 一、MyBatis Generator 的使用
1.1、生成类和映射文件
1.1.1、在 pom.xml 中引入依赖
1.1.2、根据 configurationFile 标签中配置的路径 创建 generatorConfig.xml 文件
1.1.3、自动生成类 和 映射文件
1.1.4、在 Insert 标签中添加获取主键值的选项
1.1.5、扫描配置…目录 一、MyBatis Generator 的使用
1.1、生成类和映射文件
1.1.1、在 pom.xml 中引入依赖
1.1.2、根据 configurationFile 标签中配置的路径 创建 generatorConfig.xml 文件
1.1.3、自动生成类 和 映射文件
1.1.4、在 Insert 标签中添加获取主键值的选项
1.1.5、扫描配置添加 Mapper 注解 / 添加扫描注解
1.1.6、配置 mybatis
1.1.7、测试 一、MyBatis Generator 的使用 1.1、生成类和映射文件
1.1.1、在 pom.xml 中引入依赖
在 properties 标签中加入版本号.
mybatis-generator-plugin-version1.4.1/mybatis-generator-plugin-version
在 build plugins 标签中加入如下配置 !-- mybatis ⽣成器插件 --plugingroupIdorg.mybatis.generator/groupIdartifactIdmybatis-generator-maven-plugin/artifactIdversion${mybatis-generator-plugin-version}/versionexecutionsexecutionidGenerate MyBatis Artifacts/idphasedeploy/phasegoalsgoalgenerate/goal/goals/execution/executions!-- 相关配置 --configuration!-- 打开⽇志 --verbosetrue/verbose!-- 允许覆盖 --overwritetrue/overwrite!-- 配置⽂件路径 --configurationFilesrc/main/resources/mybatis/generatorConfig.xml/configurationFile/configuration/plugin上述配置中需要注意的是 “配置文件路径”这个路径就是用来生成 实体类和映射文件 配置规则的位置. 1.1.2、根据 configurationFile 标签中配置的路径 创建 generatorConfig.xml 文件
这个文件就是用来描述生成规则的.
根据路径src/main/resources/mybatis在 mybatis 目录下创建 generatorConfig.xml 文件. Ps下述配置文件中需要修改的有 数据库连接、实体类和映射文件的路径、数据库表名 ?xml version1.0 encodingUTF-8?
!DOCTYPE generatorConfigurationPUBLIC -//mybatis.org//DTD MyBatis Generator Configuration 1.0//ENhttp://mybatis.org/dtd/mybatis-generator-config_1_0.dtdgeneratorConfiguration!-- 驱动包路径location中路径替换成⾃⼰本地路径 --classPathEntry locationD:\class\source\mysql-connector-java-5.1.49.jar/context idDB2Tables targetRuntimeMyBatis3!-- 禁⽤⾃动⽣成的注释 --commentGeneratorproperty namesuppressAllComments valuetrue/property namesuppressDate valuetrue//commentGenerator!-- 连接配置 --jdbcConnection driverClasscom.mysql.jdbc.DriverconnectionURLjdbc:mysql://127.0.0.1:3306/javanav_db?
characterEncodingutf8amp;useSSLfalseuserIdrootpassword1111/jdbcConnectionjavaTypeResolver!-- ⼩数统⼀转为BigDecimal --property nameforceBigDecimals valuefalse//javaTypeResolver!-- 实体类⽣成位置 --javaModelGenerator targetPackagecom.example.cyk.modeltargetProjectsrc/main/javaproperty nameenableSubPackages valuetrue/property nametrimStrings valuetrue//javaModelGenerator!-- mapper.xml⽣成位置 --sqlMapGenerator targetPackagemappertargetProjectsrc/main/resourcesproperty nameenableSubPackages valuetrue//sqlMapGenerator!-- mapper 接口⽣成位置 --javaClientGenerator typeXMLMAPPERtargetPackagecom.example.cyk.mapper targetProjectsrc/main/javaproperty nameenableSubPackages valuetrue//javaClientGenerator!-- 配置⽣成表与实例, 只需要修改表名tableName, 与对应类名domainObjectName 即可--table tableNamej_article domainObjectNameArticleenableSelectByExamplefalseenableDeleteByExamplefalse enableDeleteByPrimaryKeyfalseenableCountByExamplefalseenableUpdateByExamplefalse!-- 类的属性⽤数据库中的真实字段名做为属性名, 不指定这个属性会⾃动转换 _ 为驼峰命名规则--property nameuseActualColumnNames valuetrue//tabletable tableNamej_article_reply domainObjectNameArticleReplyenableSelectByExamplefalseenableDeleteByExamplefalse enableDeleteByPrimaryKeyfalseenableCountByExamplefalseenableUpdateByExamplefalseproperty nameuseActualColumnNames valuetrue//tabletable tableNamej_board domainObjectNameBoardenableSelectByExamplefalse enableDeleteByExamplefalseenableDeleteByPrimaryKeyfalse enableCountByExamplefalseenableUpdateByExamplefalseproperty nameuseActualColumnNames valuetrue//tabletable tableNamej_message domainObjectNameMessageenableSelectByExamplefalseenableDeleteByExamplefalse enableDeleteByPrimaryKeyfalseenableCountByExamplefalseenableUpdateByExamplefalseproperty nameuseActualColumnNames valuetrue//tabletable tableNamej_user domainObjectNameUserenableSelectByExamplefalse enableDeleteByExamplefalseenableDeleteByPrimaryKeyfalse enableCountByExamplefalseenableUpdateByExamplefalseproperty nameuseActualColumnNames valuetrue//table/context
/generatorConfiguration注意 驱动包路径是自己本地仓库的路径 但一定注意 需要在非中文的目录下因此你可以把这个驱动包拷贝出来放到一个非中文的目录中即可. 1.1.3、自动生成类 和 映射文件
重新加载Maven项⽬在Plugins节点下出现mybatis-generator双击运⾏在对应的目录下⽣成相应的类与映射⽂件 接着你就可以看到对应的生成了 1.1.4、在 Insert 标签中添加获取主键值的选项
在生成的 xml 文件中给每一个 insert 标签都添加以下属性useGeneratedKeystrue keyPropertyid
!-- useGeneratedKeys true --
!-- keyProperty 主键字段-- !-- 当插⼊⼀条数据后可以通过user.getId()获取到⾃动⽣成的Id值如果⽅法中需要⽴即获取Id值加⼊这个配置 --
insert idinsert parameterTypecom.example.cyk.model.User
useGeneratedKeystrue keyPropertyid Ps这个选项也可以自动生成但是不理想有些问题 1.1.5、扫描配置添加 Mapper 注解 / 添加扫描注解
有两种方式配置扫描 Mapper 接口.
1给每个 mapper 包下的 mapper 接口都添加 Mapper 注解. 2给启动类上 或者 新建一个配置类有 Configuration 注解加上 MapperScan(com.example.cyk.mapper) 注解. 1.1.6、配置 mybatis
在 yml 文件中配置
mybatis:mapper-locations: classpath:mapper/**/*Mapper.xml1.1.7、测试
SpringBootTest
public class TestMapper {Autowiredprivate UserMapper userMapper;Testpublic void select() {User user userMapper.selectByPrimaryKey(1L);System.out.println(user);}}