自己封装mybatis模板生成
来源:互联网 发布:大学生网络家教 兼职 编辑:程序博客网 时间:2024/06/05 09:50
第一步
通过加入hibernate框架,通过hibernate来自动生成model层的东西,用完撤出hibernate框架。
第二步
package com.yrmt.controller;import java.io.*;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;/** * Created by 存 on 2016/8/4. */public class YRMTool { //在idea下model层的路径 public String y_path = "E:\\IDEAWorkSpace\\finance\\src\\main\\java\\com\\yrmt\\model"; //要生成的路径 public String x_path = "E:\\Temp\\com\\yrmt"; //包名 public String pack_name = "com.yrmt"; //生成时间 private String time = ""; private ArrayList<String> mname = new ArrayList<String>(); public static void main(String[] args) throws Exception { new YRMTool().domain(); } public void domain() { setMname();//设置数组 SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd"); time = sf.format(new Date());//设置写入时间 File dao_file = new File(x_path + "\\dao"); dao_file.mkdirs();//生成dao包 File service_file = new File(x_path + "\\service\\impl"); service_file.mkdirs();//生成service包 和impl包 File conf_file = new File(x_path + "\\cm"); conf_file.mkdir();//资源配置包 for (String cname : mname) {//生成代码 writeDao(cname);//写入dao包的所有 writeSer(cname);//写入service包所有 writeImp(cname);//写入impl包所有 writeMapper(cname);//写入对应mapper配置文件 } writeMyBatis_config();//mybatis配置 } private void writeMapper(String name) { File file = new File(x_path + "\\cm\\" + name + "Mapper.xml"); BufferedWriter bw = null; try { file.createNewFile(); bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(file))); } catch (IOException e) { System.out.println(name + "Mapper.xml 创建失败"); } StringBuffer sb = new StringBuffer(); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); sb.append("<!DOCTYPE mapper"); sb.append(" PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\""); sb.append("\"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">"); sb.append("<mapper namespace=\"" + pack_name + ".dao." + name + "DAO\">"); sb.append("</mapper>"); try { bw.write(sb.toString()); bw.flush(); bw.close(); } catch (IOException e) { System.out.println(name + "Mapper.xml 更新失败"); } } //mybatis资源配置 private void writeMyBatis_config() { File file = new File(x_path + "\\cm\\mybatis-config.xml"); BufferedWriter bw = null; try { file.createNewFile(); bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(file))); } catch (IOException e) { System.out.println("mybatis_congif.xml 创建失败"); } StringBuffer sb = new StringBuffer(); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); sb.append("<!DOCTYPE configuration "); sb.append("PUBLIC \"-//mybatis.org//DTD Config 3.0//EN\""); sb.append("\"http://mybatis.org/dtd/mybatis-3-config.dtd\">"); sb.append("<configuration><!-- 命名空间 --><typeAliases>"); for (String n : mname) { sb.append(" <typeAlias alias=\"" + n + "\" type=\"" + pack_name + ".model." + n + "\"/>"); } sb.append("</configuration>"); try { bw.write(sb.toString()); bw.flush(); bw.close(); } catch (IOException e) { System.out.println("文件写入失败"); } } private void writeImp(String name) { File file = new File(x_path + "\\service\\impl\\" + name + "ServiceImpl.java"); BufferedWriter bw = null; try { file.createNewFile(); bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(file))); } catch (Exception e) { System.out.println("创建-" + name + "impl-错误"); } StringBuffer sb_imp = new StringBuffer();//向内添加代码 sb_imp.append("package " + pack_name + ".service.impl;"); sb_imp.append("import org.springframework.beans.factory.annotation.Autowired;"); sb_imp.append("import org.springframework.stereotype.Service;"); sb_imp.append("import " + pack_name + ".dao." + name + "DAO;"); sb_imp.append("import " + pack_name + ".model." + name + ";"); sb_imp.append("import " + pack_name + ".service." + name + "Service;"); sb_imp.append("@Service\r\n public class " + name + "ServiceImpl implements " + name + "Service{"); String lname = name.toLowerCase(); sb_imp.append("@Autowired \r\n private " + name + "DAO " + lname + "DAO;}"); try { bw.write(sb_imp.toString()); bw.flush(); bw.close(); } catch (IOException e) { System.out.println("impl包写入失败:" + name); } } private void writeSer(String name) { File file = new File(x_path + "\\service\\" + name + "Service.java"); BufferedWriter bw = null; try { file.createNewFile(); bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(file))); } catch (Exception e) { System.out.println("创建-" + name + "Service-错误"); } StringBuffer sb_ser = new StringBuffer();//向内添加代码 sb_ser.append("package " + pack_name + ".service;"); sb_ser.append("import " + pack_name + ".model." + name + ";"); sb_ser.append("public interface " + name + "Service{}"); try { bw.write(sb_ser.toString()); bw.flush(); bw.close(); } catch (IOException e) { System.out.println("service包写入错误:" + name); } } private void writeDao(String name) { File file = new File(x_path + "\\dao\\" + name + "DAO.java"); BufferedWriter bw = null; try { file.createNewFile(); bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(file))); } catch (Exception e) { System.out.println("创建-" + name + "DAO-错误"); } StringBuffer sb_dao = new StringBuffer();//向内添加代码 sb_dao.append("package " + pack_name + ".dao;"); sb_dao.append("import " + pack_name + ".model." + name + ";"); sb_dao.append("/**\n * Created by 存 on " + time + ".\n*/"); sb_dao.append("public interface " + name + "DAO {}"); try { bw.write(sb_dao.toString()); bw.flush(); bw.close(); } catch (IOException e) { System.out.println("dao 写入失败:" + name); } } //对于有些model层下生成的有其他注解的,此方法可以去掉上面注解,保留 @Override 注解 private void remove_annotation(File file) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(file))); StringBuffer sb = new StringBuffer(); String temp; while ((temp = br.readLine()) != null) { if (temp.indexOf("@Override") != -1 || temp.indexOf("@") == -1) { sb.append(temp); } } br.close(); BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(file))); bw.write(sb.toString()); bw.flush(); bw.close(); } //将model层下所有的实体类的名字放在 mname下 private void setMname() { File m_path = new File(y_path); for (File f : m_path.listFiles()) { mname.add(f.getName().replace(".java", "")); } }}
0 0
- 自己封装mybatis模板生成
- mybatis通过association来封装自己的对象属性
- 自己扩展封装Hibernate模板DAO-之一简介
- 封装模板
- 【ssm】mybatis逆向工程生成的模板方法使用小结
- [置顶]springmvc+mybatis,在mybatis逆向工程的基础上使用模板自动生成controller层代码
- 自己扩展封装Hibernate模板DAO-之二单独使用(hibernate)
- 自己扩展封装Hibernate模板DAO-之三Hibernate+JSP的分页功能
- mybatis BaseDao封装
- mybatis association封装对象
- Mybatis通用封装
- Mybatis的sql封装
- Mybatis的sql封装
- 修改HTMLTestRunner模板,并将HtmlTestRunner进行二次封装,使报告生成的更方便
- 自己写了个类来封装hibernate的hql语句的生成
- CList封装模板类
- 模板分页封装
- 模板分页封装
- UITextFeild
- 搭建开源论坛之JForum安装+简易二次修改
- Android NDK教程<->-- 简介
- Resource注解(重要)
- SQL SERVER 2008 发布订阅表结构变化
- 自己封装mybatis模板生成
- 一张图让你没明白 java string 的 内存分配
- Search for a Range
- html标签的block、inline分类明细
- I/O多路复用之poll模型
- 手机在加载本地图片时,由于图片过大,加载的图片过多容易产生OOM现象的解决办法。
- JDBC:基本用法
- Mysql 常用命令
- UIImageView