import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
*
* @ClassName: Codec mysql
* @Description: 生成mayatis的mapper配置文件和实体类
* 使用时拷贝注释代码BaseMapper code,生成项目的BaseMapper
* 根据项目需要配置enmu Path,有任何问题请联系309147857@qq.com
* mapper配置文件中无分页参数,这个在后续添加,建议不使用分页插件,因为所有的分页插件都是非官方支持,而mybatis的缓存非常好,
* 不能保证第三方分页插件和官方mybatis缓存的兼容性。
* 除了生成实体bean,没有生成其他po和vo,使用时请根据需要自行添加。
* 生成之mapper配置文件中有<resultMap/>,但其内容为空,配置这一项的原因是希望用户能使用mybatis的高级映射之多对一单向关联,
* 为什么要使用多对一关联,而不使用其他呢,因为其比较简单,使用最多,能降低sql join的代码。
* 那为什么不使用全部高级映射和双向关联呢,因为学习门槛高,大部分colder没用过mybatis之高级映射。
* <resultMap type="liyu.test.springboot.model.User" id="User">
* <association property="role" column="role_id"
* select="liyu.test.springboot.mapper.role.RoleMapper.get">
* </association>
* </resultMap>
* @author: liyu
* @date: 2017年12月12日 下午4:52:18
*/
public class MysqlCodec {
/**
* 首先配置,Path枚举分三部分:
* 1,那个库、那个表、那个类;
* 2,实体类位置、mapperXML位置、mapper接口位置;
* 3,jdbc连接的配置;
* 4,BaseMapper,最后将附上BaseMapper的代码。
*/
enum Path{
DB_SCHEMA ("mybatis"),
DB_TABLE ("t_person"),
DB_BEAN ("Person"),
DIR_BEAN ("liyu.test.mybatis.model"),
DIR_MAPPER ("liyu.test.mybatis.mapper"),
DIR_DAO ("liyu.test.mybatis.mapper"),
JDBC_URL ("jdbc:mysql://localhost:3306/mybatis"),
JDBC_DRIVER ("com.mysql.jdbc.Driver"),
JDBC_USER ("root"),
JDBC_PSWORD ("root"),
BASE_MAPPER ("liyu.test.mybatis.BaseMapper");
private String value;Path(String value) {this.value = value;}
}
/**文件分割符*/
public static final String sp = File.separator;
private void run(){
//1.当前时间
long timestampt = System.currentTimeMillis();
//2.dataSource
DataSource dataSource = new DataSource(
Path.JDBC_URL.value,
Path.JDBC_DRIVER.value,
Path.JDBC_USER.value,
Path.JDBC_PSWORD.value
);
try {
//3,指定代码生成的根路径,并逐级创建
File path = new File(System.getProperty("user.home") + sp + "codec_" + timestampt);
createDir(path,Path.DIR_BEAN.value);
createDir(path,Path.DIR_MAPPER.value);
createDir(path,Path.DIR_DAO.value);
Connection conn = dataSource.getConn();
String sql =
"select "
+ "COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT,COLUMN_KEY,EXTRA "
+ "from information_schema.columns "
+ "where table_schema=? and table_name=?";
String sql_ = "select TABLE_NAME,TABLE_COMMENT from information_schema.tables where table_schema = ? "
+ "and table_name = ?";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, Path.DB_SCHEMA.value);
preparedStatement.setString(2, Path.DB_TABLE.value);
PreparedStatement preparedStatement_ = conn.prepareStatement(sql_);
preparedStatement_.setString(1, Path.DB_SCHEMA.value);
preparedStatement_.setString(2, Path.DB_TABLE.value);
ResultSet resultSet_ = preparedStatement_.executeQuery();
String tableCommen = "";
while(resultSet_.next()){
tableCommen = resultSet_.getString(2);
}
//4,执行查询返回jdbc之ResultSet
ResultSet resultSet = preparedStatement.executeQuery();
//5,开始拼接
StringBuffer
javaBean = new StringBuffer(),
javaBeanGetsetM = new StringBuffer(),
mapperXml = new StringBuffer(),
daoBean = new StringBuffer();
String id = null;
ArrayLists<String> columnsExceptId = new ArrayLists<String>();
javaBean.append("package "+Path.DIR_BEAN.value+";\n");
javaBean.append("/**\n*"+tableCommen+"("+Path.DB_TABLE.value+")\n**/\npublic class "+Path.DB_BEAN.value+" implements java.io.Serializable{"+"\n");
javaBean.append("\n private static final long serialVersionUID = 1L;\n");
mapperXml.append(
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"+
"<!DOCTYPE mapper\n"+
" PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"\n"+
" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n"+
"<mapper namespace=\""+Path.DIR_MAPPER.value+"."+Path.DB_BEAN.value+"Mapper\">\n");
while(resultSet.next()){
javaBean.append(" /**"+resultSet.getString(3)+"*/\n"+" private "+typeConvert(resultSet.getString(2))+javaNaming(resultSet.getString(1))+";\n");
getsetMothod(resultSet.getString(1),resultSet.getString(2),javaBeanGetsetM);
if(!resultSet.getString(4).equals(""))
id = resultSet.getString(1);
else{
columnsExceptId.add(resultSet.getString(1));
}
}
javaBean.append(javaBeanGetsetM);
javaBean.append("\n}");
mapperXml.append(" <resultMap type=\""+Path.DIR_BEAN.value+"."+Path.DB_BEAN.value+"\" id=\"BaseResultMap\"></resultMap>");
mapperXml.append("\n <sql id=\"cols\">\n "+columnsExceptId.joinc()+id+" as "+javaNaming(id)+"\n </sql>");
mapperXml.append("\n <sql id=\"whereSql\">\n <where>");
mapperXml.append("\n <if test=\""+javaNaming(id)+" != null\">\n and "+id+"=#{"+javaNaming(id)+"}\n </if>");
for(String str:columnsExceptId)
mapperXml.append("\n <if test=\""+javaNaming(str)+" != null\">\n and "+str+"=#{"+javaNaming(str)+"}\n </if>");
mapperXml.append("\n </where>");
mapperXml.append("\n </sql>");
mapperXml.append("\n <insert id=\"create\">\n insert into "+Path.DB_TABLE.value+"("+columnsExceptId.join()+") values("+columnsExceptId.joinv()+")\n </insert>");
mapperXml.append("\n <update id=\"merge\">\n update "+Path.DB_TABLE.value+" set "+columnsExceptId.joinu()+" where "+id+"=#{"+javaNaming(id)+"}\n </update>");
mapperXml.append("\n <update id=\"updateColumn\">\n update ${tableName} set ${columnName}=#{value} where "+id+" = #{primaryKey}\n </update>");
mapperXml.append("\n <delete id=\"delete\">\n delete from "+Path.DB_TABLE.value+" where "+id+"=#{"+javaNaming(id)+"}\n </delete>");
mapperXml.append("\n <delete id=\"deleteBatch\" parameterType=\"java.util.List\">\n <foreach collection=\"list\" item=\"item\" index=\"index\" open=\"begin\" close=\";end;\" separator=\";\">\n delete from "+Path.DB_TABLE.value+" where "+id+"=#{item."+javaNaming(id)+"}\n </foreach>\n </delete>");
mapperXml.append("\n <select id=\"findOne\" resultMap=\"BaseResultMap\">\n select <include refid=\"cols\"/> from "+Path.DB_TABLE.value+" where "+id+"=#{"+javaNaming(id)+"}\n </select>");
mapperXml.append("\n <select id=\"findList\" resultMap=\"BaseResultMap\">\n select <include refid=\"cols\"/> from "+Path.DB_TABLE.value+" <include refid=\"whereSql\"/>\n </select>");
mapperXml.append("\n <select id=\"findCount\" resultType=\"java.lang.Integer\">\n select count(1) from "+Path.DB_TABLE.value+" <include refid=\"whereSql\"/>\n </select>");
mapperXml.append("\n</mapper>");
daoBean.append("package "+Path.DIR_DAO.value+";\n");
daoBean.append("import org.springframework.stereotype.Repository;\n");
daoBean.append("import "+Path.DIR_BEAN.value+"."+Path.DB_BEAN.value+";\n");
daoBean.append("@Repository\n");
daoBean.append("public interface "+Path.DB_BEAN.value+"Mapper extends BaseMapper<"+Path.DB_BEAN.value+">{"+"\n");
daoBean.append("}");
//6,写入文件
byte[] bytes = javaBean.toString().getBytes(Charset.defaultCharset());
File file = new File(path,Path.DIR_BEAN.value.replace(".", sp)+sp+Path.DB_BEAN.value+".java");
file.createNewFile();
Files.write(file.toPath(), bytes);
byte[] xmlbytes = mapperXml.toString().getBytes(Charset.defaultCharset());
File xmlfile = new File(path,Path.DIR_MAPPER.value.replace(".", sp)+sp+Path.DB_BEAN.value+"Mapper.xml");
xmlfile.createNewFile();
Files.write(xmlfile.toPath(), xmlbytes);
byte[] daobytes = daoBean.toString().getBytes(Charset.defaultCharset());
File daofile = new File(path,Path.DIR_MAPPER.value.replace(".", sp)+sp+Path.DB_BEAN.value+"Mapper.java");
daofile.createNewFile();
Files.write(daofile.toPath(), daobytes);
//7,关闭资源
dataSource.close(resultSet,preparedStatement,resultSet_,preparedStatement_,conn);
//8,打开窗口
java.awt.Desktop.getDesktop().open(path);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new MysqlCodec().run();
}
private static void getsetMothod(String string, String type, StringBuffer getsetM) {
string = javaNaming(string);
String str = string.substring(0, 1).toUpperCase() + string.substring(1);
String javaType = typeConvert(type);
getsetM.append("\n public void set"+str+"("+javaType+string+"){\n this."+string+"="+string+";\n }");
getsetM.append("\n public "+javaType+"get"+str+"(){\n return this."+string+";\n }");
}
public static String javaNaming(String column){
column = column.toLowerCase();
if(column.startsWith("_")){
throw new RuntimeException("数据库column不能以下划线开头!");
}else if(column.indexOf("_")>-1){
String[] fields = column.split("_");
StringBuilder sbuilder = new StringBuilder(fields[0]);
for (int i = 1; i < fields.length; i++) {
char[] cs=fields[i].toCharArray();
cs[0]-=32;
sbuilder.append(String.valueOf(cs));
}
return sbuilder.toString();
}
return column;
}
private static String typeConvert(String dbType){
switch (dbType) {
case "bigint":
return "Integer ";
case "int":
return "Integer ";
case "varchar":
return "String ";
case "datetime":
return "java.util.Date ";
default:
break;
}
throw new RuntimeException("类型缺失或错误!");
}
private static void createDir(File path, String value) {
mkDir(new File(path,value.replace(".", sp)));
}
private static void mkDir(File file) {
if (file.getParentFile().exists()) file.mkdir();
else{mkDir(file.getParentFile()); file.mkdir();}
}
class ArrayLists<E> extends ArrayList<E>{
private static final long serialVersionUID = 1L;
public String join(){
StringBuffer sb = new StringBuffer();
for(int i=0;i<this.size();i++){
sb.append((i==this.size()-1)?this.get(i):(this.get(i)+","));
}
return sb.toString();
}
public String joinv(){
StringBuffer sb = new StringBuffer();
for(int i=0;i<this.size();i++){
sb.append((i==this.size()-1)?val(this.get(i)):val(this.get(i))+",");
}
return sb.toString();
}
public String joinu(){
StringBuffer sb = new StringBuffer();
for(int i=0;i<this.size();i++){
if(i==this.size()-1)
sb.append(this.get(i)+"=#{"+javaNaming((String)this.get(i))+"}");
else
sb.append(this.get(i)+"=#{"+javaNaming((String)this.get(i))+"},");
}
return sb.toString();
}
public String joinc(){
StringBuffer sb = new StringBuffer();
for(int i=0;i<this.size();i++){
sb.append(this.get(i)+" as "+javaNaming((String)this.get(i))+",");
}
return sb.toString();
}
private String val(E e){
return "#{"+javaNaming((String)e)+"}";
}
}
/**
*
* @ClassName: DataSource
* @Description:
* @author: liyu
* @date: 2017年12月13日 上午10:33:53
*/
class DataSource{
private String url,driver,username,password;
private javax.sql.DataSource dataSource;
public DataSource(String url, String driver, String username, String password) {
super();
this.url = url;
this.driver = driver;
this.username = username;
this.password = password;
}
public DataSource(javax.sql.DataSource dataSource) {
super();
this.dataSource = dataSource;
}
public Connection getConn() throws SQLException, ClassNotFoundException{
if(this.dataSource!=null){
return dataSource.getConnection();
}else{
Class.forName(this.driver);
return DriverManager.getConnection(this.url, this.username, this.password);
}
}
public void close(AutoCloseable...closeable){
if(closeable!=null){
try {
for (AutoCloseable autoCloseable : closeable) {
autoCloseable.close();
}
} catch (Exception e) {
}
}
}
}
}
/*
* ================================BaseMapper code=====================================
public interface BaseMapper <T>{
public void create(T t);
public void merge(T t);
public boolean updateColumn(UpdateColumnWapper updateColumnWapper);
public void delete(T t);
public void deleteBatch(List<T> list);
public T findOne(T t);
public List<T> findList(T t);
public Integer findCount(T t);
}
public class UpdateColumnWapper {
private String tableName;
private String columnName;
private Object value;
private Object primaryKey;
public UpdateColumnWapper(String tableName, String columnName, Object value, Object primaryKey) {
this.tableName = tableName;
this.columnName = columnName;
this.primaryKey = primaryKey;
this.value = value;
}
public String getTableName() {
return tableName;
}
public String getColumnName() {
return columnName;
}
public Object getPrimaryKey() {
return primaryKey;
}
public Object getValue() {
return value;
}
}
=======================================================================================*/
package liyu.test.mybatis.codec;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
*
* @ClassName: Codec oracle
* @Description: 生成mayatis的mapper配置文件和实体类
* 使用时拷贝注释代码BaseMapper code,生成项目的BaseMapper
* 根据项目需要配置enmu Path,有任何问题请联系309147857@qq.com
* mapper配置文件中无分页参数,这个在后续添加,建议不使用分页插件,因为所有的分页插件都是非官方支持,而mybatis的缓存非常好,
* 不能保证第三方分页插件和官方mybatis缓存的兼容性。
* 除了生成实体bean,没有生成其他po和vo,使用时请根据需要自行添加。
* 生成之mapper配置文件中有<resultMap/>,但其内容为空,配置这一项的原因是希望用户能使用mybatis的高级映射之多对一单向关联,
* 为什么要使用多对一关联,而不使用其他呢,因为其比较简单,使用最多,能降低sql join的代码。
* 那为什么不使用全部高级映射和双向关联呢,因为学习门槛高,大部分colder没用过mybatis之高级映射。
* <resultMap type="liyu.test.springboot.model.User" id="User">
* <association property="role" column="role_id"
* select="liyu.test.springboot.mapper.role.RoleMapper.get">
* </association>
* </resultMap>
* @author: liyu
* @date: 2017年12月12日 下午4:52:18
*/
public class OracleCodec {
/**
* 首先配置,Path枚举分三部分:
* 1,那个库、那个表、那个类;
* 2,实体类位置、mapperXML位置、mapper接口位置;
* 3,jdbc连接的配置;
* 4,BaseMapper,最后将附上BaseMapper的代码。
*/
enum Path{
DB_TABLE ("FK_BGD"),
DB_BEAN ("BaoGuanDan"),
DIR_BEAN ("liyu.test.mybatis.model"),
DIR_MAPPER ("liyu.test.mybatis.mapper"),
DIR_DAO ("liyu.test.mybatis.mapper"),
JDBC_URL ("jdbc:oracle:thin:@192.168.100.252:1521:zssd"),
JDBC_DRIVER ("oracle.jdbc.OracleDriver"),
JDBC_USER ("cktsfk"),
JDBC_PSWORD ("cktsfk"),
BASE_MAPPER ("liyu.test.mybatis.BaseMapper");
private String value;Path(String value) {this.value = value;}
}
/**文件分割符*/
public static final String sp = File.separator;
private void run(){
//1.当前时间
long timestampt = System.currentTimeMillis();
//2.dataSource
DataSource dataSource = new DataSource(
Path.JDBC_URL.value,
Path.JDBC_DRIVER.value,
Path.JDBC_USER.value,
Path.JDBC_PSWORD.value
);
try {
//3,指定代码生成的根路径,并逐级创建
File path = new File(System.getProperty("user.home") + sp + "codec_" + timestampt);
createDir(path,Path.DIR_BEAN.value);
createDir(path,Path.DIR_MAPPER.value);
createDir(path,Path.DIR_DAO.value);
Connection conn = dataSource.getConn();
String sql =
"SELECT t.COLUMN_NAME, t.DATA_TYPE, t1.COMMENTS COLUMN_COMMENT, t.DATA_SCALE "+
"FROM USER_TAB_COLS t left join USER_COL_COMMENTS t1 "+
"on t.TABLE_NAME = t1.TABLE_NAME AND t.COLUMN_NAME = t1.COLUMN_NAME "+
"where t.TABLE_NAME=upper(?) "+
"order by t.column_id";
String sql_ =
"select COMMENTS from USER_TAB_COMMENTS WHERE TABLE_NAME=upper(?)";
String sql__ =
"select cu.column_name "+
"from user_cons_columns cu, user_constraints au "+
"where cu.constraint_name = au.constraint_name "+
"and au.constraint_type = 'P' "+
"and au.table_name = upper(?)";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, Path.DB_TABLE.value);
PreparedStatement preparedStatement_ = conn.prepareStatement(sql_);
preparedStatement_.setString(1, Path.DB_TABLE.value);
PreparedStatement preparedStatement__ = conn.prepareStatement(sql__);
preparedStatement__.setString(1, Path.DB_TABLE.value);
String tableCommen = "";
ResultSet resultSet_ = preparedStatement_.executeQuery();
while(resultSet_.next()){
tableCommen = resultSet_.getString(1);
}
String id = "";
ResultSet resultSet__ = preparedStatement__.executeQuery();
while(resultSet__.next()){
id = resultSet__.getString(1);
}
//4,执行查询返回jdbc之ResultSet
ResultSet resultSet = preparedStatement.executeQuery();
//5,开始拼接
StringBuffer
javaBean = new StringBuffer(),
javaBeanGetsetM = new StringBuffer(),
mapperXml = new StringBuffer(),
daoBean = new StringBuffer();
ArrayLists<String> columnsExceptId = new ArrayLists<String>();
javaBean.append("package "+Path.DIR_BEAN.value+";\n");
javaBean.append("/**\n*"+tableCommen+"("+Path.DB_TABLE.value+")\n**/\npublic class "+Path.DB_BEAN.value+" implements java.io.Serializable{"+"\n");
javaBean.append("\n private static final long serialVersionUID = 1L;\n");
mapperXml.append(
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"+
"<!DOCTYPE mapper\n"+
" PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"\n"+
" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n"+
"<mapper namespace=\""+Path.DIR_MAPPER.value+"."+Path.DB_BEAN.value+"Mapper\">\n");
while(resultSet.next()){
javaBean.append(" /**"+resultSet.getString(3)+"*/\n"+" private "+typeConvert(resultSet.getString(2),resultSet.getInt(4))+javaNaming(resultSet.getString(1))+";\n");
getsetMothod(resultSet.getString(1),resultSet.getString(2),resultSet.getInt(4),javaBeanGetsetM);
if(!resultSet.getString(1).equals(id))columnsExceptId.add(resultSet.getString(1));
}
javaBean.append(javaBeanGetsetM);
javaBean.append("\n}");
mapperXml.append(" <resultMap type=\""+Path.DIR_BEAN.value+"."+Path.DB_BEAN.value+"\" id=\"BaseResultMap\"></resultMap>");
mapperXml.append("\n <sql id=\"cols\">\n "+id+" as "+javaNaming(id)+","+columnsExceptId.joinc()+"\n </sql>");
mapperXml.append("\n <sql id=\"whereSql\">\n <where>");
mapperXml.append("\n <if test=\""+javaNaming(id)+" != null\">\n and "+id+"=#{"+javaNaming(id)+"}\n </if>");
for(String str:columnsExceptId)
mapperXml.append("\n <if test=\""+javaNaming(str)+" != null\">\n and "+str+"=#{"+javaNaming(str)+"}\n </if>");
mapperXml.append("\n </where>");
mapperXml.append("\n </sql>");
mapperXml.append("\n <insert id=\"create\">\n insert into "+Path.DB_TABLE.value+"("+id+","+columnsExceptId.join()+") values(#{"+javaNaming(id)+"},"+columnsExceptId.joinv()+")\n </insert>");
mapperXml.append("\n <update id=\"merge\">\n update "+Path.DB_TABLE.value+" set "+columnsExceptId.joinu()+" where "+id+"=#{"+javaNaming(id)+"}\n </update>");
mapperXml.append("\n <update id=\"updateColumn\">\n update ${tableName} set ${columnName}=#{value} where "+id+" = #{primaryKey}\n </update>");
mapperXml.append("\n <delete id=\"delete\">\n delete from "+Path.DB_TABLE.value+" where "+id+"=#{"+javaNaming(id)+"}\n </delete>");
mapperXml.append("\n <delete id=\"deleteBatch\" parameterType=\"java.util.List\">\n <foreach collection=\"list\" item=\"item\" index=\"index\" open=\"begin\" close=\";end;\" separator=\";\">\n delete from "+Path.DB_TABLE.value+" where "+id+"=#{item."+javaNaming(id)+"}\n </foreach>\n </delete>");
mapperXml.append("\n <select id=\"findOne\" resultMap=\"BaseResultMap\">\n select <include refid=\"cols\"/> from "+Path.DB_TABLE.value+" where "+id+"=#{"+javaNaming(id)+"}\n </select>");
mapperXml.append("\n <select id=\"findList\" resultMap=\"BaseResultMap\">\n select <include refid=\"cols\"/> from "+Path.DB_TABLE.value+" <include refid=\"whereSql\"/>\n </select>");
mapperXml.append("\n <select id=\"findCount\" resultType=\"java.lang.Integer\">\n select count(1) from "+Path.DB_TABLE.value+" <include refid=\"whereSql\"/>\n </select>");
mapperXml.append("\n</mapper>");
daoBean.append("package "+Path.DIR_DAO.value+";\n");
daoBean.append("import org.springframework.stereotype.Repository;\n");
daoBean.append("import "+Path.DIR_BEAN.value+"."+Path.DB_BEAN.value+";\n");
daoBean.append("@Repository\n");
daoBean.append("public interface "+Path.DB_BEAN.value+"Mapper extends BaseMapper<"+Path.DB_BEAN.value+">{"+"\n");
daoBean.append("}");
//6,写入文件
byte[] bytes = javaBean.toString().getBytes(Charset.defaultCharset());
File file = new File(path,Path.DIR_BEAN.value.replace(".", sp)+sp+Path.DB_BEAN.value+".java");
file.createNewFile();
Files.write(file.toPath(), bytes);
byte[] xmlbytes = mapperXml.toString().getBytes(Charset.defaultCharset());
File xmlfile = new File(path,Path.DIR_MAPPER.value.replace(".", sp)+sp+Path.DB_BEAN.value+"Mapper.xml");
xmlfile.createNewFile();
Files.write(xmlfile.toPath(), xmlbytes);
byte[] daobytes = daoBean.toString().getBytes(Charset.defaultCharset());
File daofile = new File(path,Path.DIR_MAPPER.value.replace(".", sp)+sp+Path.DB_BEAN.value+"Mapper.java");
daofile.createNewFile();
Files.write(daofile.toPath(), daobytes);
//7,关闭资源
dataSource.close(resultSet,preparedStatement,resultSet_,preparedStatement_,resultSet__,preparedStatement__,conn);
//8,打开窗口
java.awt.Desktop.getDesktop().open(path);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new OracleCodec().run();
}
private static void getsetMothod(String string, String type, Integer data_scale, StringBuffer getsetM) {
string = javaNaming(string);
String str = string.substring(0, 1).toUpperCase() + string.substring(1);
String javaType = typeConvert(type, data_scale);
getsetM.append("\n public void set"+str+"("+javaType+string+"){\n this."+string+"="+string+";\n }");
getsetM.append("\n public "+javaType+"get"+str+"(){\n return this."+string+";\n }");
}
public static String javaNaming(String column){
column = column.toLowerCase();
if(column.startsWith("_")){
throw new RuntimeException("数据库column不能以下划线开头!");
}else if(column.indexOf("_")>-1){
String[] fields = column.split("_");
StringBuilder sbuilder = new StringBuilder(fields[0]);
for (int i = 1; i < fields.length; i++) {
char[] cs=fields[i].toCharArray();
cs[0]-=32;
sbuilder.append(String.valueOf(cs));
}
return sbuilder.toString();
}
return column;
}
private static String typeConvert(String dbType,Integer data_scale){
switch (dbType) {
case "VARCHAR2":
return "String ";
case "NUMBER":
return data_scale==0?"Integer ":"java.math.BigDecimal ";
case "TIMESTAMP(6)":
return "java.util.Date ";
case "DATE":
return "java.util.Date ";
case "CHAR":
return "String ";
default:
break;
}
throw new RuntimeException("类型缺失或错误!");
}
private static void createDir(File path, String value) {
mkDir(new File(path,value.replace(".", sp)));
}
private static void mkDir(File file) {
if (file.getParentFile().exists()) file.mkdir();
else{mkDir(file.getParentFile()); file.mkdir();}
}
class ArrayLists<E> extends ArrayList<E>{
private static final long serialVersionUID = 1L;
public String join(){
StringBuffer sb = new StringBuffer();
for(int i=0;i<this.size();i++){
sb.append((i==this.size()-1)?this.get(i):(this.get(i)+","));
}
return sb.toString();
}
public String joinv(){
StringBuffer sb = new StringBuffer();
for(int i=0;i<this.size();i++){
sb.append((i==this.size()-1)?val(this.get(i)):val(this.get(i))+",");
}
return sb.toString();
}
public String joinu(){
StringBuffer sb = new StringBuffer();
for(int i=0;i<this.size();i++){
if(i==this.size()-1)
sb.append(this.get(i)+"=#{"+javaNaming((String)this.get(i))+"}");
else
sb.append(this.get(i)+"=#{"+javaNaming((String)this.get(i))+"},");
}
return sb.toString();
}
public String joinc(){
StringBuffer sb = new StringBuffer();
for(int i=0;i<this.size();i++){
if(i==this.size()-1)
sb.append(this.get(i)+" as "+javaNaming((String)this.get(i)));
else
sb.append(this.get(i)+" as "+javaNaming((String)this.get(i))+",");
}
return sb.toString();
}
private String val(E e){
return "#{"+javaNaming((String)e)+"}";
}
}
/**
*
* @ClassName: DataSource
* @Description:
* @author: liyu
* @date: 2017年12月13日 上午10:33:53
*/
class DataSource{
private String url,driver,username,password;
private javax.sql.DataSource dataSource;
public DataSource(String url, String driver, String username, String password) {
super();
this.url = url;
this.driver = driver;
this.username = username;
this.password = password;
}
public DataSource(javax.sql.DataSource dataSource) {
super();
this.dataSource = dataSource;
}
public Connection getConn() throws SQLException, ClassNotFoundException{
if(this.dataSource!=null){
return dataSource.getConnection();
}else{
Class.forName(this.driver);
return DriverManager.getConnection(this.url, this.username, this.password);
}
}
public void close(AutoCloseable...closeable){
if(closeable!=null){
try {
for (AutoCloseable autoCloseable : closeable) {
autoCloseable.close();
}
} catch (Exception e) {
}
}
}
}
}
/*
* ================================BaseMapper code=====================================
public interface BaseMapper <T>{
public void create(T t);
public void delete(T t);
public void deleteBatch(List<T> list);
public void merge(T t);
public T findOne(T t);
public List<T> findList(T t);
public int findCount(T t);
public boolean updateColumn(UpdateColumnWapper updateColumnWapper);
}
public class UpdateColumnWapper {
private String tableName;
private String columnName;
private Object value;
private Object primaryKey;
public UpdateColumnWapper(String tableName, String columnName, Object value, Object primaryKey) {
this.tableName = tableName;
this.columnName = columnName;
this.primaryKey = primaryKey;
this.value = value;
}
public String getTableName() {
return tableName;
}
public String getColumnName() {
return columnName;
}
public Object getPrimaryKey() {
return primaryKey;
}
public Object getValue() {
return value;
}
}
=======================================================================================*/
package liyu.test.springboot_v2.mapper;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
@Service
public final class EnhanceMapper extends SqlSessionDaoSupport implements ApplicationContextAware{
static Logger logger = LoggerFactory.getLogger(EnhanceMapper.class);
public static final String findlist = "findlist";
public static final String findone = "findone";
public static final String findcount = "findcount";
public static final String insert = "insert";
public static final String merge = "merge";
public static final String deleteBatch = "deleteBatch";
public static final String dynamicUpdate = "dynamicUpdate";
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SqlSessionFactory factory = applicationContext.getBean(SqlSessionFactory.class);
if(logger.isInfoEnabled()){
Configuration configuration = factory.getConfiguration();
Collection<?> mappedStatements = configuration.getMappedStatements();
Set<String> methods = new TreeSet<String>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
for (Object object : mappedStatements) {
if(object instanceof MappedStatement){
MappedStatement m = ((MappedStatement)object);
methods.add(m.getId());
}
}
System.out.println("==============================================");
for (String str : methods) {
System.out.println("*"+str);
}
System.out.println("==============================================");
}
super.setSqlSessionFactory(factory);
}
public static final String PAGENO = "pageNo";
public static final String PAGESIZE = "pageSize";
private String _all(String method, Class<?> type){
return type.getName()+"."+method;
}
/**
* @Description: 查询列表
*/
public <T> List<T> findList(String method,Object parameter,Class<T> type){
return this.getSqlSession().selectList(
_all(method, type), parameter);
}
/**
* @Description: 查询实体
*/
public <T> T findOne(String method,Object parameter,Class<T> type){
return this.getSqlSession().selectOne(
_all(method, type), parameter);
}
/**
* @Description: 查询总数
*/
public <T> Long findCount(String method,Object parameter,Class<T> type){
return this.getSqlSession().selectOne(
_all(method, type), parameter);
}
/**
* @Description: 查询分页
*/
public <T> Page<T> findPage(String method,String countMethod, PageParam page, Object parameter, Class<T> type){
Map<String, Object> map = bean2mapWithPageParam(parameter,page);
return new Page<T>(
page.getPageNo(),
page.getPageSize(),
this.findCount(countMethod, map, type).intValue(),
this.findList(method, map, type)
);
}
/**
* @Description: 增删改操作,改操作是全量合并
*/
public <T> int exccute(Class<T> type,String method,Object parameter){
if(parameter instanceof UpdateColumnWapper[]){
int i = 0;
//由于mysql不支持匿名块,故采用分批次调用:
/*<delete id="deleteBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
delete from table where id=#{item.id}
</foreach>
</delete>*/
for (UpdateColumnWapper el : (UpdateColumnWapper[])parameter) {
i += this.getSqlSession().update(
_all(method, type), el);
}
return i;
}else{
return this.getSqlSession().update(
_all(method, type), parameter);
}
}
public static Map<String, Object> bean2map (Object bean){
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter!=null ? getter.invoke(bean) : null;
map.put(key, value);
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
private static Map<String, Object> bean2mapWithPageParam (Object bean,PageParam page){
Map<String, Object> map = bean2map(bean);
map.put(PAGENO, page.getPageNo());
map.put(PAGESIZE, page.getPageSize());
return map;
}
public static class PageParam{
private int pageNo;
private int pageSize;
public PageParam() {
}
public PageParam(int pageNo, int pageSize) {
this.pageNo = pageNo;
this.pageSize = pageSize;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
public static class Page<T>{
private int pageNo;
private int pageSize;
private int totalCount;
private int totalPage;
private List<T> elements;
public Page() {
}
public Page(int pageNo, int pageSize, int totalCount, List<T> elements) {
this.pageNo = pageNo;
this.pageSize = pageSize;
this.totalCount = totalCount;
this.elements = elements;
this.totalPage = totalCount/pageSize == 0 ? totalCount/pageSize : totalCount/pageSize+1;;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getElements() {
return elements;
}
public void setElements(List<T> elements) {
this.elements = elements;
}
}
}