卜若的代码笔记-Mybatis-第二章:自动化

    科技2026-04-24  16

    2.1 Mybatis自动化简介

    所谓自动化,就是自动生成dao层和模型

    举个简单的例子:正常情况下,我要做一个查询

    我需要自己手动写一个模型(当然你如果自己返回json也不是不可以)

    然后手写sql,使用jdbc去查....自己写查询逻辑

    整个过程简单,但是极度麻烦

    但是当你使用自动化工具之后...

    贼爽!

    以下是你要做的操作(当你自动化后需要去查询一组数据,在controller层)

    飘红是idea的毛病,可以运行的

     其他代码都是由自动化工具帮你生成出来的

    舒服不?

    舒服那就开始自动化吧!

    2.2 自动化步骤

    注意,我也是凭记忆去复现整个过程,可能出现少一两个步骤,请在评论区输出相关问题,我有时间会补充

    2.2.1 pom加载Mybatis自动化插件

    <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> <configuration> <fork>true</fork> </configuration> </plugin> <!-- mvn mybatis-generator:generate --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> </configuration> <dependencies> <!-- 数据库驱动 --> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency> </dependencies> </plugin> </plugins> </build>

     2.2.2 添加一个mapping包

    2.2.3 添加自动化配置文件

    该文件时公式化文件,只需要改一些特定的地方就行

    <?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> <!-- 数据库驱动包位置 --> <!-- 由于在pom.xml中加入插件时已经配置数据库驱动包,所以此处不必配置了--> <!-- <classPathEntry location="D:\generator\mysql-connector-java-5.1.34.jar" /> --> <!--<classPathEntry location="E:\Database\Oracle\jdbc\lib\ojdbc14.jar" />--> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库链接URL、用户名、密码 --> <!--<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/my_db?characterEncoding=utf8" userId="root" password="123456"> --> <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://ip:1433;database=数据库名称;integratedSecurity=false;" userId="账户名" password="密码"> <property name="useInformationSchema" value="true"/> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.example.models" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成的映射文件包名和位置 --> <sqlMapGenerator targetPackage="com.example.mapping" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成那些表(更改tableName和domainObjectName就可以) --> <table tableName="skill_info" domainObjectName="SkillInfo" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" /> <!-- <table tableName="course_info" domainObjectName="CourseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="course_user_info" domainObjectName="CourseUserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> --> </context> </generatorConfiguration>

    注意以下标红的位置,是生成的相关接口,映射,模型的位置 

    位置放在:

    2.2.4 配置映射文件

    映射文件里面标注了sql,所以如果你不配置映射文件,相当于你的SkillInfoMapper就是一个废物

    在application.properties里面添加

    mybatis.mapper-locations=classpath:com/example/mapping/*.xml mybatis.type-aliases-package=com.example.models

     不加会报错哦

    2.2.5 代码生成

     生成出来的代码:

    至于怎么玩...看一开始那样就可以了呗

    反正用着挺爽的 

    Processed: 0.012, SQL: 9