package org.yeshi.utils.mybatis; import java.io.File; import java.io.FileOutputStream; import java.io.PrintWriter; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import org.dom4j.DocumentHelper; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class MyBatisMapperUtil { private static String basePath = "D:/mybatis"; static { // 创建工作目录 if (!new File(basePath).exists()) new File(basePath).mkdirs(); } // 获取dao所在的包名 private static String getDaoPackageName(Class bean) { String name = bean.getName(); String[] ns = name.split("\\."); if (ns.length > 2) { String pks = ""; for (int i = 0; i < ns.length - 2; i++) { pks += ns[i] + "."; } return pks + "dao"; } return ""; } private static String getMapperPackageName(Class bean) { String name = bean.getName(); String[] ns = name.split("\\."); if (ns.length > 2) { String pks = ""; for (int i = 0; i < ns.length - 2; i++) { pks += ns[i] + "."; } return pks + "mapper"; } return ""; } public static void createMapper(Class clz) { // 生成mapper java文件 String pks = getDaoPackageName(clz); StringBuffer buffer = new StringBuffer("package " + pks + ";"); buffer.append("\n\n"); buffer.append("import " + clz.getName() + ";"); buffer.append("\n\n"); buffer.append(String.format("public interface %sMapper {", clz.getSimpleName())); buffer.append("\n\n\t"); buffer.append(String.format("int deleteByPrimaryKey(Long id);")); buffer.append("\n\n\t"); buffer.append(String.format("int insert(%s record);", clz.getSimpleName())); buffer.append("\n\n\t"); buffer.append(String.format("int insertSelective(%s record);", clz.getSimpleName())); buffer.append("\n\n\t"); buffer.append(String.format("%s selectByPrimaryKey(Long id);", clz.getSimpleName())); buffer.append("\n\n\t"); buffer.append(String.format("int updateByPrimaryKeySelective(%s record);", clz.getSimpleName())); buffer.append("\n\n\t"); buffer.append(String.format("int updateByPrimaryKey(%s record);", clz.getSimpleName())); buffer.append("\n"); buffer.append("}"); String daoName = String.format("%sMapper.java", clz.getSimpleName()); String daoPath = basePath + "/dao/" + daoName; try { if (!new File(basePath + "/dao/").exists()) new File(basePath + "/dao/").mkdirs(); if (!new File(daoPath).exists()) new File(daoPath).createNewFile(); FileOutputStream fos = new FileOutputStream(new File(daoPath)); PrintWriter pw = new PrintWriter(fos); pw.write(buffer.toString().toCharArray()); pw.flush(); pw.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } /** * 生成Mapper xml文件 */ // 获取需要映射的列 List keysList = new ArrayList<>(); Field[] fields = clz.getDeclaredFields(); for (Field fd : fields) { Annotation[] as = fd.getAnnotations(); for (Annotation a : as) { if (a instanceof Column) { Column c = (Column) a; keysList.add(new AttributeColumnMap(fd.getName(), c.name(), fd.getType().getName())); } } } String tableName = ""; Annotation[] as = clz.getAnnotations(); for (Annotation a : as) { if (a instanceof Table) { Table t = (Table) a; tableName = t.value(); } } try { String mapperName = String.format("%sMapper.xml", clz.getSimpleName()); String mapperPath = basePath + "/mapper/" + mapperName; org.dom4j.Document document = DocumentHelper.createDocument(); document.addDocType("mapper", "-//mybatis.org//DTD Mapper 3.0//EN", "http://mybatis.org/dtd/mybatis-3-mapper.dtd"); org.dom4j.Element root = document.addElement("mapper"); root.addAttribute("namespace", pks + "." + mapperName.replace(".xml", "")); org.dom4j.Element resultMap = root.addElement("resultMap"); resultMap.addAttribute("id", "BaseResultMap"); resultMap.addAttribute("type", clz.getName()); AttributeColumnMap idKeys = getAttributeColumnMapByAttribute("id", keysList); if (idKeys != null) { org.dom4j.Element id = resultMap.addElement("id"); id.addAttribute("column", idKeys.column); id.addAttribute("property", idKeys.attribute); id.addAttribute("jdbcType", ColumnParseUtil.getJDBCType(idKeys.type)); } for (AttributeColumnMap key : keysList) { if (key.attribute.equalsIgnoreCase(idKeys.attribute) || ColumnParseUtil.getJDBCType(key.type) == null) continue; org.dom4j.Element result = resultMap.addElement("result"); result.addAttribute("column", key.column); result.addAttribute("property", key.attribute); result.addAttribute("jdbcType", ColumnParseUtil.getJDBCType(key.type)); } // 属性值中包含实体 for (AttributeColumnMap key : keysList) { if (ColumnParseUtil.getJDBCType(key.type) == null) { Class propertyClass = Class.forName(key.type); String propertyMapper = getDaoPackageName(propertyClass) + "." + propertyClass.getSimpleName() + "Mapper"; org.dom4j.Element association = resultMap.addElement("association"); association.addAttribute("property", key.attribute); association.addAttribute("column", key.column); association.addAttribute("resultMap", propertyMapper + ".BaseResultMap"); key.attribute = key.attribute + ".id"; key.type = "java.lang.Long"; } } org.dom4j.Element sql = root.addElement("sql"); sql.addAttribute("id", "Base_Column_List"); sql.setText(getColumns(keysList)); org.dom4j.Element select = root.addElement("select"); select.addAttribute("id", "selectByPrimaryKey"); select.addAttribute("resultMap", "BaseResultMap"); select.addAttribute("parameterType", "java.lang.Long"); select.addText("select"); org.dom4j.Element include = select.addElement("include"); include.addAttribute("refid", "Base_Column_List"); select.addText(String.format("from %s where %s = #{%s,jdbcType=BIGINT}", tableName, idKeys.column, idKeys.attribute)); org.dom4j.Element delete = root.addElement("delete"); delete.addAttribute("id", "deleteByPrimaryKey"); delete.addAttribute("parameterType", "java.lang.Long"); delete.setText(String.format("delete from %s where %s = #{%s,jdbcType=BIGINT}", tableName, idKeys.column, idKeys.attribute)); org.dom4j.Element insert = root.addElement("insert"); insert.addAttribute("id", "insert"); insert.addAttribute("parameterType", clz.getName()); insert.addAttribute("useGeneratedKeys", "true"); insert.addAttribute("keyProperty", "id"); StringBuffer text = new StringBuffer(); text.append(String.format("insert into %s (", tableName)); text.append(getColumns(keysList)); text.append(")"); text.append(" values ("); for (AttributeColumnMap acm : keysList) text.append(getKeyPair(acm)).append(","); text.deleteCharAt(text.length() - 1); text.append(")"); insert.setText(text.toString()); org.dom4j.Element insertSelective = root.addElement("insert"); insertSelective.addAttribute("id", "insertSelective"); insertSelective.addAttribute("parameterType", clz.getName()); insertSelective.addAttribute("useGeneratedKeys", "true"); insertSelective.addAttribute("keyProperty", "id"); insertSelective.addText("insert into " + tableName); org.dom4j.Element trim = insertSelective.addElement("trim"); trim.addAttribute("prefix", "("); trim.addAttribute("suffix", ")"); trim.addAttribute("suffixOverrides", ","); for (AttributeColumnMap acm : keysList) { org.dom4j.Element iff = trim.addElement("if"); iff.addAttribute("test", (acm.attribute.indexOf(".") > -1 ? acm.attribute.split("\\.")[0] : acm.attribute) + " != null"); iff.setText(acm.column + ","); } insertSelective.addText("values"); trim = insertSelective.addElement("trim"); trim.addAttribute("prefix", "("); trim.addAttribute("suffix", ")"); trim.addAttribute("suffixOverrides", ","); for (AttributeColumnMap acm : keysList) { org.dom4j.Element iff = trim.addElement("if"); iff.addAttribute("test", (acm.attribute.indexOf(".") > -1 ? acm.attribute.split("\\.")[0] : acm.attribute) + " != null"); iff.setText(getKeyPair(acm) + ","); } // update org.dom4j.Element update = root.addElement("update"); update.addAttribute("id", "updateByPrimaryKey"); update.addAttribute("parameterType", clz.getName()); text = new StringBuffer(String.format("update %s set ", tableName)); for (AttributeColumnMap acm : keysList) { if (acm.attribute.equalsIgnoreCase(idKeys.attribute)) continue; text.append(String.format("%s = #{%s,jdbcType=%s}", acm.column, acm.attribute, ColumnParseUtil.getJDBCType(acm.type))).append(","); } text.deleteCharAt(text.length() - 1); text.append(String.format(" where %s = #{%s,jdbcType=%s}", idKeys.column, idKeys.attribute, ColumnParseUtil.getJDBCType(idKeys.type))); update.setText(text.toString()); // updateSelective org.dom4j.Element updateSelective = root.addElement("update"); updateSelective.addAttribute("id", "updateByPrimaryKeySelective"); updateSelective.addAttribute("parameterType", clz.getName()); updateSelective.addText("update " + tableName); org.dom4j.Element set = updateSelective.addElement("set"); for (AttributeColumnMap acm : keysList) { if (acm.attribute.equalsIgnoreCase(idKeys.attribute)) continue; org.dom4j.Element iff = set.addElement("if"); iff.addAttribute("test", (acm.attribute.indexOf(".") > -1 ? acm.attribute.split("\\.")[0] : acm.attribute) + " != null"); iff.setText(acm.column + "=" + getKeyPair(acm) + ","); } updateSelective.addText(String.format(" where %s = #{%s,jdbcType=%s}", idKeys.column, idKeys.attribute, ColumnParseUtil.getJDBCType(idKeys.type))); // 创建mapper文件 if (!new File(basePath + "/mapper/").exists()) new File(basePath + "/mapper/").mkdirs(); if (!new File(mapperPath).exists()) new File(mapperPath).createNewFile(); XMLWriter writer = new XMLWriter(new FileOutputStream(new File(mapperPath)), OutputFormat.createPrettyPrint()); writer.setEscapeText(false);// 字符是否转义,默认true writer.write(document); writer.close(); } catch (Exception e) { e.printStackTrace(); } } private static AttributeColumnMap getAttributeColumnMapByAttribute(String attributeName, List list) { for (AttributeColumnMap acm : list) if (acm.attribute.equalsIgnoreCase(attributeName)) return acm; return null; } private static String getColumns(List list) { String columns = ""; for (AttributeColumnMap map : list) columns += map.column + ","; return columns.length() > 0 ? columns.substring(0, columns.length() - 1) : columns; } private static String getKeyPair(AttributeColumnMap map) { return String.format("#{%s,jdbcType=%s}", map.attribute, ColumnParseUtil.getJDBCType(map.type)); } } class AttributeColumnMap { String attribute; String column; String type; public AttributeColumnMap(String attribute, String column, String type) { this.attribute = attribute; this.column = column; this.type = type; } public AttributeColumnMap() { } }