mybatis 逆向生成插件的使用和方法说明

一.使用

1.在maven的pom.xml的 <build></build>l中导入插件

  <!--反向生成插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <!--配置文件的路径-->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                </dependencies>
            </plugin>

2.在resourses创建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>
    <!--1、数据库驱动jar:添加自己的jar路径 -->
    <classPathEntry
            location="D:\repository\mysql\mysql-connector-java\8.0.23\mysql-connector-java-8.0.23.jar" />

    <context id="MyBatis" targetRuntime="MyBatis3">

        <!--去除注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--2、数据库连接 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT"
                        userId="root"
                        password="123456">
                     <property name="nullCatalogMeansCurrent" value="true"/>
        </jdbcConnection>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer;
        为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--3、生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建
        使用Maven生成在target目录下,会自动创建) -->
        <javaModelGenerator targetPackage="com.kkb.pojo"
                            targetProject="src\main\java">
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--4、生成SQLmapper.xml文件 -->
        <sqlMapGenerator targetPackage="com.kkb.mapper"
                         targetProject="src\main\resources">
        </sqlMapGenerator>
        <!--5、生成Dao(Mapper)文件,生成接口 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.kkb.mapper"
                             targetProject="src\main\java">
        </javaClientGenerator>
        <!--6、要生成哪些表(更改tableName) -->
        <!-- tableName:要生成的表名 与数据库表明对应
        enableCountByExample:Count语句中加入where条件查询,默认为true开启
        enableUpdateByExample:Update语句中加入where条件查询,默认为true开启
        enableDeleteByExample:Delete语句中加入where条件查询,默认为true开启
        enableSelectByExample:Select多条语句中加入where条件查询,默认为true开启
        selectByExampleQueryId:Select单个对象语句中加入where条件查询,默认为true开启
        -->
        <table tableName="Team">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="Player">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="game">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="GameType">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="Admins">
            <property name="useActualColumnNames" value="true"/>
        </table>
        <table tableName="AdminRole">
            <property name="useActualColumnNames" value="true"/>
        </table>
    </context>
</generatorConfiguration>

3.启动mybatis-generator:generate

生成完毕后实体类会出现对应的  实体类名+Example类  这种类处理多条件查询和排序时候用

注意:与mapper对应的xml映射文件,应该放在resourses下的同路径下,此路径必须层级的创建出来(自动生成的时候不必手动创建)。

二.自动生成的接口类的方法使用说明

以Team实体类的teamMapper接口举例

long countByExample(TeamExample example);
//按条件统计
int deleteByExample(TeamExample example);
//按条件删除
Team selectByPrimaryKey(Integer teamId);
//根据主键查询


int deleteByPrimaryKey(Integer teamId);
//根据主键删除
int updateByPrimaryKey(Team record);
//根据主键更新,updateByPrimaryKey对你注入的字段全部更新
int updateByPrimaryKeySelective(Team record);
// 根据主键更新,pdateByPrimaryKeySelective会对字段进行判断再更新(如果为Null就忽略更新),如果你只想更新某一字段,可以用这个方法。
int insert(Team record);
//插入    sql语句会出现全部属性 未设置的属性赋值为null  属性是与数据库表格列名称一一对应
int insertSelective(Team record);
//选择性的插入   sql语句里面只会出现设置的属性  其他没有设置的默认为null
int updateByExample(@Param("record") Team record, @Param("example") TeamExample example);
//updateByExample是传入一个对象,将整条数据都更新,如果对象中没有值的属性,就自动设置为null.
int updateByExampleSelective(@Param("record") Team record, @Param("example") TeamExample example);
//是将一行中某几个属性更新,而不改变其他的值
//对象可以里面可以只有一个参数,其他都为null,但是当更新的时候,只会更新有属性的那一列,其他列之前是什么样,现在还是什么样子,不会去修改

带有Example的方法都要创建容器来盛放多条件

 //多条件
        TeamExample example=new TeamExample();
        //创建条件的容器
        TeamExample.Criteria criteria = example.createCriteria();

criateria对象能够调用的方法:

         //添加条件 名称为空    
        public Criteria andTeamNameIsNull() {
            addCriterion("teamName is null");
            return (Criteria) this;
        }
         //添加条件 名称不能为空   
        public Criteria andTeamNameIsNotNull() {
            addCriterion("teamName is not null");
            return (Criteria) this;
        }
         //添加条件 等于
        public Criteria andTeamNameEqualTo(String value) {
            addCriterion("teamName =", value, "teamName");
            return (Criteria) this;
        }
        //添加条件 不等于
        public Criteria andTeamNameNotEqualTo(String value) {
            addCriterion("teamName <>", value, "teamName");
            return (Criteria) this;
        }
        //添加条件 大于
        public Criteria andTeamNameGreaterThan(String value) {
            addCriterion("teamName >", value, "teamName");
            return (Criteria) this;
        }
        //添加条件 大于等于
        public Criteria andTeamNameGreaterThanOrEqualTo(String value) {
            addCriterion("teamName >=", value, "teamName");
            return (Criteria) this;
        }
           //添加条件 小于
        public Criteria andTeamNameLessThan(String value) {
            addCriterion("teamName <", value, "teamName");
            return (Criteria) this;
        }
        //添加条件 小于等于
        public Criteria andTeamNameLessThanOrEqualTo(String value) {
            addCriterion("teamName <=", value, "teamName");
            return (Criteria) this;
        }
        //添加条件 模糊查询 like
        public Criteria andTeamNameLike(String value) {
            addCriterion("teamName like", value, "teamName");
            return (Criteria) this;
        }
        //添加条件 模糊查询 notlike
        public Criteria andTeamNameNotLike(String value) {
            addCriterion("teamName not like", value, "teamName");
            return (Criteria) this;
        }
        //添加条件 在List<?>条件
        public Criteria andTeamNameIn(List<String> values) {
            addCriterion("teamName in", values, "teamName");
            return (Criteria) this;
        }
        //添加条件 不在List<?>条件
        public Criteria andTeamNameNotIn(List<String> values) {
            addCriterion("teamName not in", values, "teamName");
            return (Criteria) this;
        }
        //添加条件 范围在约束区间
        public Criteria andTeamNameBetween(String value1, String value2) {
            addCriterion("teamName between", value1, value2, "teamName");
            return (Criteria) this;
        }    
        //添加条件 范围不再约束区间
        public Criteria andTeamNameNotBetween(String value1, String value2) {
            addCriterion("teamName not between", value1, value2, "teamName");
            return (Criteria) this;
        }

无论你是什么类型的属性都是默认的以上方法,这时候就需要根据具体类型调用有实际意义的方法,比如说如果你的属性类型是String类型的  使用范围约束区间就没有实际意思。

调用方法示例:

模糊查询

criteria.andTeamNameLike("%"+"name".trim()+"%");

区间查询

criteria.andTeamNameBetween("1001", "1010");

根据id排序

example.setOrderByClause("teamId desc");

测试类:

import com.kkb.mapper.TeamMapper;
import com.kkb.pojo.Team;
import com.kkb.pojo.TeamExample;
import com.kkb.utils.MybatisUtil;
import org.junit.Test;

import java.util.Date;
import java.util.List;

public class TestGenerator {
    TeamMapper teamMapper = MybatisUtil.getSqlSession().getMapper(TeamMapper.class);

    /**
     * 根据主键查询
     */
    @Test
    public  void test001(){
            Team team = teamMapper.selectByPrimaryKey(1005);
            System.out.println(team);
        }

    /**
     * 根据条件查询
     */
    @Test
     public  void test002() {
        TeamExample teamExample = new TeamExample();
        TeamExample.Criteria criteria = teamExample.createCriteria();
        criteria.andTeamIdBetween(1001, 1010);
        List<Team> teams = teamMapper.selectByExample(teamExample);
        for (Team team : teams) {
            System.out.println(team);
        }
    }

    /**
     * 根据条件选择的更新   只更赋值的属性
     */
        @Test
           public  void test003(){
             Team team = new Team();
             team.setLocation("北京");
        TeamExample teamExample = new TeamExample();
            TeamExample.Criteria criteria = teamExample.createCriteria();
            criteria.andTeamNameLike("wqs");
            int i = teamMapper.updateByExampleSelective(team, teamExample);
            MybatisUtil.getSqlSession().commit();
            System.out.println("更新的数量:"+i);
        }

    /**
     * 根据主键更新 属性会全部更新  没有赋值的属性默认为null
     */
        @Test
        public  void test004(){
            Team team = new Team();
            team.setTeamId(1025);
            team.setTeamName("erGe");
            //team.setCreateTime(new Date());
            team.setLocation("河北");
            teamMapper.updateByPrimaryKey(team);
        }

    /**
     * 根据主键选择更新  没有赋值的属性 使用数据库中的原有值
     */
    @Test
    public  void test005(){
        Team team = new Team();
        team.setTeamId(1025);
        team.setTeamName("erGe");
        //team.setCreateTime(new Date());
        team.setLocation("河北");
        teamMapper.updateByPrimaryKeySelective(team);
    }
}

最后在srvice层使用动态代理对象

 <!--@Resource注解的依赖-->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>

@Resource
private TeamMapper teamMapper;

在service层的方法中

                //多条件
        TeamExample example=new TeamExample();
        //创建条件的容器
        TeamExample.Criteria criteria = example.createCriteria();

        //这里添加条件  使用criteria  调用方法传值

           ...
           ...
           ...

        //这里是多条件查询语句返回的类型,不同方法返回的类型不一样

        List<Team> list = teamMapper.selectByExample(example);

updateByExampleSelective的使用说明

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值