java实现从实体到SQL语句的转换(二)

时间:2024-07-16 16:33:38

前面写过一篇生成insert语句的博客,今天把CRUD的都完善了一下,需要的上次写的获取实体信息的类,这里就不贴出来了【点击打开查看

下面是完整的代码:

package tan.code.utils;

import java.util.List;
import java.util.Map; /**
* 简单的SQL语句拼装类 ;要求数据库表名及列命和实体保持一致;
*
* @author Mr_Tank_
*
* @param <T>
*/
public class SQLStringHelper<T> { private EntityHelper entityHelper = new EntityHelper(); /**
* 根据传入的实体生成Insert SQL語句
*
* @param entity
* @return 拼接好的sql语句
*/
@SuppressWarnings("unchecked")
public String createInsert(T entity) {
String sql = "Insert into ";
String column = ""; // 列
String c_values = ""; // 列值
List<Map<String, Object>> list = entityHelper.getFiledsInfo(entity);
sql += list.get(0).get("obj_name").toString() + " ";
for (int i = 0; i < list.size(); i++) { // 約定id在數據庫自動生成-20130807 if (String.valueOf(list.get(i).get("f_name")).equals("id")) {
i++;
} else if (list.get(i).get("f_value") != null) {
column += list.get(i).get("f_name") + ",";
c_values += "'" + list.get(i).get("f_value") + "',";
} }
sql += "(" + column.substring(0, column.length() - 1) + ") values ("
+ c_values.substring(0, c_values.length() - 1) + ");"; return sql; } /**
* 拼裝Delete SQL語句
*
* @param entity
* @param id
* 实体id
* @return
*/
@SuppressWarnings("unchecked")
public String createDelete(T entity, int id) {
String sql = "delete from ";
List<Map<String, Object>> list = entityHelper.getFiledsInfo(entity);
sql += list.get(0).get("obj_name").toString() + " where id=" + id + ";";
return sql;
} /**
* 查找
*
* @param entity
* @return
*/
@SuppressWarnings("unchecked")
public String createSelect(T entity) {
String sql = "select * from ";
String column = ""; // 列
List<Map<String, Object>> list = entityHelper.getFiledsInfo(entity);
sql += list.get(0).get("obj_name").toString() + " where ";
for (int i = 0; i < list.size(); i++) { // 約定id在數據庫自動生成-20130807 // id計數不能從0開始
// 如果知道id直接根据id生成sql语句就可以了
if (String.valueOf(list.get(i).get("f_name")).equals("id")
&& !String.valueOf(list.get(i).get("f_value")).equals("0")) {
sql += "id=" + list.get(i).get("f_value");
return sql;
} else if (list.get(i).get("f_value") != null) { column += list.get(i).get("f_name") + " like %"
+ list.get(i).get("f_value") + "% or ";
}
}
return sql += column.substring(0, column.length() - 4) + ";"; } /**
* 根据id更新实体数据
*
* @param entity
* @param id
* 实体id
* @return 拼装好的 Update SQL语句
*/
@SuppressWarnings("unchecked")
public String createUpdate(T entity, int id) {
String sql = "update ";
String column = ""; // 列
List<Map<String, Object>> list = entityHelper.getFiledsInfo(entity);
sql += list.get(0).get("obj_name").toString() + " set ";
for (int i = 0; i < list.size(); i++) { // id不能修改
if (String.valueOf(list.get(i).get("f_name")).equals("id")) {
i++;
} else if (list.get(i).get("f_value") != null) {
column += list.get(i).get("f_name") + "='"
+ list.get(i).get("f_value")+"',";
} }
return sql +=column.substring(0, column.length()-1) + " where id='" + id+"'"; }
}

测试【其中可以使用任意实体类替代测试代码里面的的Tutor类】:

	@Test
public void test() {
// fail("Not yet implemented");
Tutor tutor = new Tutor();
tutor.setId(3);
//tutor.setTitle("寻找一個初三物理長期家教");
tutor.setProvince("广西壮族自治区");
tutor.setCity("桂林");
tutor.setRegion("朝阳区");
//tutor.setPatriarch("王先生");
tutor.setPhone("139772109981");
tutor.setGrade("初三");
tutor.setSubject("物理");
tutor.setPay(50); SQLStringHelper<Tutor> sqlStringHelper = new SQLStringHelper<Tutor>();
System.out.println(sqlStringHelper.createInsert(tutor));
System.out.println(sqlStringHelper.createDelete(tutor, 1));
System.out.println(sqlStringHelper.createSelect(tutor));
System.out.println(sqlStringHelper.createUpdate(tutor, 2));
}

测试结果:

Insert into Tutor (subject,grade,pay,phone,browse,province,city,region,wb_num) values ('物理','初三','50.0','139772109981','0','广西壮族自治区','桂林','朝阳区','0');
delete from Tutor where id=1;
select * from Tutor where id=3
update Tutor set subject='物理',grade='初三',pay='50.0',phone='139772109981',browse='0',province='广西壮族自治区',city='桂林',region='朝阳区',wb_num='0' where id='2'

以上只是本人的一些尝试,如有不足之处,请指出。