8.BaseDao中的通用的添加
//insert into 表名 (...) values(...)
public int insert(T t) throws Exception {
//创建一个sql的字符串
StringBuffer sql=new StringBuffer();
sql.append("insert into ");
//获取实体类的反射类
Class<?> aClass = t.getClass();
//获取表名
String tableName = aClass.getSimpleName();
//获取实体类的注解以便获取数据库表的表名
TableName annotation = aClass.getAnnotation(TableName.class);
if (annotation!=null){
tableName=annotation.value();
}
sql.append(tableName);
/* System.out.println(sql);*/
//insert into 表名(...) values
List<String> columnNames=new ArrayList<>();
List<Object> values=new ArrayList<>();
//获取所有的属性,getDeclaredFields():获取该类的全部属性
Field[] fields = aClass.getDeclaredFields();
for (Field field : fields) {
//获取每一个属性的注解
TableField tableField = field.getAnnotation(TableField.class);
//用来判断是否是id
TableId tableId = field.getAnnotation(TableId.class);
if (tableId!=null){
//如果是id则跳过,因为id是递增的可以为null
continue;
}
//获取属性名
String name = field.getName();
//为了获取是否与数据库的列名一致
if (tableField!=null){
name=tableField.value();
}
//允许访问私有成员
field.setAccessible(true);
Object o = field.get(t);
values.add("'"+o+"'");
columnNames.add(name);
}
String columnName = columnNames.toString().replace("[", "(").replace("]", ")");
sql.append(columnName+" values");
String value = values.toString().replace("[", "(").replace("]", ")");
sql.append(value);
/* System.out.println(sql);*/
//调用DbUtil,用来连接数据库
Connection conn = DbUtil.getConnection();
//执行sql语句
PreparedStatement ps = conn.prepareStatement(sql.toString());
//返回被影响的行数
int i = ps.executeUpdate();
//关闭资源
DbUtil.closeAll(conn,ps,null);
return i;
}
9.BaseDao中的通用的修改
// update 表名 set 列名=..,...where id =..
public int update(T t) throws Exception {
StringBuffer sql=new StringBuffer();
sql.append("update ");
Class<?> aClass = t.getClass();
String name = aClass.getSimpleName();
TableName tableName = aClass.getAnnotation(TableName.class);
if (tableName!=null){
name=tableName.value();
}
sql.append(name+" set ");
Field[] fields = aClass.getDeclaredFields();
String where=" where ";
for (Field field : fields) {
field.setAccessible(true);
TableField tableField = field.getAnnotation(TableField.class);
TableId tableId = field.getAnnotation(TableId.class);
if (tableId!=null){
String value = tableId.value();
Object o = field.get(t);
where=where+value+"='"+o.toString()+"'";
continue;
}
String name1 = field.getName();
if (tableField!=null){
name1=tableField.value();
}
Object o = field.get(t);
sql.append(name1+"='"+o.toString()+"',");
}
sql.deleteCharAt(sql.length()-1);
sql.append(where);
System.out.println(sql);
Connection conn = DbUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql.toString());
int i = ps.executeUpdate();
DbUtil.closeAll(conn,ps,null);
return i;
}
10.BaseDao中的通用的删除
10.1 先获取泛型类型(利用构造函数)
private Class clazz;
//当子类构造函数调用时,父类的构造函数也被调用。
public BaseDao(){
//this表示子类的Dao对象
Class<? extends BaseDao> aClass = this.getClass();
//获取当前子类的父类的反射类
ParameterizedType genericSuperclass = (ParameterizedType) aClass.getGenericSuperclass();
//获取该反射类中的泛型类型
Type actualTypeArgument = genericSuperclass.getActualTypeArguments()[0];
clazz= (Class) actualTypeArgument;
}
10.2 利用得到的反射类完成删除代码
//delete from 表名 where id=...
public int deleteById(Object id) throws Exception {
StringBuffer sql=new StringBuffer("delete from ");
String tableName = clazz.getSimpleName();
TableName annotation = (TableName) clazz.getAnnotation(TableName.class);
if (annotation!=null){
tableName=annotation.value();
}
sql.append(tableName);
String where =" where ";
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
TableId annotation1 = field.getAnnotation(TableId.class);
if (annotation1!=null){
String tableIdName=annotation1.value();
where=where+tableIdName+"='"+id.toString()+"'";
break;
}
}
sql.append(where);
Connection conn = DbUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql.toString());
int i = ps.executeUpdate();
System.out.println(sql);
DbUtil.closeAll(conn,ps,null);
return i;
}
11.BaseDao中的通用的通过id查询
//查询 select * from 表名 where id=?
public T selectById(Object id) throws Exception {
StringBuffer sql=new StringBuffer("select * from ");
String simpleName = clazz.getSimpleName();
TableName annotation = clazz.getAnnotation(TableName.class);
if (annotation!=null){
simpleName=annotation.value();
}
sql.append(simpleName+" ");
String where="where ";
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
TableId idAnnotation = field.getAnnotation(TableId.class);
if (idAnnotation!=null){
where=where+idAnnotation.value()+"="+id;
}
}
sql.append(where);
System.out.println(sql);
Connection conn = DbUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql.toString());
ResultSet rs = ps.executeQuery();
while (rs.next()){
T t = clazz.newInstance();
for (Field field : declaredFields) {
field.setAccessible(true);
TableId tableId = field.getAnnotation(TableId.class);
TableField tableField = field.getAnnotation(TableField.class);
String name = field.getName();
if (tableId!=null){
name=tableId.value();
}
if (tableField!=null){
name=tableField.value();
}
Object v = rs.getObject(name);
field.set(t,v);
}
return t;
}
return null;
}
12.BaseDao中的通用的查询全部
//查询全部 select * from 表名
public List<T> selectAll() throws Exception {
List<T> list=new ArrayList<>();
StringBuffer sql=new StringBuffer("select * from ");
String name = clazz.getSimpleName();
TableName annotation = clazz.getAnnotation(TableName.class);
if (annotation!=null){
name=annotation.value();
}
sql.append(name);
System.out.println(sql);
Connection conn = DbUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(sql.toString());
ResultSet rs = ps.executeQuery();
Field[] declaredFields = clazz.getDeclaredFields();
while (rs.next()){
T t = clazz.newInstance();
for (Field field : declaredFields) {
field.setAccessible(true);
TableId tableId = field.getAnnotation(TableId.class);
TableField tableField = field.getAnnotation(TableField.class);
String name1 = field.getName();
if (tableId!=null){
name1=tableId.value();
}
if (tableField!=null){
name1=tableField.value();
}
Object v = rs.getObject(name1);
field.set(t,v);
}
list.add(t);
}
return list;
}