Mybatis逆向工程
Mybatis是目前非常流行的持久层框架,其逆向工程更是大大缩减了我们的开发时间。所谓mybatis逆向工程,就是Mybatis会根据我们设计好的数据表,自动生成pojo(实体)、mapper(接口)以及mapper.xml(映射)
官网地址:https://mybatis.org/generator/
环境搭建
1 创建工程并导入依赖
创建
mybatis-generator
工程并导入依赖
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
2 编写配置文件
在resources目录下添加名为
generatorConfig.xml(固定的名称)
配置文件,内容如下
<?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>
<!-- classPathEntry:数据库的 JDBC驱动的jar 包地址 -->
<classPathEntry
location="lib/mysql-connector-java-5.1.47.jar"/>
<context id="context" targetRuntime="MyBatis3">
<!--mybatis支持很多插件,这些插件都在 org.mybatis.generator.plugins包下 -->
<!-- 配置生成pojo的序列化的插件 -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!-- 配置生成pojo的toString的插件 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/heima_movies"
userId="root"
password="root"/>
<!--
默认为false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
为true时,把JDBC DECIMAL 和 NUMERIC 类型解析为BigDecimal
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- targetProject:生成 POJO 实体类的位置 -->
<javaModelGenerator targetPackage="cn.itcast.domain" targetProject="./src/main/java">
<!-- enableSubPackages:是否让schema 作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成 XML文件 -->
<sqlMapGenerator targetPackage="cn.itcast.dao" targetProject="./src/main/resources">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!--生成接口-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="cn.itcast.dao" targetProject="./src/main/java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<!-- 指定数据库表 -->
<table tableName="tb_banner" domainObjectName="Banner" mapperName="BannerMapper"
enableCountByExample="false" enableDeleteByExample="false"
enableUpdateByExample="false"/>
<table tableName="tb_comment" domainObjectName="Comment" mapperName="CommentMapper"
enableCountByExample="false" enableDeleteByExample="false"
enableUpdateByExample="false"/>
<table tableName="tb_members" domainObjectName="Members" mapperName="MembersMapper"
enableCountByExample="false" enableDeleteByExample="false"
enableUpdateByExample="false"/>
<table tableName="tb_movies" domainObjectName="Movies" mapperName="MoviesMapper"
enableCountByExample="false" enableDeleteByExample="false"
enableUpdateByExample="false"/>
<table tableName="tb_user" domainObjectName="User" mapperName="UserMapper"
enableCountByExample="false" enableDeleteByExample="false"
enableUpdateByExample="false"/>
<table tableName="tb_category" domainObjectName="Category" mapperName="CategoryMapper"
enableCountByExample="false" enableDeleteByExample="false"
enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
3 使用插件生成
注意:创建不要多次点击,否则会重复生成代码,追加到文件中,执行报错!!!
方法介绍
3.1 CRUD
public interface MoviesMapper {
Movies selectByPrimaryKey(Integer id); //根据id做查询
int insertSelective(Movies record); //增
int deleteByPrimaryKey(Integer id); //删
int updateByPrimaryKeySelective(Movies record); // 改
List<Movies> selectByExample(MoviesExample example); //全查或者条件查
...
}
3.2 条件查询
public List<Movies> findAll() {
/*全查*/
/*MoviesExample moviesExample = new MoviesExample();
List<Movies> list = moviesMapper.selectByExample(moviesExample);*
/*条件查*/
MoviesExample moviesExample = new MoviesExample();
//获取条件对象
MoviesExample.Criteria criteria = moviesExample.createCriteria();
//设置条件1 都以and开头
criteria.andActorsLike("%小罗伯特%");
//设置条件2 都以and开头
//criteria.andIdEqualTo(8);
//设置条件3 都以and开头 ...
//排序--字段名
moviesExample.setOrderByClause("id desc");
List<Movies> list = moviesMapper.selectByExample(moviesExample);
return list;
}
```