学生管理系统
util包
SqlType.java
public enum SqlType {
Insert,Delete,Update,Query
}
SqlUtil.java
import java.beans.PropertyDescriptor;
import java.io.ObjectInputStream.GetField;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
public class SqlUtil {
private static String tableName = "";
private static String sql = "";
private static StringBuffer sfield;
private static StringBuffer sValue;
// 填充预处理命占位符
public static <T> PreparedStatement initPstm(T t, PreparedStatement pstm, SqlType sqlType) {
Field fields[] = t.getClass().getDeclaredFields();
try {
for (int i = 0; i < fields.length; i++) {
// 属性描述
PropertyDescriptor pd = new PropertyDescriptor(fields[i].getName(), t.getClass());
// 属性可读方法
Method method = pd.getReadMethod();
// 添加 修改
if (sqlType == SqlType.Insert) {
// 排除主键
if (i != 0) {
pstm.setObject(i, method.invoke(t));
}
} else if (sqlType == SqlType.Update) {
// 排除主键
if (i == 0) {
// upate ewt nds where id=?
pstm.setObject(fields.length, method.invoke(t));
} else {
pstm.setObject(i, method.invoke(t));
}
} else if (sqlType == SqlType.Delete) // 删除
{
// 字段是否是主键
if (fields[i].getName().equals("id")) {
pstm.setObject(1, method.invoke(t));
}
} else if (sqlType == SqlType.Query) {
Object value = method.invoke(t);
if (value != null) {
if (fields[i].getName().equals("id") && (long) value == 0) {
continue;
}else if(fields[i].getName().equals("age") && (long) value == 0) {
continue;
}else {
if (fields[i].getName().equals("id"))
{
pstm.setObject(1, (long) value);
} else {
pstm.setObject(i, value);
}
}
}
}
}
} catch (
Exception e) {
e.printStackTrace();
}
return pstm;
}
// 返回sql语句1 2 3 4
public static <T> String getSql(T t, int sqlType) {
tableName = t.getClass().getSimpleName().toLowerCase();
sfield= new StringBuffer();
sValue = new StringBuffer();
if (sqlType == 1) {
sql = "insert into " + tableName + " ( ";
Field fields[] = t.getClass().getDeclaredFields();
for (int i = 1; i < fields.length; i++) {
if (i != fields.length - 1) {
sfield.append(fields[i].getName()).append(",");
sValue.append(" ?, ");
} else {
sfield.append(fields[i].getName());
sValue.append(" ? ");
}
}
sql = sql + sfield.toString() + " ) values (" + sValue.toString() + " )";
}
if (sqlType == 2) {
sql = "delete from " + tableName + " where id = ? ";
}
if (sqlType == 3) {
sql = "update " + tableName + " set ";
Field fields[] = t.getClass().getDeclaredFields();
for (int i = 1; i < fields.length; i++) {
if (i != fields.length - 1) {
sfield.append(fields[i].getName()).append("=").append("?,");
} else {
sfield.append(fields[i].getName()).append("=").append("?");
}
}
sql = sql + sfield + " where id = ? ";
}
// new User("DDD","123456","13904719999")
if (sqlType == 4) {
try {
int flag = 0;
sql = "select * from " + tableName + " where ";
Field fields[] = t.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
// 属性描述
PropertyDescriptor pd = new PropertyDescriptor(fields[i].getName(), t.getClass());
// 属性可读方法
Method method = pd.getReadMethod();
Object v = method.invoke(t);
if (v != null) {
if (fields[i].getName().equals("id") && (long) v == 0) {
continue;
}
if (fields[i].getName().equals("age") && (long) v == 0) {
continue;
}
if (flag != 0) {
sfield.append("and ");
sfield.append(fields[i].getName()).append(" = ? ");
} else {
sfield.append(fields[i].getName()).append(" = ? ");
flag = 1;
}
}
}
sql = sql + sfield;
} catch (Exception e) {
e.printStackTrace();
}
}
if (sqlType == 5) {
sql = "select * from " + tableName+" limit ?,?";
}
return sql;
}
//select * from where id=?
public static <T> List<T> handlerRs(Class<T> clazz ,ResultSet rs)
{
ArrayList<T> list=new ArrayList<>();
try {
ResultSetMetaData rsmd = rs.getMetaData();
Field fields[]=clazz.getDeclaredFields();
while (rs.next())
{
T t=clazz.newInstance();
// 获取查询数据库的列数
for (int i = 0; i < rsmd.getColumnCount(); ++i)
{
// 字段列名->对象 属性赋值
String colName = rsmd.getColumnName(i + 1);
Object colVau = rs.getObject(colName);
fieldVau(t, fields, colName, colVau);
}
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static <T> String getSql(Class<T> clazz, Object filed, String oper) {
tableName=clazz.getSimpleName().toLowerCase();
sql="select * from " + tableName;
//select * from user where id > ?
if(filed!=null&&!filed.equals("") ) {
sql=sql+" where " +filed+ " "+oper+" ? ";
}
return sql;
}
//处理结果集;映射对象赋值
public static <T> T handlerRs(T t,ResultSet rs)
{
try {
ResultSetMetaData rsmd = rs.getMetaData();
Field fields[]=t.getClass().getDeclaredFields();
while (rs.next())
{
// 获取查询数据库的列数
for (int i = 0; i < rsmd.getColumnCount(); ++i)
{
// 字段列名->对象 属性赋值
String colName = rsmd.getColumnName(i + 1);
Object colVau = rs.getObject(colName);
fieldVau(t, fields, colName, colVau);
}
}
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static <T> void fieldVau(T t, Field fields[], String colName, Object colVau) {
for (Field field : fields) {
try {
// 找到对应属性:int ->Integer互相转换;拆箱
if (field.getName().equals(colName) )
{
// 属性描述
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), t.getClass());
// 属性可读方法
Method method = pd.getWriteMethod();
// 属性赋值
if(colVau.getClass().equals("java.lang.Long")) {
method.invoke(t, (Long)colVau);
}else {
method.invoke(t, colVau);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
UserInfo.java
import com.imau.entry.User;
/**
*
* @author yangzj
* session :功能 保存基本信息
*/
public class UserInfo {
private static User user;
public static void setUser(User u) {
user = u;
}
public static User getUser() {
return user;
}
}
service包
StudentService.java
import java.util.List;
import com.imau.dao.student.StudentDaoImpl;
import com.imau.entry.Student;
public class StudentService {
private StudentDaoImpl dao;
public StudentService() {
dao = new StudentDaoImpl();
}
// 查询所有的学生信息
public List<Student> getAll() {
return dao.query(null, null, null);
}
// 天机学生
public boolean addStu(Student student) {
int row = dao.add(student);
return row > 0 ? true : false;
}
public boolean updateStu(Student student) {
int row = dao.update(student);
return row > 0 ? true : false;
}
public Student byId(Student student) {
return dao.find(student);
}
public boolean delById(Student student) {
int row = dao.delete(student);
return row > 0 ? true : false;
}
public List<Student> searchStu(String field,Object value) {
return dao.query(field, "like", value);
}
}
UserService.java
import com.imau.dao.user.UserDao;
import com.imau.dao.user.UserDaoImpl;
import com.imau.entry.User;
public class UserService {
private UserDaoImpl dao;
public UserService() {
dao=new UserDaoImpl();
}
//按照用户名密码查询:admin 123456
public User byNamePass(String uname,String upass) {
User user =new User();
user.setUname(uname);
user.setUpass(upass);
return dao.find(user) ;
}
public boolean editPass(User user) {
int row=dao.update(user);
return row>0?true:false;
}
}