mybatis三大插件使用方法
使用Intellij Idea2016.3.1+Maven3.3.9。
一、mybatis-generator的使用
作用:根据数据库自动生成pojo、dao和xml文件。
1、引入mybatis-generator
- <build>
- <finalName>MMall</finalName>
- <plugins>
- <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>
- </plugins>
- </build>
双击mybaitis-generator:generate,执行生成代码操作,控制台会提示BUILD FAILURE。
原因如下:
- [ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:
- generate (default-cli) on project MMall:
- configfile C:\Users\Administrator\Desktop\MMall\src\main\resources\generatorConfig.xml
- does not exist -> [Help 1]
2、引入generatorConfig.xml配置
新建方法:在resources处右击选择New——>mybatis-generator-config,如图
填写finaname为:generatorConfig,生成代码,并修改:
- <?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>
- <!--导入属性配置-->
- <properties resource="db.properties"></properties>
- <!-- 指定特定数据库的jdbc驱动jar包的位置 -->
- <classPathEntry location="${db.driverLocation}"/>
- <!--
- context:至少需要有一个<context>元素,用于指定一组对象的环境。
- 必选属性:id:用来确定一个<context>元素
- 可选属性:
- 1、defaultModelType:**这个属性很重要**,这个属性定义了MBG如何生成**实体类**。
- conditional 默认属性,这个模型和下面的hierarchical类似,除了如果那个单独的类将只包含一个字段,
- 将不会生成一个单独的类。因此,如果一个表的主键只有一个字段,那么不会为该字段生成单独的实体类,
- 将该字段合并到基本实体类中。
- flat:该模型为每一张表只生成一个实体类。这个实体类包含表中的所有字段。**这种模型最简单,推荐使用。
- hierarchical:如果表有主键,那么该模型会产生一个单独的主键实体类,如果表还有BLOB字段,
- 则会为表生成一个包含所有BLOB字段的单独的实体类,
- 然后为所有其他的字段生成一个单独的实体类。 MBG会在所有生成的实体类之间维护一个继承关系。
- 2、targetRuntime::此属性用于指定生成的代码的运行时环境。一般使用默认值即可。
- MyBatis3:*这是默认值*
- MyBatis3Simple
- Ibatis2Java2
- Ibatis2Java5
- -->
- <context id="context" defaultModelType="conditional" targetRuntime="MyBatis3">
- <!--旨在创建class时,对注释进行控制-->
- <commentGenerator>
- <!--suppressAllComments false时打开注释,true时关闭注释-->
- <property name="suppressAllComments" value="false"/>
- <!--suppressDate false时打开时间标志,true时关闭-->
- <property name="suppressDate" value="true"/>
- </commentGenerator>
- <!--jdbc的数据库连接 -->
- <jdbcConnection driverClass="${db.driverClass}"
- connectionURL="${db.url}"
- userId="${db.username}"
- password="${db.password}"/>
- <!--java类型处理器
- 用于处理DB中的类型到Java中的类型,默认使用JavaTypeResolverDefaultImpl;
- 注意一点,默认会先尝试使用Integer,Long,Short等来对应DECIMAL和 NUMERIC数据类型;
- -->
- <javaTypeResolver>
- <!--
- true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型
- false:默认,
- scale>0;length>18:使用BigDecimal;
- scale=0;length[10,18]:使用Long;
- scale=0;length[5,9]:使用Integer;
- scale=0;length<5:使用Short;
- -->
- <property name="forceBigDecimals" value="true"/>
- </javaTypeResolver>
- <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
- targetPackage 指定生成的model生成所在的包名
- targetProject 指定在该项目下所在的路径
- -->
- <javaModelGenerator targetPackage="com.mmall.entity"
- 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"/>
- <!-- 设置一个根对象,即基类-->
- <property name="rootClass" value="com.mmall.entity.BaseEntity"/>
- </javaModelGenerator>
- <!--Mapper映射文件生成器
- targetPackage:指定生成的model生成所在的包名
- targetProject 指定在该项目下所在的路径
- -->
- <sqlMapGenerator targetPackage="com.mmall.dao"
- targetProject="src/main/java">
- <!--是否允许子包,即targetPackage.schemaName.tableName-->
- <property name="enableSubPackages" value="false"/>
- </sqlMapGenerator>
- <!-- 对于mybatis来说,即生成Mapper接口
- targetPackage/targetProject:同javaModelGenerator
- type:选择怎么生成mapper接口(在MyBatis3/MyBatis3Simple下):
- 1,ANNOTATEDMAPPER:会生成使用Mapper接口+Annotation的方式创建(SQL生成在annotation中),
- 不会生成对应的XML;
- 2,MIXEDMAPPER:使用混合配置,会生成Mapper接口,并适当添加合适的Annotation,但是XML会生成在XML中;
- 3,XMLMAPPER:会生成Mapper接口,接口完全依赖XML;
- 注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER
- -->
- <javaClientGenerator targetPackage="com.mmall.dao"
- targetProject="src/main/java"
- type="XMLMAPPER">
- <!--是否允许子包,即targetPackage.schemaName.tableName-->
- <property name="enableSubPackages" value="false"/>
- </javaClientGenerator>
- <!--
- tableName:数据库表名
- domainObjectName:实体类名
- enableCountByExample(默认true):MyBatis3Simple为false,
- 指定是否生成动态查询总条数语句(用于分页的总条数查询);
- enableUpdateByExample:(默认true):MyBatis3Simple为false,
- 指定是否生成动态修改语句(只修改对象中不为空的属性);
- enableDeleteByExample:enableDeleteByExample(默认true):
- MyBatis3Simple为false,指定是否生成动态删除语句;
- enableSelectByExample:enableSelectByExample(默认true):
- MyBatis3Simple为false,指定是否生成动态查询语句;
- selectByExampleQueryId:
- enableInsert(默认true):指定是否生成insert语句;
- enableSelectByPrimaryKey(默认true):指定是否生成按照主键查询对象的语句(就是getById或get);
- enableUpdateByPrimaryKey(默认true):指定是否生成按照主键修改对象的语句(即update);
- enableDeleteByPrimaryKey(默认true):指定是否生成按照主键删除对象的语句(即delete);
- modelType:参考context元素的defaultModelType,相当于覆盖;
- -->
- <table tableName="t_good"
- domainObjectName="Good"
- enableCountByExample="false" enableUpdateByExample="false"
- enableDeleteByExample="false" enableSelectByExample="false"
- selectByExampleQueryId="false"/>
- </context>
- </generatorConfiguration>
db.properties内容如下:
- db.driverLocation=D:\mysql-connector-java-5.1.7-bin.jar
- db.driverClass=com.mysql.jdbc.Driver
- db.url=jdbc:mysql://127.0.0.1:3306/girl?useUnicode=true&characterEncoding=utf-8&useSSL=false
- db.username=root
- db.password=
二、mybatis-plugin的使用
这是一个能够追踪dao 接口和mapper文件里xml的一个插件,包括
- 编辑XML时自动补全
- 根据Mapper接口,使用快捷键生成xml文件及SQL标签
- ResultMap中的property支持自动补全
- 快捷键生成@Param注解
- XML中编辑SQL时,
括号自动补全
- XML中编辑SQL时,
支持参数自动补全(基于@Param注解识别参数)
- 自动检查Mapper
XML文件中ID冲突
- 自动检查Mapper
XML文件中错误的属性值
- 支持Find
Usage
- 支持重构从命名
- 支持别名
- 自动生成ResultMap属性
- 快捷键: Alt + Enter(Windows)
1、安装插件
2、mybatis-plugin插件**
点击打开链接进入根据插件版本下载相应包至本地。
找到本机的mybatis_plus.jar,位置为C:\Users\Administrator\.IntelliJIdea2016.3\config\plugins\mybatis_plus\lib。
使用解压工具打开
将下载好的**,与plugin插件压缩包中的文件更换,拖入压缩包,更换就行
重启IDEA,**成功。
点击绿色小按钮可以进入xml文件
三、mybatis-pagehelper的使用
它是一个开源的分页插件点击打开链接,它的原理,是通过spring的AOP来实现的,这个插件能在执行sql的时候,把相关的数据再执行一次。
1、在pom.xml中添加依赖
- <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 name="plugins">
- <array>
- <bean class="com.github.pagehelper.PageHelper">
- <property name="properties">
- <value>
- dialect=mysql
- </value>
- </property>
- </bean>
- </array>
- </property>
- </bean>
注意不同数据库的方言的使用