mybatis自动生成ORM代码

下载地址:http://download.csdn.net/download/chenjiazhu/9964676

mybatis-generator来生成固定的ORM代码


修改mysqlgeneratorConfig.xml和mysqlgeneratorConfig_modify.xml



修改main方法


生成的代码如下



自定义方法示例

public interface NodeQuery {

    List<NodePo> selectByPageAndSize(@Param("skip") int skip,@Param("size") int size);
    List<NodePo> selectByCriteriaAndPageAndSize(@Param("name") String name,@Param("domain") String domain, @Param("stressstatus") Boolean stressstatus,@Param("sitstatus") Boolean sitstatus,@Param("uatstatus") Boolean uatstatus, @Param("active") Boolean active,@Param("skip") int skip,@Param("size") int size);
    int getSizeByCriteria(@Param("name") String name,@Param("domain") String domain, @Param("stressstatus") Boolean stressstatus,@Param("sitstatus") Boolean sitstatus,@Param("uatstatus") Boolean uatstatus, @Param("active") Boolean active);
}
NodeQuery.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!--
  ~ /*
  ~  (C) Copyright 2016 Ymatou (http://www.ymatou.com/).
  ~  All rights reserved.
  ~  */
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ymatou.envmanagement.infrastructure.mysqldb.query.NodeQuery" >

    <select id="selectByPageAndSize"  resultType="com.ymatou.envmanagement.infrastructure.mysqldb.model.NodePo" >
        SELECT * FROM node ORDER BY id ASC LIMIT #{skip},#{size}
    </select>

    <select id="selectByCriteriaAndPageAndSize"  resultType="com.ymatou.envmanagement.infrastructure.mysqldb.model.NodePo" >
        SELECT * FROM node
        where 1=1
        <if test="name != null" >
            and name like CONCAT('%','${name}','%' )
        </if>

        <if test="domain != null" >
            and domain like CONCAT('%','${domain}','%' )
        </if>

        <if test="stressstatus != null" >
            and stressstatus=#{stressstatus,jdbcType=BIT}
        </if>

        <if test="sitstatus != null" >
            and sitstatus=#{sitstatus,jdbcType=BIT}
        </if>

        <if test="uatstatus != null" >
            and uatstatus=#{uatstatus,jdbcType=BIT}
        </if>

        <if test="active != null" >
            and active=#{active,jdbcType=BIT}
        </if>
        ORDER BY id ASC LIMIT #{skip},#{size}
    </select>

    <select id="getSizeByCriteria"  resultType="Integer" >
        SELECT count(1) FROM node
        where 1=1
        <if test="name != null" >
            and name=#{name}
        </if>

        <if test="domain != null" >
            and domain=#{domain}
        </if>

        <if test="stressstatus != null" >
            and stressstatus=#{stressstatus,jdbcType=BIT}
        </if>

        <if test="sitstatus != null" >
            and sitstatus=#{sitstatus,jdbcType=BIT}
        </if>

        <if test="uatstatus != null" >
            and uatstatus=#{uatstatus,jdbcType=BIT}
        </if>

        <if test="active != null" >
            and active=#{active,jdbcType=BIT}
        </if>
    </select>

</mapper>

使用示例

import com.ymatou.envmanagement.infrastructure.mysqldb.model.NodeExample;
import com.ymatou.envmanagement.infrastructure.mysqldb.model.NodePo;
import com.ymatou.envmanagement.infrastructure.mysqldb.mapper.NodeMapper;
import com.ymatou.envmanagement.infrastructure.mysqldb.query.NodeQuery;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by chenjiazhu on 2017/7/5.
 */
@Component
public class NodeRepository {
    @Resource
    private NodeMapper nodeMapper;

    @Resource
    private NodeQuery nodeQuery;

    public NodePo selectByPrimaryKey(Integer id){
        return nodeMapper.selectByPrimaryKey(id);
    }

    public List<NodePo> selectNodeByName(String name){
        NodeExample example = new NodeExample();
        example.createCriteria().andNameEqualTo(name);

        return nodeMapper.selectByExample(example);
    }

    public List<NodePo> selectByDomain(String  domain){
        NodeExample example = new NodeExample();
        example.createCriteria().andDomainEqualTo(domain);

        return nodeMapper.selectByExample(example);
    }

    public int insertSelective(NodePo record)
    {
        return nodeMapper.insertSelective(record);
    }

    public int updateByPrimaryKeySelective(NodePo record)
    {
        return  nodeMapper.updateByPrimaryKeySelective(record);
    }

    public List<NodePo> selectByPageAndSize(int skip, int size )
    {
        return nodeQuery.selectByPageAndSize(skip,size);
    }

    public List<NodePo> selectByPageAndSize(String name,String domain,Boolean stressstatus, Boolean sitstatue, Boolean uatstatus, Boolean  active,int skip, int size )
    {
        return nodeQuery.selectByCriteriaAndPageAndSize(name,domain, stressstatus, sitstatue, uatstatus,  active, skip,size);
    }

    public int getSize(String name,String domain,Boolean stressstatus, Boolean sitstatue, Boolean uatstatus, Boolean  active)
    {
        return nodeQuery.getSizeByCriteria(name,domain, stressstatus, sitstatue, uatstatus,  active);
    }

    public List<NodePo> selectActive()
    {
        NodeExample example = new NodeExample();

        NodeExample.Criteria criteria = example.createCriteria().andActiveEqualTo(true);

        return nodeMapper.selectByExample(example);

    }

    public int getSize()
    {
        return nodeMapper.countByExample(null);
    }

    public int getSize(String name,String domain)
    {
        NodeExample example = new NodeExample();
        NodeExample.Criteria criteria = example.createCriteria();

        if(name!=null)
        {
            criteria.andNameLike(name);
        }

        if(domain!=null)
        {
            criteria.andDomainLike(domain);
        }

        return nodeMapper.countByExample(example);
    }


    public int insert(NodePo record){
        return nodeMapper.insertSelective(record);
    }

    public int update(NodePo record){
        return nodeMapper.updateByPrimaryKeySelective(record);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值