pdmaner使用模版

本文详细展示了在Java中如何使用ORM框架进行数据库操作,包括Model(实体类)、Mapper(数据访问层)、Service(业务逻辑层)和Controller(控制层)的创建和关联,涉及基本的CRUD操作。
摘要由CSDN通过智能技术生成

1.Model

{{  var today=new Date();
    var fullYear=today.getFullYear();
    var month=today.getMonth() + 1;
    var days=today.getDate();
    
    var pkVarName = "undefinedId";
    var pkDataType = "String";
    it.entity.fields.forEach(function(field){
        if(field.primaryKey){
            pkVarName = it.func.camel(field.defKey,false);
            pkDataType = field["type"];
            return;
        }
    });
    
    var pkgName = it.entity.env.base.nameSpace;
    var beanClass = it.entity.env.base.codeRoot;
    var beanVarName = beanClass.charAt(0).toLowerCase()+beanClass.slice(1);
    var serviceClass = beanClass+'Service';
    var serviceVarName= beanVarName+'Service';
    
}}package {{=pkgName}}.entity;
$blankline
import java.time.LocalDateTime;
import lombok.Data;
import java.math.BigDecimal;
$blankline

 /**
 * {{=it.entity.defName}};{{=it.entity.comment}}
 * @author : heimi
 * @date : {{=fullYear}}-{{=month}}-{{=days}}
 */

@Data
public class {{=beanClass}} implements BaseModel {
{{~it.entity.fields:field:index}}
    /** {{=it.func.join(field.defName,field.comment,';')}} */
    private {{=field.type}} {{=it.func.camel(field.defKey,false)}} ;
{{~}}
$blankline

}

2.Dao

{{  var today=new Date();
    var fullYear=today.getFullYear();
    var month=today.getMonth() + 1;
    var days=today.getDate();
    
    var pkVarName = "undefinedId";
    var pkDataType = "String";
    it.entity.fields.forEach(function(field){
        if(field.primaryKey){
            pkVarName = it.func.camel(field.defKey,false);
            pkDataType = field["type"];
            return;
        }
    });
    
    var pkgName = it.entity.env.base.nameSpace;
    var beanClass = it.entity.env.base.codeRoot;
    var beanVarName = beanClass.charAt(0).toLowerCase()+beanClass.slice(1);
    var serviceClass = beanClass+'Service';
    var serviceVarName= beanVarName+'Service';
    
}}package {{=pkgName}}.mapper;
$blankline
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.Pageable;
import {{=pkgName}}.model.{{=beanClass}};
$blankline

 /**
 * {{=it.entity.defName}};({{=it.entity.defKey}})表数据库访问层
 * @author : heimi
 * @date : {{=fullYear}}-{{=month}}-{{=days}}
 */
@Mapper
public interface {{=beanClass}}Dao{


    @PageX
    List<{{=beanClass}}> select({{=beanClass}}Query query);

    Integer insert({{=beanClass}} {{=beanVarName}});
    
    Integer insertBatch(List<{{=beanClass}}> {{=beanVarName}}s);

    Integer update({{=beanClass}} {{=beanVarName}});

    Integer delete(Integer id);


}

3.Service

{{  var today=new Date();
    var fullYear=today.getFullYear();
    var month=today.getMonth() + 1;
    var days=today.getDate();
    
    var pkVarName = "undefinedId";
    var pkVarNameU = "UndefinedId";
    var pkDataType = "String";
    it.entity.fields.forEach(function(field){
        if(field.primaryKey){
            pkVarName = it.func.camel(field.defKey,false);
            pkVarNameU = it.func.camel(field.defKey,true);
            pkDataType = field["type"];
            return;
        }
    });
    
    var pkgName = it.entity.env.base.nameSpace;
    var beanClass = it.entity.env.base.codeRoot;
    var beanVarName = beanClass.charAt(0).toLowerCase()+beanClass.slice(1);
    var serviceClass = beanClass+'Service';
    var serviceVarName= beanVarName+'Service';
    var daoName = beanVarName+'Dao';
    
}}package {{=pkgName}}.service;
$blankline
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import {{=pkgName}}.model.{{=beanClass}};
import {{=pkgName}}.dao.{{=beanClass}}Dao;
import {{=pkgName}}.service.{{=serviceClass}};

 /**
 * {{=it.entity.defName}};({{=it.entity.defKey}})表服务实现类
 * @author : heimi
 * @date : {{=fullYear}}-{{=month}}-{{=days}}
 */
@Service
public class {{=serviceClass}}Impl implements {{=serviceClass}}{
   
     $blankline
    @Autowired
    private {{=beanClass}}Dao {{=daoName}};
    $blankline
    
    public List<{{=beanClass}}> select({{=beanClass}}Query query) {
        return {{=daoName}}.select(query);
    }
    $blankline
    public Integer insert({{=beanClass}} {{=beanVarName}}) {
        return {{=daoName}}.insert({{=beanVarName}});
    }
    $blankline
    public Integer update({{=beanClass}} {{=beanVarName}}) {
        return {{=daoName}}.update({{=beanVarName}});
    }
    $blankline
    public Integer delete(Integer id) {
        return {{=daoName}}.delete(id);
    }
}

4.Controller

{{  var today=new Date();
    var fullYear=today.getFullYear();
    var month=today.getMonth() + 1;
    var days=today.getDate();
    
    var pkVarName = "undefinedId";
    var pkDataType = "String";
    it.entity.fields.forEach(function(field){
        if(field.primaryKey){
            pkVarName = it.func.camel(field.defKey,false);
            pkDataType = field["type"];
            return;
        }
    });
    
    var pkgName = it.entity.env.base.nameSpace;
    var beanClass = it.entity.env.base.codeRoot;
    var beanVarName = beanClass.charAt(0).toLowerCase()+beanClass.slice(1);
    var serviceClass = beanClass+'Service';
    var serviceVarName= beanVarName+'Service';
    
}}package {{=pkgName}}.controller;
$blankline

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import {{=pkgName}}.model.{{=beanClass}};
import {{=pkgName}}.service.{{=serviceClass}};
$blankline

 /**
 * {{=it.entity.defName}};({{=it.entity.defKey}})控制层
 * @author : heimi
 * @date : {{=fullYear}}-{{=month}}-{{=days}}
 */

@RestController
@RequestMapping("/api/{{=beanVarName}}")
public class {{=beanClass}}Controller{
    @Autowired
    private {{=serviceClass}} {{=serviceVarName}};
    $blankline
    /** 
     * 查询接口
     */
     @GetMapping
     public List<{{=beanClass}}> select({{=beanClass}}Query query){
       
        return {{=serviceVarName}}.select(query);
    }
    
    $blankline
    
    @PostMapping 
    public Integer insert(@RequestBody {{=beanClass}} product){
       return {{=serviceVarName}}.insert(product);
    }
    $blankline
    @PutMapping 
    public Integer update(@RequestBody {{=beanClass}} product){
        return {{=serviceVarName}}.update(product);
    }
    $blankline
    @DeleteMapping
    public Integer delete(Integer id){
        return {{=serviceVarName}}.delete(id);
    }
   
}

5.Mapper.xml

{{  var today=new Date();
    var fullYear=today.getFullYear();
    var month=today.getMonth() + 1;
    var days=today.getDate();
    
    var pkVarName = "undefinedId";
    var pkDataType = "String";
    var pkField = "UNDEFINED_ID";
    it.entity.fields.forEach(function(field){
        if(field.primaryKey){
            pkField = field.defKey;
            pkVarName = it.func.camel(field.defKey,false);
            pkDataType = field["type"];
            return;
        }
    });
    
    var pkgName = it.entity.env.base.nameSpace;
    var beanClass = it.entity.env.base.codeRoot;
    var beanVarName = beanClass.charAt(0).toLowerCase()+beanClass.slice(1);
    var serviceClass = beanClass+'Service';
    var serviceVarName= beanVarName+'Service';
    
}}<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="{{=pkgName}}.mapper.{{=beanClass}}Mapper">
    <resultMap type="{{=pkgName}}.model.{{=beanClass}}" id="{{=beanClass}}Map">
    {{~it.entity.fields:field:index}}
        <result property="{{=it.func.camel(field.defKey,false)}}" column="{{=field.defKey}}" />
    {{~}}
    </resultMap>
    $blankline
 
    $blankline
    <!--通用查询-->
    <select id="select" resultMap="{{=beanClass}}Map">
        select
            {{=it.entity.fields.map(function(e,i){return e.defKey}).join(',')}}
        from {{=it.entity.defKey}}
        <where>
        {{~it.entity.fields:field:index}}
            <if test="{{=it.func.camel(field.defKey,false)}} != null and {{=it.func.camel(field.defKey,false)}} != ''">
                and {{=field.defKey}} = #{{{=it.func.camel(field.defKey,false)}}}
            </if>
        {{~}}
        </where>
       
    </select>
    $blankline

    <!--新增数据-->
    <insert id="insert" keyProperty="{{=pkField}}" useGeneratedKeys="true">
        insert into {{=it.entity.defKey}}({{=it.entity.fields.map(function(e,i){return e.defKey}).join(',')}})
        values ({{=it.entity.fields.map(function(e,i){return '#{'+it.func.camel(e.defKey,false)+'}'}).join(',')}})
    </insert>
    $blankline
    <!-- 批量新增数据 -->
    <insert id="insertBatch" keyProperty="{{=pkField}}" useGeneratedKeys="true">
        insert into {{=it.entity.defKey}}({{=it.entity.fields.map(function(e,i){return e.defKey}).join(',')}})
        values
        <foreach collection="entities" item="entity" separator=",">
            ({{=it.entity.fields.map(function(e,i){return '#{entity.'+it.func.camel(e.defKey,false)+'}'}).join(',')}})
        </foreach>
    </insert>
  
    $blankline
    <!-- 更新数据 -->
    <update id="update">
        update {{=it.entity.defKey}}
        <set>
        {{~it.entity.fields:field:index}}
            <if test="{{=it.func.camel(field.defKey,false)}} != null and {{=it.func.camel(field.defKey,false)}} != ''">
                {{=field.defKey}} = #{{{=it.func.camel(field.defKey,false)}}},
            </if>
        {{~}}
        </set>
        where {{=pkField}} = #{{{=pkVarName}}}
    </update>
    $blankline
    <!--通过主键软删除-->
    <update id="delete">
        update  {{=it.entity.defKey}} set status = 0 where {{=pkField}} = #{{{=pkVarName}}}
    </update>
</mapper>


我的示例模板 =========模板开始================== package com.sgcc.{{=it.module.name}}.domain; $blankline import javax.persistence.*; import java.io.Serializable; import java.util.Date; import lombok.Data; import org.hibernate.annotations.GenericGenerator; $blankline $blankline /** * 容量电价信息表 * @Column参数: 1. name: 指定映射到数据库中的字段名 2. unique: 是否唯一,默认为false 3. nullable: 是否允许为null,默认为true 5. insertable: 是否允许插入,默认为true 6. updatetable: 是否允许更新,默认为true 7. columnDefinition: 指定该属性映射到数据库中的实际类型,通常是自动判断。 * @author changjiakang */ @Data @Entity @Table(name="{{=it.entity.title}}") public class {{=it.func.camel(it.entity.title,true) }} implements Serializable{ {{~it.entity.fields:field:index}} /** {{=it.func.join(field.chnname,field.remark,';')}} */ {{? field.pk }} @Id @GeneratedValue(generator = "idGenerator") @GenericGenerator(name = "idGenerator", strategy = "uuid") @Column(name = "ID", nullable = false, length = 32) {{?}} @Column(name = "{{=field.name}}", nullable = true) private {{=field.type}} {{=it.func.camel(field.name,false)}}; {{~}} } $blankline $blankline =================================================== import java.io.Serializable; import java.math.BigDecimal; import lombok.Data; /** * 容量电价信息Vo * @author changjiakang */ @Data public class {{=it.func.camel(it.entity.title,true) }}Vo implements Serializable { {{~it.entity.fields:field:index}} /** {{=it.func.join(field.chnname,field.remark,';')}} */ private {{=field.type}} {{=it.func.camel(field.name,false)}}; {{~}} } =========模板结束================== 四.重命名数据类型,生成Po Vo如果字段类型为空,需要设置所有空字段对应的数据类型 五.生成po vo 六.数据库新加的表需要重新导入
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值