package yangpu.jdbc.mysql;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import yangpu.jdbc.mysql.model.ColumnInfo;
import yangpu.util.CharUtil;
import yangpu.util.ReaderWriter;
/**
* <p>
* yangpu.jdbc.mysql.ModelCreate.java
* </p>
* <p>
* description:
* </p>
*
* @author YangPu
* @createTime 2016年7月21日 下午3:50:33
*/
public class MapperCreate {
private BufferedWriter writer;
private List<ColumnInfo> list;
/**
* 加载模板
*
* @param modelName
*/
public MapperCreate(String tableName, List<ColumnInfo> list) {
try {
this.list = list;
String modelName = CharUtil.UnderlineToUppercaseTitle(tableName);
String fileName = null;
if (Constant.isTable) {
fileName = Constant.path + modelName + File.separator + modelName + "Mapper.xml";
} else {
fileName = Constant.path + Constant.package_mapper + File.separator + modelName + "Mapper.xml";
}
File file = new File(fileName);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
if (!file.exists()) {
file.createNewFile();
}
writer = new BufferedWriter(new FileWriter(file));
BufferedReader reader = new BufferedReader(new FileReader(Constant.mapper));
reader = ReaderWriter.readWrite(reader, list, tableName);
String line = null;
while ((line = reader.readLine()) != null) {
if (line.contains("-modelName-")) {
line = line.replaceAll("-modelName-", modelName);
writer.write(line + "\n");
} else if (line.contains("-columns-")) {
createColumns();
} else if (line.contains("-where-")) {
createWhere();
} else if (line.contains("-insert-")) {
createInsert();
} else if (line.contains("-update-")) {
createUpdate();
} else if (line.contains("-deleteById-")) {
createDeleteById();
} else if (line.contains("-deleteByEntity-")) {
createDeleteByEntity();
} else if (line.contains("-getById-")) {
createGetById();
} else if (line.contains("-getList-")) {
createGetList();
} else if (line.contains("-getList_count-")) {
createGetListCount();
} else {
writer.write(line + "\n");
}
}
writer.close();
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void createColumns() {
try {
int MaxLength = 0;
String temp = null;
List<String[]> fields = new ArrayList<>();
for (int index = 0; index < list.size(); index++) {
ColumnInfo columninfo = list.get(index);
String cloumn = columninfo.getColumnName();
String field = CharUtil.UnderlineToUppercase(cloumn);
String[] strings = new String[3];
temp = new String("\t\t" + cloumn);
if (temp.length() > MaxLength) {
MaxLength = temp.length();
}
strings[0] = temp;
temp = new String("AS");
strings[1] = temp;
if (index == (list.size() - 1)) {
temp = new String(field);
} else {
temp = new String(field + " ,");
}
strings[2] = temp;
fields.add(strings);
}
for (String[] strings : fields) {
writer.write(strings[0] + CharUtil.getWhitespace(MaxLength - strings[0].length()) + "\t " + strings[1]
+ "\t " + strings[2] + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void createWhere() {
String line = new String();
try {
for (int index = 0; index < list.size(); index++) {
ColumnInfo columninfo = list.get(index);
String cloumn = columninfo.getColumnName();
String field = CharUtil.UnderlineToUppercase(cloumn);
line = new String("\t\t\t<if test=\"" + field + "!=null and " + field + "!=''\">");
writer.write(line.toString() + "\n");
line = new String("\t\t\t\tAND " + cloumn + " = #{" + field + "}");
writer.write(line.toString() + "\n");
line = new String("\t\t\t</if>");
writer.write(line.toString() + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void createInsert() {
String line = null;
try {
line = new String("\t\tINSERT INTO " + list.get(0).getTableName().toUpperCase() + " (");
writer.write(line + "\n");
for (int index = 0; index < list.size(); index++) {
ColumnInfo columninfo = list.get(index);
String cloumn = columninfo.getColumnName();
String field = CharUtil.UnderlineToUppercase(cloumn);
if (index == (list.size() - 1)) {
line = new String("\t\t\t" + cloumn);
} else {
line = new String("\t\t\t" + cloumn + " ,");
}
writer.write(line + "\n");
}
writer.write("\t\t) VALUES (\n");
for (int index = 0; index < list.size(); index++) {
ColumnInfo columninfo = list.get(index);
String cloumn = columninfo.getColumnName();
String field = CharUtil.UnderlineToUppercase(cloumn);
if (index == (list.size() - 1)) {
line = new String("\t\t\t#{" + field + "}");
} else {
line = new String("\t\t\t#{" + field + "} ,");
}
writer.write(line + "\n");
}
writer.write("\t\t)\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void createUpdate() {
String line = null;
try {
line = new String("\t\tUPDATE " + list.get(0).getTableName().toUpperCase() + " SET");
writer.write(line + "\n");
List<ColumnInfo> pris = new ArrayList<>();
int MaxLength = 0;
String temp = null;
List<String[]> fields = new ArrayList<>();
for (int index = 0; index < list.size(); index++) {
ColumnInfo columninfo = list.get(index);
String cloumn = columninfo.getColumnName();
String field = CharUtil.UnderlineToUppercase(cloumn);
if (StringUtils.isNotBlank(columninfo.getColumnKey())
&& "PRI".equals(columninfo.getColumnKey().toUpperCase())) {
pris.add(columninfo);
continue;
}
String[] strings = new String[3];
temp = new String("\t\t\t" + cloumn);
if (MaxLength < temp.length()) {
MaxLength = temp.length();
}
strings[0] = temp;
temp = new String("=");
strings[1] = temp;
if (index == (list.size() - 1)) {
temp = new String("#{" + field + "}");
} else {
temp = new String("#{" + field + "} ,");
}
strings[2] = temp;
fields.add(strings);
}
for (String[] strings : fields) {
writer.write(strings[0] + CharUtil.getWhitespace(MaxLength - strings[0].length()) + "\t" + strings[1]
+ "\t" + strings[2] + "\n");
}
writer.write("\t\tWHERE\n");
if (pris.size() != 0) {
for (ColumnInfo columninfo : pris) {
String cloumn = columninfo.getColumnName();
String field = CharUtil.UnderlineToUppercase(cloumn);
writer.write("\t\t\t" + cloumn + " = #{" + field + "}\n");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void createDeleteById() {
String line = null;
try {
line = new String("\t\tDELETE FROM " + list.get(0).getTableName().toUpperCase());
writer.write(line + "\n");
line = new String("\t\t\tWHERE");
writer.write(line + "\n");
for (int index = 0; index < list.size(); index++) {
ColumnInfo columninfo = list.get(index);
if (StringUtils.isNotBlank(columninfo.getColumnKey())
&& "PRI".equals(columninfo.getColumnKey().toUpperCase())) {
String cloumn = columninfo.getColumnName();
String field = CharUtil.UnderlineToUppercase(cloumn)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
auto.zip (50个子文件)
auto
WebContent
META-INF
MANIFEST.MF 39B
WEB-INF
lib
mysql-connector-java-5.1.25.jar 829KB
commons-lang3-3.4.jar 424KB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.common.component 464B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 567B
org.eclipse.jdt.core.prefs 364B
src
yangpu
jdbc
oracle
mysql
MapperCreate.java 10KB
DaoCreate.java 2KB
ConnectionSingle.java 5KB
model
ColumnInfo.java 5KB
Table.java 665B
ModelCreate.java 5KB
ControllerCreate.java 2KB
ServiceCreate.java 2KB
Constant.java 2KB
resources
templateCtr.yp 3KB
templateMapper.yp 963B
templateSer.yp 2KB
templateModel.yp 329B
templateCtrRestful.yp 3KB
templateDao.yp 935B
main
AutoMain.java 635B
util
CharUtil.java 3KB
Log.java 598B
ReaderWriter.java 3KB
.project 1KB
.classpath 838B
build
classes
yangpu
jdbc
oracle
mysql
DaoCreate.class 2KB
ConnectionSingle.class 6KB
model
Table.class 1KB
ColumnInfo.class 5KB
ControllerCreate.class 3KB
ModelCreate.class 5KB
MapperCreate.class 9KB
Constant.class 3KB
ServiceCreate.class 2KB
resources
templateCtr.yp 3KB
templateMapper.yp 963B
templateSer.yp 2KB
templateModel.yp 329B
templateCtrRestful.yp 3KB
templateDao.yp 935B
temp.yp 3KB
main
AutoMain.class 968B
util
CharUtil.class 2KB
Log.class 1KB
ReaderWriter.class 4KB
共 50 条
- 1
资源评论
want_water_fish
- 粉丝: 24
- 资源: 27
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功