下载地址: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);
}
}