1.定义
MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解来配置和映射原生信息,将接口和java的POJOs(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。
2.使用MyBatis
- 新建一个Maven项目在POM中添加
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
- 新建generatorConfig.xml配置内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--本地数据库驱动-->
<classPathEntry location="E:\.m2\repository\mysql\mysql-connector-java\6.0.6\mysql-connector-java-6.0.6.jar"/>
<context id="MysqlTables" targetRuntime="MyBatis3">
<!--去除注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/junli?useUnicode=true&characterEncoding=utf-8&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--实体存放位置-->
<javaModelGenerator targetPackage="com.junly.mybatis.beans"
targetProject="G:\learnworkspace\learn-mybatis\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--XML存放位置-->
<sqlMapGenerator targetPackage="xml" targetProject="G:\learnworkspace\learn-mybatis\src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--Mapper存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.junly.mybatis.mapper"
targetProject="G:\learnworkspace\learn-mybatis\\src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--需要生成的表-->
<table schema="junli" tableName="posts" domainObjectName="Posts">
<property name="useActualColumnNames" value="false"/>
</table>
<table schema="junli" tableName="test" domainObjectName="Test">
<property name="useActualColumnNames" value="false"/>
</table>
<table schema="junli" tableName="blog" domainObjectName="Blog">
<property name="useActualColumnNames" value="false"/>
</table>
<table schema="junli" tableName="author" domainObjectName="Author">
<property name="useActualColumnNames" value="false"/>
</table>
</context>
</generatorConfiguration>
-
生成代码
在完成以上两步之后。就会在idea中看到:直接点击MyBatis-generator:generate就课生成。