MyBatis三剑客指的是:MyBatis-Generate、Mybatis Plus、MyBatis-PageHelper
MyBatis-Generate
使用 Mybatis Generator 这个maven插件来快速生成 Dao 类, mapper 配置文件和 Model 类.
MyBatis Generator(简称MBG)是MyBatis的代码生成器.可以自动查询数据库中的所有表,然后生成可以访问表的基础对象类型.解决了对数据库操作有最大影响的一些简单的CRUD增删改查操作,但是仍需要对联合查询和存储过程手写SQL语句和对象.
1.在pom文件中添加插件
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin>
2.在maven项目中的resource中创建xml文件与properties资源文件
- 名称可以随便取,这里以 generatorConfig.xml 为名
- 资源文件为 datasource.properties 文件,这个可不要,这里用是因为方便管理而已
3.配置generatorConfig.xml与资源文件
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "file:///Users/mac/Documents/work/tools/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!--导入属性配置--> <properties resource="datasource.properties"></properties> <!--指定特定数据库的jdbc驱动jar包的位置--> <classPathEntry location="${db.driverLocation}"/> <context id="default" targetRuntime="MyBatis3"> <!-- optional,旨在创建class时,对注释进行控制 --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--jdbc的数据库连接 --> <jdbcConnection driverClass="${db.driverClassName}" connectionURL="${db.url}" userId="${db.username}" password="${db.password}"> </jdbcConnection> <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制--> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类 targetPackage 指定生成的model生成所在的包名 targetProject 指定在该项目下所在的路径 --> <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">--> <javaModelGenerator targetPackage="com.mmall.pojo" targetProject="./src/main/java"> <!-- 是否允许子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> <!-- 是否对model添加 构造函数 --> <property name="constructorBased" value="true"/> <!-- 是否对类CHAR类型的列的数据进行trim操作 --> <property name="trimStrings" value="true"/> <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 --> <property name="immutable" value="false"/> </javaModelGenerator> <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 --> <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">--> <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 --> <!-- targetPackage:mapper接口dao生成的位置 --> <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject="./src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <table tableName="mmall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_cart" domainObjectName="Cart" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_cart_item" domainObjectName="CartItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_pay_info" domainObjectName="PayInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="mmall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <columnOverride column="detail" jdbcType="VARCHAR" /> <columnOverride column="sub_images" jdbcType="VARCHAR" /> </table> <table tableName="mmall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <!-- geelynote mybatis插件的搭建 --> </context> </generatorConfiguration>
datasource.properties
db.driverLocation=/Users/mac/Documents/work/mysqljar/mysql-connector-java-5.1.6-bin.jar db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc:mysql://127.0.0.1:3306/mmall?characterEncoding=utf-8db.username=root db.password=123456 db.initialSize = 20 db.maxActive = 50 db.maxIdle = 20 db.minIdle = 10 db.maxWait = 10 db.defaultAutoCommit = true db.minEvictableIdleTimeMillis = 3600000
4.运行
- 方式:
在idea 的左下角有个图标,如图:
点击图标,在右侧调出Maven Projects窗口,右侧已有该窗口,可以忽略。然后选中插件,双击即可:
下方的控制台出现build success 就是成功了。就会发现dao 和pojo的包都生成了接口和数据对象实体类以及mapper文件夹中的内容。
注意:mapper中的时间戳可以根据自己需要进行优化。
eg:
把insert标签下的#{createTime,jdbcType=TIMESTAMP}和#{updateTime,jdbcType=TIMESTAMP}改成now()
把update标签下的#{updateTime,jdbcType=TIMESTAMP}改成now()
这个now()方法是数据库自带的函数,表示现在的时间
Mybatis-Plus
Mybatis Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
Mybatis Plus中文文档
1.功能
- 提供Mapper接口与配置文件中对应SQL的导航
- 编辑XML文件时自动补全
- 根据Mapper接口, 使用快捷键生成xml文件及SQL标签
- ResultMap中的property支持自动补全,支持级联(属性A.属性B.属性C)
- 快捷键生成@Param注解
- XML中编辑SQL时, 括号自动补全
- XML中编辑SQL时, 支持参数自动补全(基于@Param注解识别参数)
- 自动检查Mapper XML文件中ID冲突
- 自动检查Mapper XML文件中错误的属性值
- 支持Find Usage
- 支持重构从命名
- 支持别名
- 自动生成ResultMap属性
- 快捷键: Option + Enter(Mac) | Alt + Enter(Windows)
2.安装与破解
- 这是一个IDE插件,目前是收费的,这里我用的是Intellij IDEA的
- 装完之后把Intellij IDEA关了,然后打开下面的链接跟着步骤来就可以大功告成
- 想安装正版但是破解好像是没有成功。以后再补充。
Mybatis-pageHelper
是一个开源的分页插件(如下网址有插件的全介绍)
https://github.com/pagehelper/Mybatis-PageHelper
它的原理,是通过spring的AOP来实现的,这个插件能在执行sql的时候,把相关的数据再执行一次
git上详细介绍了这个开源插件支持的DB,使用方法,在不同的Spring版本及Spring Boot中的配置。具体的可以自行研究。
1:在pom.xml中添加依赖:
<!-- mybatis pager --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>0.9.4</version> </dependency>
2:在spring配置文件中添加配置:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"></property> <!-- 分页插件 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=mysql </value> </property> </bean> </array> </property> </bean>
相关推荐
- **IDE集成**:可能使用Eclipse、IntelliJ IDEA等IDE进行项目开发,这些工具提供了丰富的代码提示和调试功能。 通过研究和实践这个人事管理系统项目,初学者不仅可以掌握Java编程,还能了解到Web开发的全貌,包括...
- **Eclipse或IntelliJ IDEA**:主流的Java集成开发环境,支持代码调试、重构等功能。 以上只是部分关键知识点,实际项目中还可能涉及更多的技术和工具,如缓存技术(Redis)、安全框架(Spring Security)、性能...
10. **测试与调试**:编写单元测试、集成测试来验证代码的正确性,熟练使用IDE(如Eclipse、IntelliJ IDEA)的调试工具,以便在遇到问题时快速定位和修复。 通过这个项目,你不仅能够掌握JavaWeb开发的基本技能,还...
- **Eclipse** 或 **IntelliJ IDEA**:这些是Java开发的主流IDE,提供代码自动完成、调试、版本控制等多种功能,提高开发效率。 5. **版本控制系统** - **Git**:用于管理项目源代码的版本,便于团队协作和代码...
6. **开发环境搭建**:如何配置开发环境,比如安装JDK、设置环境变量、下载并配置IDE(如Eclipse或IntelliJ IDEA)以及部署Web项目到服务器。 7. **版本控制**:可能会介绍版本控制系统如Git,对于团队协作和代码...