1.概述
1.关于数据的增删改查,mybatis有一个自动生成的工具,mybatis-generator,可以自动生成对应的SQL,接口,对象等等
2.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gaoxinfu.demo.open.source.mybatis.db</groupId>
<artifactId>demo-open-source-mybatis-db</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>demo-open-source-mybatis-db</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!--这个包的依赖实际上对自动生成没有影响,主要是自动生成的接口,会有一些注解需要引入这个jar包,如@Param-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<!--is ignored for maven-compiler-plugin-->
<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.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
3.generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gaoxinfu.demo.open.source.mybatis.db</groupId>
<artifactId>demo-open-source-mybatis-db</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>demo-open-source-mybatis-db</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!--这个包的依赖实际上对自动生成没有影响,主要是自动生成的接口,会有一些注解需要引入这个jar包,如@Param-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<!--is ignored for maven-compiler-plugin-->
<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.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
4.数据库表
5.执行命令
我这里用的idea去执行的,直接点击即可
6.效果
7.代码地址
https://gitee.com/gaoxinfu_admin/open-source/tree/master/mybatis/mybatis-3-master/demo-open-source-mybatis-db