好多段代码
许多段代码
1、项目地址与类的包名路径
String pro = System.getProperty("user.dir"); String pkg = CgSpringMvcIbatis.class.getPackage().getName().replaceAll("\\.","\\\\");
2、根据模板生成文件(用时需修改)
Template tJsp = cfg.getTemplate("js.ftl");// 模板名称 tJsp.setEncoding("UTF-8"); String jspDir = jMap.get("conpath").toString(); File fileDir = new File(jspDir); org.apache.commons.io.FileUtils.forceMkdir(fileDir); File output_service = new File(jspDir + "/default.js"); Writer writer_service = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output_service), "UTF-8")); tJsp.process(jMap, writer_service); writer_service.close();
3、泛型接口Ibatis
/** * Ibatis Dao 接口 * 通用增删改查\带分页\单条 * @author mywhile * @param <T> */ public abstract interface IbatisDao<T> { public void delete(String guid, String sqlMapId); public void save(T bean, String sqlMapId); public void update(T bean, String sqlMapId); public List<T> query(String sqlMapId); public T queryRowByGuid(String guid, String sqlMapId); public List<T> queryPageData(String sqlMapId, Map map, int pageNo, int pageSize); public int queryCountRow(Map map, String SqlMapId); }
4、泛型接口实现Ibatis
import java.util.List; import java.util.Map; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import sdcncsi.ibatis.test.dao.IbatisDao; @SuppressWarnings("unchecked") public class IbatisServicesImpl<T> extends SqlMapClientDaoSupport implements IbatisDao<T> { @Override public void delete(String guid, String sqlMapId) { getSqlMapClientTemplate().delete(sqlMapId, guid); } @Override public List<T> query(String sqlMapId) { return getSqlMapClientTemplate().queryForList(sqlMapId); } @Override public T queryRowByGuid(String guid, String sqlMapId) { return (T) getSqlMapClientTemplate().queryForObject(sqlMapId, guid); } @Override public void save(T bean, String sqlMapId) { getSqlMapClientTemplate().insert(sqlMapId, bean); } @Override public void update(T bean, String sqlMapId) { getSqlMapClientTemplate().update(sqlMapId, bean); } @Override public List<T> queryPageData(String sqlMapId, Map map, int pageNo, int pageSize) { map.put("pageNo", pageNo == 1 ? 0 : pageSize); map.put("pageSize", pageSize); return getSqlMapClientTemplate().queryForList(sqlMapId, map);//, pageNo, pageSize } @Override public int queryCountRow(Map map, String sqlMapId) { return (Integer) getSqlMapClientTemplate().queryForObject(sqlMapId, map); } }
5、Ibatis中取SQL
String sql = null; SqlMapClientImpl sqlmap = (SqlMapClientImpl)this.getSqlMapClient(); MappedStatement stmt = sqlmap.getMappedStatement(sqlMapId); Sql stmtSql = stmt.getSql(); SessionScope session = new SessionScope(); StatementScope scope = new StatementScope(session); scope.setStatement(stmt); sql = stmtSql.getSql(scope, map);
6、附件=Spring+Ibatis代码生成器;
7、文件读取
/** * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 * @param fileName 文件的名 */ public static void readFileByBytes(String fileName) { File file = new File(fileName); InputStream in = null; try { System.out.println("以字节为单位读取文件内容,一次读一个字节:"); // 一次读一个字节 in = new FileInputStream(file); int tempbyte; while ((tempbyte = in.read()) != -1) { System.out.write(tempbyte); } in.close(); } catch (IOException e) { e.printStackTrace(); return; } try { System.out.println("以字节为单位读取文件内容,一次读多个字节:"); // 一次读多个字节 byte[] tempbytes = new byte[100]; int byteread = 0; in = new FileInputStream(fileName); FileReadWrite.showAvailableBytes(in); // 读入多个字节到字节数组中,byteread为一次读入的字节数 while ((byteread = in.read(tempbytes)) != -1) { System.out.write(tempbytes, 0, byteread); } } catch (Exception e1) { e1.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e1) { } } } } /** * 以字符为单位读取文件,常用于读文本,数字等类型的文件 * @param fileName 文件名 */ public static void readFileByChars(String fileName) { File file = new File(fileName); Reader reader = null; try { System.out.println("以字符为单位读取文件内容,一次读一个字节:"); // 一次读一个字符 reader = new InputStreamReader(new FileInputStream(file)); int tempchar; while ((tempchar = reader.read()) != -1) { // 对于windows下,rn这两个字符在一起时,表示一个换行。 // 但如果这两个字符分开显示时,会换两次行。 // 因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。 if (((char) tempchar) != 'r') { System.out.print((char) tempchar); } } reader.close(); } catch (Exception e) { e.printStackTrace(); } try { System.out.println("以字符为单位读取文件内容,一次读多个字节:"); // 一次读多个字符 char[] tempchars = new char[30]; int charread = 0; reader = new InputStreamReader(new FileInputStream(fileName)); // 读入多个字符到字符数组中,charread为一次读取字符数 while ((charread = reader.read(tempchars)) != -1) { // 同样屏蔽掉r不显示 if ((charread == tempchars.length) && (tempchars[tempchars.length - 1] != 'r')) { System.out.print(tempchars); } else { for (int i = 0; i < charread; i++) { if (tempchars[i] == 'r') { continue; } else { System.out.print(tempchars[i]); } } } } } catch (Exception e1) { e1.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } } /** * 以行为单位读取文件,常用于读面向行的格式化文件 * @param fileName 文件名 */ public static void readFileByLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; try { System.out.println("以行为单位读取文件内容,一次读一整行:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { // 显示行号 System.out.println("line " + line + ": " + tempString); line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } } /** * 随机读取文件内容 * @param fileName 文件名 */ public static void readFileByRandomAccess(String fileName) { RandomAccessFile randomFile = null; try { System.out.println("随机读取一段文件内容:"); // 打开一个随机访问文件流,按只读方式 randomFile = new RandomAccessFile(fileName, "r"); // 文件长度,字节数 long fileLength = randomFile.length(); // 读文件的起始位置 int beginIndex = (fileLength > 4) ? 4 : 0; // 将读文件的开始位置移到beginIndex位置。 randomFile.seek(beginIndex); byte[] bytes = new byte[10]; int byteread = 0; // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。 // 将一次读取的字节数赋给byteread while ((byteread = randomFile.read(bytes)) != -1) { System.out.write(bytes, 0, byteread); } } catch (IOException e) { e.printStackTrace(); } finally { if (randomFile != null) { try { randomFile.close(); } catch (IOException e1) { } } } } /** * 显示输入流中还剩的字节数 * @param in */ private static void showAvailableBytes(InputStream in) { try { System.out.println("当前字节输入流中的字节数为:" + in.available()); } catch (IOException e) { e.printStackTrace(); } } /** * 将内容追加到文件尾部 * A方法追加文件:使用RandomAccessFile * @param fileName 文件名 * @param content 追加的内容 */ public static void appendMethodA(String fileName, String content) { try { // 打开一个随机访问文件流,按读写方式 RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); // 文件长度,字节数 long fileLength = randomFile.length(); // 将写文件指针移到文件尾。 randomFile.seek(fileLength); randomFile.writeBytes(content); randomFile.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 将内容追加到文件尾部 * B方法追加文件:使用FileWriter * @param fileName * @param content */ public static void appendMethodB(String fileName, String content) { try { // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 FileWriter writer = new FileWriter(fileName, true); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); } }
8、Myeclipse生成序列号
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class MyEclipseGEN { private static final String LL = "Decompiling this copyrighted software is a violation of both your license agreement and the Digital Millenium Copyright Act of 1998 (http://www.loc.gov/copyright/legislation/dmca.pdf). Under section 1204 of the DMCA, penalties range up to a $500,000 fine or up to five years imprisonment for a first offense. Think about it; pay for a license, avoid prosecution, and feel better about yourself."; public String getSerial(String userId, String licenseNum) { java.util.Calendar cal = java.util.Calendar.getInstance(); cal.add(1, 3); cal.add(6, -1); java.text.NumberFormat nf = new java.text.DecimalFormat("000"); licenseNum = nf.format(Integer.valueOf(licenseNum)); String verTime = new StringBuilder("-").append(new java.text.SimpleDateFormat("yyMMdd").format(cal.getTime())).append("0").toString(); String type = "YE3MP-"; String need = new StringBuilder(userId.substring(0, 1)).append(type).append("300").append(licenseNum).append(verTime).toString(); String dx = new StringBuilder(need).append(LL).append(userId).toString(); int suf = this.decode(dx); String code = new StringBuilder(need).append(String.valueOf(suf)).toString(); return this.change(code); } private int decode(String s) { int i; char[] ac; int j; int k; i = 0; ac = s.toCharArray(); j = 0; k = ac.length; while (j < k) { i = (31 * i) + ac[j]; j++; } return Math.abs(i); } private String change(String s) { byte[] abyte0; char[] ac; int i; int k; int j; abyte0 = s.getBytes(); ac = new char[s.length()]; i = 0; k = abyte0.length; while (i < k) { j = abyte0[i]; if ((j >= 48) && (j <= 57)) { j = (((j - 48) + 5) % 10) + 48; } else if ((j >= 65) && (j <= 90)) { j = (((j - 65) + 13) % 26) + 65; } else if ((j >= 97) && (j <= 122)) { j = (((j - 97) + 13) % 26) + 97; } ac[i] = (char) j; i++; } return String.valueOf(ac); } public MyEclipseGEN() { super(); } public static void main(String[] args) { try { System.out.println("please input register name:"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String userId = null; userId = reader.readLine(); MyEclipseGEN myeclipsegen = new MyEclipseGEN(); String res = myeclipsegen.getSerial(userId, "20"); System.out.println("Serial:" + res); reader.readLine(); } catch (IOException ex) { } } }
9、Myeclipse7.1插件添加
public class Plugin { private String path; public Plugin(String path) { this.path = path; } public void print() { List list = getFileList(path); if (list == null) { return; } int length = list.size(); for (int i = 0; i < length; i++) { String result = ""; String thePath = getFormatPath(getString(list.get(i))); File file = new File(thePath); if (file.isDirectory()) { String fileName = file.getName(); if (fileName.indexOf("_") < 0) { continue; } String[] filenames = fileName.split("_"); String filename1 = filenames[0]; String filename2 = filenames[1]; result = filename1 + "," + filename2 + ",file:/" + path + "\\" + fileName + "\\,4,false"; System.out.println(result); } else if (file.isFile()) { String fileName = file.getName(); if (fileName.indexOf("_") < 0) { continue; } String[] filenames = fileName.split("_"); String filename1 = filenames[0]; String filename2 = filenames[1].substring(0, filenames[1] .lastIndexOf(".")); result = filename1 + "," + filename2 + ",file:/" + path + "\\" + fileName + ",4,false"; System.out.println(result); } } } public List getFileList(String path) { path = getFormatPath(path); path = path + "/"; File filePath = new File(path); if (!filePath.isDirectory()) { return null; } String[] filelist = filePath.list(); List filelistFilter = new ArrayList(); for (int i = 0; i < filelist.length; i++) { String tempfilename = getFormatPath(path + filelist[i]); filelistFilter.add(tempfilename); } return filelistFilter; } public String getString(Object object) { if (object == null) { return ""; } return String.valueOf(object); } public String getFormatPath(String path) { path = path.replaceAll("\\\\", "/"); path = path.replaceAll("//", "/"); return path; } public static void main(String[] args) { new Plugin("D:\\Program Files\\MyEclipse 7.1\\plugins").print(); } }