In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
< 0) { tableComment = ""; } else { tableComment = createDDL.substring(index + 9); tableComment = tableComment.substring(0, tableComment.length() - 1); } } } catch (SQLException e) { } finally { closeConnection(connection, statement, rs); } return tableComment; } /** * * @author Marvis * @date Jul 13, 2017 4:45:26 PM * @param connection * @param statement * @param rs */ private void closeConnection(Connection connection, Statement statement, ResultSet rs) { try { if (null != rs) rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (statement != null) statement.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (connection != null) connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } /** * This plugin is always valid - no properties are required */ @Override public boolean validate(List warnings) { return true; }}二、分页和分组代码生成 这里,我对Dao Model进行了通用方法的抽取,建立通用基类。同时,对其进行了一些扩展,增加分页和分组。 先对基类进行介绍。 1)BaseMapperpackage cn.xxx.core.base.dao;import java.util.List;import org.apache.ibatis.annotations.Param;public interface BaseMapper { long countByExample(Example example); int deleteByExample(Example example); int deleteByPrimaryKey(ID id); int insert(T record); int insertSelective(T record); List selectByExample(Example example); T selectByPrimaryKey(ID id); int updateByExampleSelective(@Param("record") T record, @Param("example") Example example); int updateByExample(@Param("record") T record, @Param("example") Example example); int updateByPrimaryKeySelective(T record); int updateByPrimaryKey(T record);}2)BaseExamplepackage cn.xxx.core.base.model;/** * BaseExample 基类 * @ClassName BaseExample * @Description 增加分页参数 * @author Marvis * @date Jul 31, 2017 11:26:53 AM */public abstract class BaseExample { protected PageInfo pageInfo; protected String groupByClause; public PageInfo getPageInfo() { return pageInfo; } public void setPageInfo(PageInfo pageInfo) { this.pageInfo = pageInfo; } public String getGroupByClause() { return groupByClause; } public void setGroupByClause(String groupByClause) { this.groupByClause = groupByClause; }}3)PageInfopackage cn.xxx.core.base.model;import com.fasterxml.jackson.annotation.JsonIgnore;/** * 分页查询参数类 * * @author * */public class PageInfo { public static final int Default_PageSize = 20; // 当前页码 protected int currentPage = 1; // 总页数 protected int totalPage; // 总记录数 protected int totalCount; // 每页条数 protected int pageSize = Default_PageSize; // 开始 protected int pageBegin = 0; // 结束 protected int pageEnd = 20; /** * bean起始坐标(不包含) */ private Integer pageBeginId = null; public static final String PageQuery_classname = "pageInfo"; /** * 将分布参数传入处理,最终计算出当前页码PageQuery_currPage,开始坐标PageQuery_star, * 结束坐标PageQuery_end,总页数PageQuery_Psize * * 页数从1开始计数 * * @param totalCount * 记录总数 * @param pageSize * 每页显示个数 * @param currentPage * 当前页码 */ public void setPageParams(int totalCount, int pageSize, int currentPage) { this.totalPage = pageSize == 0 ? 1 : (int) Math.ceil((double) totalCount / (double) pageSize); this.totalCount = totalCount; this.pageSize = pageSize; this.currentPage = currentPage; float Psize_l = totalCount / (float) (this.pageSize); if (currentPage < 2) { currentPage = 1; pageBegin = 0; } else if (currentPage > < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); if (childNode.getNodeType() == Node.ELEMENT_NODE && "property".equals(childNode.getNodeName())) parseProperty(serviceGeneratorConfiguration, childNode); } } @Override public Configuration parseConfiguration(Element rootNode) throws XMLParserException { Configuration configuration = new Configuration(); NodeList nodeList = rootNode.getChildNodes(); for (int i = 0; i < nodeList.getLength(); ++i) { Node childNode = nodeList.item(i); if (childNode.getNodeType() != 1) { continue; } if ("properties".equals(childNode.getNodeName())) parseProperties(configuration, childNode); else if ("classPathEntry".equals(childNode.getNodeName())) parseClassPathEntry(configuration, childNode); else if ("context".equals(childNode.getNodeName())) { parseContext(configuration, childNode); } } return configuration; } private void parseContext(Configuration configuration, Node node) { Properties attributes = parseAttributes(node); String defaultModelType = attributes.getProperty("defaultModelType"); String targetRuntime = attributes.getProperty("targetRuntime"); String introspectedColumnImpl = attributes.getProperty("introspectedColumnImpl"); String id = attributes.getProperty("id"); ModelType mt = defaultModelType != null ? ModelType.getModelType(defaultModelType) : null; Context context = new ContextOverride(mt); context.setId(id); if (StringUtility.stringHasValue(introspectedColumnImpl)) context.setIntrospectedColumnImpl(introspectedColumnImpl); if (StringUtility.stringHasValue(targetRuntime)) context.setTargetRuntime(targetRuntime); configuration.addContext(context); NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); if (childNode.getNodeType() != 1) continue; if ("property".equals(childNode.getNodeName())) { parseProperty(context, childNode); continue; } if ("plugin".equals(childNode.getNodeName())) { parsePlugin(context, childNode); continue; } if ("commentGenerator".equals(childNode.getNodeName())) { parseCommentGenerator(context, childNode); continue; } if ("jdbcConnection".equals(childNode.getNodeName())) { parseJdbcConnection(context, childNode); continue; } if ("connectionFactory".equals(childNode.getNodeName())) { parseConnectionFactory(context, childNode); continue; } if ("javaModelGenerator".equals(childNode.getNodeName())) { parseJavaModelGenerator(context, childNode); continue; } if ("javaTypeResolver".equals(childNode.getNodeName())) { parseJavaTypeResolver(context, childNode); continue; } if ("sqlMapGenerator".equals(childNode.getNodeName())) { parseSqlMapGenerator(context, childNode); continue; } if ("javaClientGenerator".equals(childNode.getNodeName())) { parseJavaClientGenerator(context, childNode); continue; } if ("javaServiceGenerator".equals(childNode.getNodeName())) { parseJavaServiceGenerator(context, childNode); continue; } if ("table".equals(childNode.getNodeName())) parseTable(context, childNode); } } private void parsePlugin(Context context, Node node) { PluginConfiguration pluginConfiguration = new PluginConfiguration(); context.addPluginConfiguration(pluginConfiguration); Properties attributes = parseAttributes(node); String type = attributes.getProperty("type"); pluginConfiguration.setConfigurationType(type); NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); if (childNode.getNodeType() == 1 && "property".equals(childNode.getNodeName())) parseProperty(pluginConfiguration, childNode); } } private void parseJavaClientGenerator(Context context, Node node) { JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration(); context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration); Properties attributes = parseAttributes(node); String type = attributes.getProperty("type"); String targetPackage = attributes.getProperty("targetPackage"); String targetProject = attributes.getProperty("targetProject"); String implementationPackage = attributes.getProperty("implementationPackage"); javaClientGeneratorConfiguration.setConfigurationType(type); javaClientGeneratorConfiguration.setTargetPackage(targetPackage); javaClientGeneratorConfiguration.setTargetProject(targetProject); javaClientGeneratorConfiguration.setImplementationPackage(implementationPackage); NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node childNode = nodeList.item(i); if (childNode.getNodeType() == 1 && "property".equals(childNode.getNodeName())) parseProperty(javaClientGeneratorConfiguration, childNode); } }}6)ServiceGeneratorConfigurationpackage run.override.service;import java.util.List;import org.mybatis.generator.api.dom.xml.Attribute;import org.mybatis.generator.api.dom.xml.XmlElement;import org.mybatis.generator.config.PropertyHolder;import org.mybatis.generator.internal.util.StringUtility;import org.mybatis.generator.internal.util.messages.Messages;public class ServiceGeneratorConfiguration extends PropertyHolder { private String targetPackage; private String implementationPackage; private String targetProject; /** * */ public ServiceGeneratorConfiguration() { super(); } public String getTargetPackage() { return targetPackage; } public void setTargetPackage(String targetPackage) { this.targetPackage = targetPackage; } public String getImplementationPackage() { return implementationPackage; } public void setImplementationPackage(String implementationPackage) { this.implementationPackage = implementationPackage; } public String getTargetProject() { return targetProject; } public void setTargetProject(String targetProject) { this.targetProject = targetProject; } public XmlElement toXmlElement() { XmlElement answer = new XmlElement("javaServiceGenerator"); if (targetPackage != null) { answer.addAttribute(new Attribute("targetPackage", targetPackage)); } if (implementationPackage != null) { answer.addAttribute(new Attribute("implementationPackage", targetPackage)); } if (targetProject != null) { answer.addAttribute(new Attribute("targetProject", targetProject)); } addPropertyXmlElements(answer); return answer; } @SuppressWarnings({ "rawtypes", "unchecked" }) public void validate(List errors, String contextId) { if (!StringUtility.stringHasValue(getTargetProject())) errors.add(Messages.getString("ValidationError.102", contextId)); if (!StringUtility.stringHasValue(getTargetPackage())) errors.add(Messages.getString("ValidationError.112", "ServiceGenerator", contextId)); if (!StringUtility.stringHasValue(getImplementationPackage())) errors.add(Messages.getString("ValidationError.120", contextId)); }}7)ConfigurationParserOverridepackage run.override.service;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.Reader;import java.util.ArrayList;import java.util.List;import java.util.Properties;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.mybatis.generator.config.Configuration;import org.mybatis.generator.config.xml.ConfigurationParser;import org.mybatis.generator.config.xml.MyBatisGeneratorConfigurationParser;import org.mybatis.generator.config.xml.ParserEntityResolver;import org.mybatis.generator.config.xml.ParserErrorHandler;import org.mybatis.generator.exception.XMLParserException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;public class ConfigurationParserOverride extends ConfigurationParser { private List warnings; private List parseErrors; private Properties extraProperties; public ConfigurationParserOverride(List warnings) { this(null, warnings); } public ConfigurationParserOverride(Properties extraProperties, List warnings) { super(extraProperties, warnings); this.extraProperties = extraProperties; if (warnings == null) this.warnings = new ArrayList(); else { this.warnings = warnings; } this.parseErrors = new ArrayList(); } @Override public Configuration parseConfiguration(File inputFile) throws IOException, XMLParserException { FileReader fr = new FileReader(inputFile); return parseConfiguration(fr); } @Override public Configuration parseConfiguration(InputStream inputStream) throws IOException, XMLParserException { InputSource is = new InputSource(inputStream); return parseConfiguration(is); } @Override public Configuration parseConfiguration(Reader reader) throws IOException, XMLParserException { InputSource is = new InputSource(reader); return parseConfiguration(is); } private Configuration parseConfiguration(InputSource inputSource) throws IOException, XMLParserException { this.parseErrors.clear(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); try { DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(new ParserEntityResolver()); ParserErrorHandler handler = new ParserErrorHandler(this.warnings, this.parseErrors); builder.setErrorHandler(handler); Document document = null; try { document = builder.parse(inputSource); } catch (SAXParseException e) { throw new XMLParserException(this.parseErrors); } catch (SAXException e) { if (e.getException() == null) this.parseErrors.add(e.getMessage()); else { this.parseErrors.add(e.getException().getMessage()); } } if (this.parseErrors.size() >0) {throw new XMLParserException (this.parseErrors);} Element rootNode = document.getDocumentElement (); Configuration config = parseMyBatisGeneratorConfiguration (rootNode); if (this.parseErrors.size () > 0) {throw new XMLParserException (this.parseErrors);} return config } catch (ParserConfigurationException e) {this.parseErrors.add (e.getMessage ()); throw new XMLParserException (this.parseErrors);}} private Configuration parseMyBatisGeneratorConfiguration (Element rootNode) throws XMLParserException {/ / replace MyBatisGeneratorConfigurationParser MyBatisGeneratorConfigurationParser parser = new MyBatisGeneratorConfigurationParserOverride (this.extraProperties); return parser.parseConfiguration (rootNode);}} VII, PluginChain
String the above extension Plugin together through inheritance (SerializablePlugin may not be needed in some projects, so do not add Chain. At the same time, others can also make changes to Chain as needed.
Package run.override;import run.override.service.ServiceLayerPlugin;public class PluginChain extends ServiceLayerPlugin {} VIII, generatorConfig.xml
Add javaServiceGenerator-related configuration tags. This article uses internal DTD as an example, and can also be implemented through external DTD or xsd.
1) generatorConfig.xml] > 9. Main starts package run.generator Import java.io.File;import java.util.ArrayList;import java.util.List;import org.mybatis.generator.api.MyBatisGenerator;import org.mybatis.generator.config.Configuration;import org.mybatis.generator.internal.DefaultShellCallback;import run.override.service.ConfigurationParserOverride;public class Generator {public void generator () throws Exception {List warnings = new ArrayList (); boolean overwrite = true; File configFile = new File ("generatorConfig.xml") / replace ConfigurationParser ConfigurationParserOverride cp = new ConfigurationParserOverride (warnings); Configuration config = cp.parseConfiguration (configFile); DefaultShellCallback callback = new DefaultShellCallback (overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator (config, callback, warnings); myBatisGenerator.generate (null);} public static void main (String [] args) throws Exception {try {Generator generator = new Generator (); generator.generator () } catch (Exception e) {e.printStackTrace ();} about what is the custom generation of Mybatis-Generator extensions, so much for sharing here. I hope the above content can be helpful to you and you can learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.