package com.hskj.operateDbUtils;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
*
* @创建时间:2017年3月14日 @创建人:lili @类说明: @修改时间: @修改人: @修改说明:
*/
public class ReadDb {
private static String url = "jdbc:mysql://10.0.1.1/newspaper?useUnicode=true&characterEncoding=utf8&autoReconnect=true";// 简单写法:url
private static String user = "root";
private static String password = "hskj2015";
private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;
public static void main(String[] args) {
//System.out.println(">>>");
ReadDb obj = new ReadDb();
obj.getCon();
//obj.query();
List
for (String str : tbList) {
System.out.println("///////////////////////");
if("categoryTb".equalsIgnoreCase(str.trim())){
//System.out.println("表名:"+str);
System.out.println("表名:"+str);
List
//for (String col : colList) {
//System.out.println("列:"+col);
//}
//
GenerateClass.generatePojo(str, colList, "f:/pojo/","com.hskj.model");//生成pojo
GenerateClass.generateDao(str, colList, "f:/dao/","com.hskj.dao");//生成 dao
GenerateClass.generateService(str, colList, "f:/service/","com.hskj.service");//生成 dao
}
//break;
}
}
/**
* 获得表信息
*/
public List
List
try {
DatabaseMetaData m_DBMetaData = conn.getMetaData();
ResultSet tbRs = m_DBMetaData.getTables(null, "%", "%", new String[] { "TABLE" });
String tmp = "";
while (tbRs.next()) {
tbList.add(tbRs.getString("TABLE_NAME"));
// System.out.println(tbRs.getString("TABLE_NAME"));
}
if(null != tbRs ){
tbRs.close();
tbRs = null;
}
} catch (Exception e) {
System.out.println("获得数据库表名错误");
e.printStackTrace();
}
return tbList;
}
/**
* 获得表里所有 字段
*
* @param tbName
*/
public List
List
try {
DatabaseMetaData m_DBMetaData = conn.getMetaData();
ResultSet colRet = m_DBMetaData.getColumns(null, "%", tbName, "%");
String col = "";
String colType = "";
while (colRet.next()) {
col = colRet.getString("COLUMN_NAME");
colsList.add(col);
//colType = colRet.getString("TYPE_NAME");
}
if(null != colRet ){
colRet.close();
colRet = null;
}
} catch (Exception e) {
System.out.println("获得" + tbName + "表字段错误");
e.printStackTrace();
}
return colsList;
}
/**
* 查询
*/
public void query() {
try {
stmt = conn.createStatement();
String sql = "select * from userTb";// dept这张表有deptno,deptname和age这三个字段
rs = stmt.executeQuery(sql);// 执行sql语句
while (rs.next()) {
System.out.print(" --------------- ");
System.out.print(rs.getString("id") + " , ");
System.out.print(rs.getString("account") + " , ");
System.out.println(rs.getString("phoneNumber") + " ");
}
} catch (SQLException e) {
System.out.println("数据操作错误");
e.printStackTrace();
}
}
/**
* 获得连接
*/
public void getCon() {
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载mysq驱动
} catch (ClassNotFoundException e) {
System.out.println("驱动加载错误");
e.printStackTrace();// 打印出错详细信息
}
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
System.out.println("数据库链接错误");
e.printStackTrace();
}
}
/**
* 关闭连接
*/
public void colseCon() {
// 关闭数据库
try {
if (null != rs) {
rs.close();
rs = null;
}
if (null != stmt) {
stmt.close();
stmt = null;
}
if (null != conn) {
conn.close();
conn = null;
}
} catch (Exception e) {
System.out.println("数据库关闭错误");
e.printStackTrace();
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////
package com.hskj.operateDbUtils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.hskj.dao.UserDao;
import com.hskj.dao.UserDaoImpl;
import com.hskj.model.Admin;
import com.hskj.model.User;
import com.hskj.service.UserService;
import com.hskj.utils.BaseDao;
import com.hskj.utils.BasoDaoImpl;
/**
*
* @创建时间:2017年3月14日 @创建人:lili
* @类说明:生成类文件 @修改时间: @修改人: @修改说明:
*/
public class GenerateClass {
// private static String filePath = "f:";
public static void main(String[] args) {
String tbName = "adminTb";
tbName = tbName.replaceAll("(?i)tb", "");
System.out.println(tbName);// 替换所有大小写的tb
tbName = tbName.substring(0, 1).toUpperCase() + tbName.substring(1);
System.out.println(tbName);// 替换所有大小写的tb
generatePojo(tbName, null, "/pojo/", "com.hskj.model");
// if(){
//
// }
}
/**
* 生成dao
*
* @param tbName
* 表名称
* @param colList
* 访表下面相应的字段
*/
public static void generateService(String tbName, List
String replacTbStr = tbName.replaceAll("(?i)tb", "");// 替换所有大小写的tb
String fileName = replacTbStr;
fileName = fileName.substring(0, 1).toUpperCase() + fileName.substring(1);// 首字母大写
fileName = fileName.trim();
try {
// 生成dao文件
File file = new File(fileNamePath + fileName + "Service.java");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
StringBuffer sb = new StringBuffer();
sb.append(" package " + packageStr + ";\r\n");
sb.append(" import java.util.List;\r\n");
sb.append(" import com.hskj.model." + fileName + ";\r\n");
sb.append(" \r\n");
sb.append(" public interface " + fileName + "Service{ \r\n");
sb.append(" \r\n");
//根据id获得Obj
sb.append(" /** \r\n");
sb.append(" * 根据用户id获得Obj对象信息 \r\n");
sb.append(" * \r\n");
sb.append(" * @param id \r\n");
sb.append(" * @return \r\n");
sb.append(" */ \r\n");
sb.append(" public "+fileName+" getObjByid(String id); \r\n");
//根据ids删除
sb.append(" /** \r\n");
sb.append(" * 批量删除Obj \r\n");
sb.append(" * \r\n");
sb.append(" * @param ids \r\n");
sb.append(" */ \r\n");
sb.append(" public void delete"+fileName+"ByIds(List
//根据条件查询
sb.append(" /** \r\n");
sb.append(" * 根据对象信息条件查询对象信息 \r\n");
sb.append(" * \r\n");
sb.append(" * @param o \r\n");
sb.append(" * @return \r\n");
sb.append(" */ \r\n");
sb.append(" public List<"+fileName+"> getDatasBycods("+fileName+" o); \r\n");
//添加和修改
sb.append(" /** \r\n");
sb.append(" * 添加或修改对象 \r\n");
sb.append(" * @param o 对象实体 \r\n");
sb.append(" * @return \r\n");
sb.append(" */ \r\n");
sb.append(" public void addOrUpdate"+fileName+"("+fileName+" o); \r\n");
sb.append(" \r\n");
sb.append(" } " + "\r\n");
bw.write(sb.toString());
bw.close();
// 生成daoImpl文件
// 生成dao文件
file = new File(fileNamePath + "/Impl/" + fileName + "ServiceImpl.java");
if (!file.exists()) {
file.createNewFile();
}
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
sb = new StringBuffer();
sb.append(" package " + packageStr + ".Impl;\r\n");
sb.append(" import java.util.List; \r\n");
sb.append(" import java.util.ArrayList; \r\n");
sb.append(" import java.util.Arrays; \r\n");
sb.append(" import javax.persistence.criteria.CriteriaBuilder; \r\n");
sb.append(" import javax.persistence.criteria.CriteriaQuery; \r\n");
sb.append(" import javax.persistence.criteria.Predicate; \r\n");
sb.append(" import javax.persistence.criteria.Root; \r\n");
sb.append(" import org.apache.commons.lang3.StringUtils; \r\n");
sb.append(" import org.springframework.beans.factory.annotation.Autowired; \r\n");
sb.append(" import org.springframework.data.domain.Page; \r\n");
sb.append(" import org.springframework.data.domain.PageRequest; \r\n");
sb.append(" import org.springframework.data.domain.Pageable; \r\n");
sb.append(" import org.springframework.data.jpa.domain.Specification; \r\n");
sb.append(" import org.springframework.stereotype.Service; \r\n");
String tmp0 = packageStr.replace("service", "model");
String tmp1 = packageStr.replace("service", "dao");
sb.append(" import "+ tmp0 +"."+ fileName + "; \r\n");
sb.append(" import "+ tmp1 +"."+ fileName + "Dao; \r\n");
sb.append(" import "+ tmp1 +"."+ fileName + "DaoImpl; \r\n");
sb.append(" import "+ packageStr+"." + fileName + "Service; \r\n");
sb.append(" \r\n");
sb.append(" @Service(\""+replacTbStr+"Service\") \r\n");
sb.append(" public class "+fileName+"ServiceImpl implements "+fileName+"Service{ \r\n");
sb.append(" \r\n");
sb.append(" @Autowired \r\n");
sb.append(" private "+fileName+"Dao "+replacTbStr+"Dao; \r\n");
sb.append(" @Autowired \r\n");
sb.append(" private "+fileName+"DaoImpl "+replacTbStr+"DaoImpl; \r\n");
//根据id获得Obj
sb.append(" /** \r\n");
sb.append(" * 根据用户id获得Obj对象信息 \r\n");
sb.append(" * \r\n");
sb.append(" * @param id \r\n");
sb.append(" * @return \r\n");
sb.append(" */ \r\n");
sb.append(" @Override \r\n");
sb.append(" public "+fileName+" getObjByid(String id){ \r\n");
sb.append(" return "+replacTbStr+"Dao.getObjByid(id); \r\n");
sb.append(" } \r\n");
//根据ids删除
sb.append(" /** \r\n");
sb.append(" * 批量删除Obj \r\n");
sb.append(" * \r\n");
sb.append(" * @param ids \r\n");
sb.append(" */ \r\n");
sb.append(" public void delete"+fileName+"ByIds(List
sb.append(" "+replacTbStr+"Dao.delete"+fileName+"(ids); \r\n");
sb.append(" } \r\n");
//根据条件查询
sb.append(" /** \r\n");
sb.append(" * 根据对象信息条件查询对象信息 \r\n");
sb.append(" * \r\n");
sb.append(" * @param o \r\n");
sb.append(" * @return \r\n");
sb.append(" */ \r\n");
sb.append(" public List<"+fileName+"> getDatasBycods("+fileName+" o){ \r\n");
sb.append(" return "+replacTbStr+"DaoImpl.getDatasBycods(o); \r\n");
sb.append(" } \r\n");
//添加和修改
sb.append(" /** \r\n");
sb.append(" * 添加或修改对象 \r\n");
sb.append(" * @param o 对象实体 \r\n");
sb.append(" * @return \r\n");
sb.append(" */ \r\n");
sb.append(" public void addOrUpdate"+fileName+"("+fileName+" o){ \r\n");
sb.append(" "+replacTbStr+"Dao.save(o); \r\n");
sb.append(" } \r\n");
sb.append(" \r\n");
sb.append(" } " + "\r\n");
bw.write(sb.toString());
bw.close();
System.out.println("Done");
} catch (IOException e) {
System.out.println("写dao文件出错:" + tbName);
e.printStackTrace();
}
}
/**
* 生成dao
*
* @param tbName
* 表名称
* @param colList
* 访表下面相应的字段
*/
public static void generateDao(String tbName, List
String fileName = tbName.replaceAll("(?i)tb", "");// 替换所有大小写的tb
fileName = fileName.substring(0, 1).toUpperCase() + fileName.substring(1);// 首字母大写
fileName = fileName.trim();
try {
// 生成dao文件
File file = new File(fileNamePath + fileName + "Dao.java");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
StringBuffer sb = new StringBuffer();
sb.append(" package " + packageStr + ";\r\n");
sb.append(" import org.springframework.data.jpa.repository.Modifying;\r\n");
sb.append(" import org.springframework.data.jpa.repository.Query;\r\n");
sb.append(" import org.springframework.transaction.annotation.Transactional;\r\n");
sb.append(" import com.hskj.model." + fileName + ";\r\n");
sb.append(" import com.hskj.utils.BaseDao;\r\n");
sb.append(" import java.util.List; \r\n");
sb.append(" \r\n");
sb.append(" public interface " + fileName + "Dao extends BaseDao<" + fileName + ", Integer>{ \r\n");
sb.append(" \r\n");
sb.append(" /** \r\n");
sb.append(" * 根据用户id获得Obj对象信息\r\n");
sb.append(" * @param id\r\n");
sb.append(" * @return \r\n");
sb.append(" */ \r\n");
sb.append(" @Query(\"select o from " + fileName + " o where o.id=?1 and o.isDelete = '0' \") \r\n");
sb.append(" public "+fileName+" getObjByid(String id); \r\n");
sb.append(" \r\n");
sb.append(" /** \r\n");
sb.append(" * 批量删除Obj \r\n");
sb.append(" * @param ids \r\n");
sb.append(" */ \r\n");
sb.append(" @Transactional \r\n");
sb.append(" @Modifying \r\n");
sb.append(" @Query(\"update " + fileName + " o set o.isDelete='1' where o.id in(?1)\") \r\n");
sb.append(" public void delete" + fileName + "(List
sb.append(" } " + "\r\n");
bw.write(sb.toString());
bw.close();
// 生成daoImpl文件
// 生成dao文件
file = new File(fileNamePath + fileName + "DaoImpl.java");
if (!file.exists()) {
file.createNewFile();
}
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
sb = new StringBuffer();
sb.append(" package " + packageStr + "; \r\n");
sb.append(" import java.util.ArrayList; \r\n");
sb.append(" import java.util.List; \r\n");
sb.append(" import javax.persistence.Query; \r\n");
sb.append(" import org.springframework.stereotype.Repository; \r\n");
sb.append(" import org.springframework.transaction.annotation.Transactional; \r\n");
sb.append(" import com.hskj.model." + fileName + "; \r\n");
sb.append(" import com.hskj.utils.BasoDaoImpl; \r\n");
sb.append(" \r\n");
// sb.append(" @Repository \r\n");
sb.append(" public class " + fileName + "DaoImpl extends BasoDaoImpl{ \r\n");
sb.append(" /** \r\n");
sb.append(" * 根据对象信息条件查询对象信息 \r\n");
sb.append(" * @param o \r\n");
sb.append(" * @return \r\n");
sb.append(" */ \r\n");
sb.append(" public List<"+fileName+"> getDatasBycods("+fileName+" o) { \r\n");
sb.append(" StringBuilder hql = new StringBuilder(); \r\n");
sb.append(" hql.append(\"select o from "+fileName+" o where o.isDelete = '0' \"); \r\n");
sb.append(" Query query = em.createQuery(hql.toString()); \r\n");
sb.append(" List<"+fileName+"> vList = new ArrayList<"+fileName+">(); \r\n");
sb.append(" vList = query.getResultList(); \r\n");
sb.append(" return vList; \r\n");
sb.append(" } \r\n");
sb.append(" } " + "\r\n");
bw.write(sb.toString());
bw.close();
System.out.println("Done");
} catch (IOException e) {
System.out.println("写dao文件出错:" + tbName);
e.printStackTrace();
}
}
/**
* 生成pojo
*
* @param tbName
* 表名称
* @param colList
* 访表下面相应的字段
*/
public static void generatePojo(String tbName, List
// System.out.println("表名:" + tbName);
// String packageStr = "com.hskj.model";
String fileName = tbName.replaceAll("(?i)tb", "");// 替换所有大小写的tb
fileName = fileName.substring(0, 1).toUpperCase() + fileName.substring(1);// 首字母大写
try {
// String content = "This is the content to write into file";
File file = new File(fileNamePath + fileName + ".java");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
StringBuffer sb = new StringBuffer();
sb.append(" package " + packageStr + ";\r\n");
sb.append(" import javax.persistence.*; \r\n");
sb.append(" @Entity \r\n");
sb.append(" @Table(name = \"" + tbName + "\") \r\n");
sb.append(" public class " + fileName + "{ \r\n");
// sb.append(" ");
// 属性
for (String col : colList) {
// System.out.println("列:" + col);.
sb.append(" private String " + col + "; \r\n");
}
// 构造方法
sb.append(" public " + fileName + "() { " + "\r\n");
sb.append(" super(); " + "\r\n");
sb.append(" } " + "\r\n");
String tmp = "";
for (String col : colList) {
if ("id".equalsIgnoreCase(col)) {// 如果是主键id
sb.append(" @Id " + "\r\n");
sb.append(" @Column(name = \"" + col + "\") " + "\r\n");
tmp = col;
tmp = tmp.substring(0, 1).toUpperCase() + tmp.substring(1);// 首字母大写
sb.append(" public String get" + tmp + "() { " + "\r\n");
sb.append(" return id; " + "\r\n");
sb.append(" } " + "\r\n");
sb.append(" public void set" + tmp + "(String id) { " + "\r\n");
sb.append(" this.id = id; " + "\r\n");
sb.append(" } " + "\r\n");
} else {
tmp = col;
tmp = tmp.substring(0, 1).toUpperCase() + tmp.substring(1);// 首字母大写
sb.append(" public String get" + tmp + "() { " + "\r\n");
sb.append(" return " + col + "; " + "\r\n");
sb.append(" } " + "\r\n");
sb.append(" public void set" + tmp + "(String " + col + ") { " + "\r\n");
sb.append(" this." + col + " = " + col + "; " + "\r\n");
sb.append(" } " + "\r\n");
}
}
sb.append(" } " + "\r\n");
bw.write(sb.toString());
bw.close();
System.out.println("Done");
} catch (IOException e) {
System.out.println("写pojo文件出错:" + tbName);
e.printStackTrace();
}
}
}