mysql 表结构生成java实体与mybatis映射

Posted lljliulljn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 表结构生成java实体与mybatis映射相关的知识,希望对你有一定的参考价值。

service_merchant | CREATE TABLE service_merchant (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
merchant_name varchar(32) NOT NULL COMMENT ‘服务商名称’,
coop_type tinyint(2) NOT NULL DEFAULT ‘0’ COMMENT ‘合作类型’,
linkman varchar(16) NOT NULL COMMENT ‘负责联系人’,
identity_card varchar(18) NOT NULL COMMENT ‘身份证’,
phone varchar(16) NOT NULL COMMENT ‘联系电话’,
service_phone varchar(16) NOT NULL COMMENT ‘客服电话’,
introduction varchar(128) NOT NULL COMMENT ‘商户介绍’,
contract_id int(11) NOT NULL DEFAULT ‘0’ COMMENT ‘合同ID’,
raw_time datetime NOT NULL,
update_time datetime NOT NULL,
average_comment double DEFAULT NULL COMMENT ‘服务商的平均评分’,
list_img varchar(200) DEFAULT NULL COMMENT ‘列表图片’,
detail_img varchar(200) DEFAULT NULL COMMENT ‘详情图片,逗号分隔路径’,
address varchar(100) DEFAULT NULL COMMENT ‘商家详细地址’,
state_id int(11) DEFAULT NULL COMMENT ‘省市id’,
city_id int(11) DEFAULT NULL COMMENT ‘城市id’,
county_id int(11) DEFAULT NULL COMMENT ‘区县id’,
state varchar(20) DEFAULT NULL COMMENT ‘省市’,
city varchar(20) DEFAULT NULL COMMENT ‘城市’,
county varchar(20) DEFAULT NULL COMMENT ‘区县’,
notices varchar(300) DEFAULT NULL COMMENT ‘商户热点,多条逗号分隔’,
status varchar(64) DEFAULT NULL COMMENT ‘状态(正常: NORMAL, 停业休整: CLOSURE, 商户关闭: CLOSED, 合作暂停: COOPERATION_SUSPENDED, 合约过期: CONTRACT_EXPIRED)’,
service_end_time varchar(50) DEFAULT NULL COMMENT ‘营业开始时间’,
service_start_time varchar(50) DEFAULT NULL COMMENT ‘营业结束时间’,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=69 DEFAULT CHARSET=utf8 COMMENT=’服务商表’ |

/**
* @author luoyh
* @date May 23, 2016
*/
public class AutoDBToJavaMybatis

public static void main(String[] args) throws Exception 
    Pattern p = Pattern.compile("`([a-zA-Z_0-9]+)`");
    Pattern cp = Pattern.compile("comment '(.*)'", Pattern.CASE_INSENSITIVE);
    File file = new File("F:/ftps/reslove.txt");
    String alias = "sm";
    String detail = "merchant";
    StringBuilder javaString = new StringBuilder();
    StringBuilder mybatisString = new StringBuilder();
    StringBuilder attrString = new StringBuilder();
    StringBuilder insertString = new StringBuilder();
    StringBuilder updateString = new StringBuilder();
    StringBuilder searchString = new StringBuilder();
    StringBuilder detailString = new StringBuilder();
    StringBuilder detailMybatis = new StringBuilder();
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line = null;
    while((line = br.readLine()) != null) 
        line = StringUtils.trim(line);
        Matcher m = p.matcher(line);
        if(m.find()) 
            String attr = m.group(1);
            String java = handle(attr);
            String des = detail + "." + java;
            searchString.append(alias).append(".`").append(attr).append("` AS `").append(java).append("`,");
            detailString.append(alias).append(".`").append(attr).append("` AS `").append(des).append("`,");
            attrString.append("`").append(attr).append("`,");
            insertString.append("#").append(java).append(",");
            updateString.append(attr).append("=#").append(java).append(",");
            javaString.append("private ");
            mybatisString.append("<result property=\\"").append(java)
            .append("\\" column=\\"").append(attr).append("\\" />").append(System.lineSeparator());
            detailMybatis.append("<result property=\\"").append(des)
            .append("\\" column=\\"").append(des).append("\\" />").append(System.lineSeparator());
            String[] mms = line.split(" ");
            if(mms[2].equals("unsigned") && mms[1].startsWith("int")) 
                javaString.append("long ");
             else if(mms[1].startsWith("int") || mms[1].startsWith("tinyint")) 
                javaString.append("int ");
             else if(mms[1].startsWith("date")) 
                javaString.append("Date ");
             else 
                javaString.append("String ");
            
            String comment = "";
            Matcher cm = cp.matcher(line);
            if(cm.find()) 
                comment = cm.group(1);
            
            javaString.append(java).append("; // ").append(comment).append(System.lineSeparator());
        
    
    br.close();

    System.out.println(javaString.toString());
    System.out.println("=======================");
    System.out.println(mybatisString.toString());
    System.out.println("=======================");
    System.out.println(attrString.toString());
    System.out.println("=======================");
    System.out.println(insertString.toString());
    System.out.println("=======================");
    System.out.println(updateString.toString());
    System.out.println("=======================");
    System.out.println(searchString.toString());
    System.out.println("=======================");
    System.out.println(detailString.toString());
    System.out.println("=======================");
    System.out.println(detailMybatis.toString());


private static String handle(String msg) 
    String[] str = msg.split("_");
    int i = 0;
    StringBuilder sb = new StringBuilder();
    for(String s : str) 
        if(i ++ > 0) 
            sb.append(Character.toUpperCase(s.charAt(0)));
            sb.append(s.substring(1));
         else 
            sb.append(s);
        
    
    return sb.toString();

private String serviceMerchant; //
private long id; //
private String merchantName; // 服务商名称
private int coopType; // 合作类型
private String linkman; // 负责联系人
private String identityCard; // 身份证
private String phone; // 联系电话
private String servicePhone; // 客服电话
private String introduction; // 商户介绍
private int contractId; // 合同ID
private Date rawTime; //
private Date updateTime; //
private String averageComment; // 服务商的平均评分
private String listImg; // 列表图片
private String detailImg; // 详情图片,逗号分隔路径
private String address; // 商家详细地址
private int stateId; // 省市id
private int cityId; // 城市id
private int countyId; // 区县id
private String state; // 省市
private String city; // 城市
private String county; // 区县
private String notices; // 商户热点,多条逗号分隔
private String status; // 状态(正常: NORMAL, 停业休整: CLOSURE, 商户关闭: CLOSED, 合作暂停: COOPERATION_SUSPENDED, 合约过期: CONTRACT_EXPIRED)
private String serviceEndTime; // 营业开始时间
private String serviceStartTime; // 营业结束时间
private String id; //

=======================


























=======================

service_merchant,id,merchant_name,coop_type,linkman,identity_card,phone,service_phone,introduction,contract_id,raw_time,update_time,average_comment,list_img,detail_img,address,state_id,city_id,county_id,state,city,county,notices,status,service_end_time,service_start_time,id,

serviceMerchant,#id,#merchantName,#coopType,#linkman,#identityCard,#phone,#servicePhone,#introduction,#contractId,#rawTime,#updateTime,#averageComment,#listImg,#detailImg,#address,#stateId,#cityId,#countyId,#state,#city,#county,#notices,#status,#serviceEndTime,#serviceStartTime,#id,

=======================

service_merchant=#serviceMerchant,id=#id,merchant_name=#merchantName,coop_type=#coopType,linkman=#linkman,identity_card=#identityCard,phone=#phone,service_phone=#servicePhone,introduction=#introduction,contract_id=#contractId,raw_time=#rawTime,update_time=#updateTime,average_comment=#averageComment,list_img=#listImg,detail_img=#detailImg,address=#address,state_id=#stateId,city_id=#cityId,county_id=#countyId,state=#state,city=#city,county=#county,notices=#notices,status=#status,service_end_time=#serviceEndTime,service_start_time=#serviceStartTime,id=#id,

sm.service_merchant AS serviceMerchant,sm.id AS id,sm.merchant_name AS merchantName,sm.coop_type AS coopType,sm.linkman AS linkman,sm.identity_card AS identityCard,sm.phone AS phone,sm.service_phone AS servicePhone,sm.introduction AS introduction,sm.contract_id AS contractId,sm.raw_time AS rawTime,sm.update_time AS updateTime,sm.average_comment AS averageComment,sm.list_img AS listImg,sm.detail_img AS detailImg,sm.address AS address,sm.state_id AS stateId,sm.city_id AS cityId,sm.county_id AS countyId,sm.state AS state,sm.city AS city,sm.county AS county,sm.notices AS notices,sm.status AS status,sm.service_end_time AS serviceEndTime,sm.service_start_time AS serviceStartTime,sm.id AS id,

sm.service_merchant AS merchant.serviceMerchant,sm.id AS merchant.id,sm.merchant_name AS merchant.merchantName,sm.coop_type AS merchant.coopType,sm.linkman AS merchant.linkman,sm.identity_card AS merchant.identityCard,sm.phone AS merchant.phone,sm.service_phone AS merchant.servicePhone,sm.introduction AS merchant.introduction,sm.contract_id AS merchant.contractId,sm.raw_time AS merchant.rawTime,sm.update_time AS merchant.updateTime,sm.average_comment AS merchant.averageComment,sm.list_img AS merchant.listImg,sm.detail_img AS merchant.detailImg,sm.address AS merchant.address,sm.state_id AS merchant.stateId,sm.city_id AS merchant.cityId,sm.county_id AS merchant.countyId,sm.state AS merchant.state,sm.city AS merchant.city,sm.county AS merchant.county,sm.notices AS merchant.notices,sm.status AS merchant.status,sm.service_end_time AS merchant.serviceEndTime,sm.service_start_time AS merchant.serviceStartTime,sm.id AS merchant.id,



























以上是关于mysql 表结构生成java实体与mybatis映射的主要内容,如果未能解决你的问题,请参考以下文章

mybatis根据数据库表结构自动生成实体类,dao,mapper

mybatis-generator自动生成代码(lombok带注释)

Mybatis深度整合Mysql的Json字段

Mybatis学习MyBatis的逆向工程

IDEA——mybatis-generator插件自动生成实体代码(Maven)

Screw工具基本使用:一键生成Java实体类和数据库文档