你好,老师让用Java编写一个图书管理系统,用MySQL,你好我想看一下代码文件,有空可以发一下吗
你这太无语了,不能复制格式化代码
public interface IDao {
public int insert(IBean bean) throws Exception;
public int delete(int id) throws Exception;
public int update(int id, IBean bean) throws Exception;
public List<IBean> query(int id) throws Exception;
}
public interface IBean {
/**
* 保存table字段名和bean字段映射
*
* @return
*/
public Map<String, Field> getFieldsMap();
}
package dao.table;
public interface ITable {
/**
* 主键
* @return
*/
public String getTableKey();
/**
* 表名
*
* @return
*/
public String getTableName();
/**
* 构建字段
*
* @param isUserFields
* 使用字段名构建/使用?通配符构建
* @param isSingle
* true: 形如: ?,?
* false 形如:fiele1=?,fiele2=?
* @return
*/
public String buildFilds(boolean isUserFields, boolean isSingle);
public String insertSql();
public String updateSql();
public String deleteSql();
public String querySql();
}
package service;
import java.util.List;
import bean.IBean;
public interface IService {
public void insert(IBean bean);
public void delete(int id);
public void update(int id, IBean bean);
public List<IBean> query(int id);
}
public class DaoImp implements IDao {
Connection conn;
ITable table;
public DaoImp(Connection conn, ITable table) {
super();
this.conn = conn;
this.table = table;
}
@Override
public int insert(IBean bean) throws Exception {
PreparedStatement statement = null;
try {
String sql = table.insertSql();
statement = conn.prepareStatement(sql);
for (int i = 0; i < table.getClass().getDeclaredFields().length; i++) {
statement.setObject(i, TableUtil.getFieldValue(table.getClass().getDeclaredFields()[i], bean));
}
return statement.executeUpdate();
} catch (Exception e) {
throw e;
} finally {
Dbutil.close(statement);
Dbutil.close(conn);
}
}
@Override
public int delete(int id) throws Exception {
PreparedStatement preparedStatement = null;
try {
preparedStatement = conn.prepareStatement(table.deleteSql());
preparedStatement.setObject(0, id);
return preparedStatement.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
Dbutil.close(preparedStatement);
Dbutil.close(conn);
}
}
@Override
public int update(int id, IBean bean) throws Exception {
PreparedStatement statement = null;
try {
String sql = table.updateSql();
statement = conn.prepareStatement(sql);
for (int i = 0; i < table.getClass().getDeclaredFields().length; i++) {
statement.setObject(i, TableUtil.getFieldValue(table.getClass().getDeclaredFields()[i], bean));
}
statement.setObject(table.getClass().getDeclaredFields().length, id);
return statement.executeUpdate();
} catch (Exception e) {
throw e;
} finally {
Dbutil.close(statement);
Dbutil.close(conn);
}
}
@Override
public List<IBean> query(int id) throws Exception {
PreparedStatement preparedStatement = null;
List<IBean> result = new ArrayList<>();
try {
preparedStatement = conn.prepareStatement(table.deleteSql());
preparedStatement.setObject(0, id);
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
Class<?> clazz = Class.forName(table.getClass().getAnnotation(Bean.class).value());
IBean bean = (IBean) clazz.newInstance();
Dbutil.fildBean(rs, bean);
result.add(bean);
}
return result;
} catch (Exception e) {
throw e;
} finally {
Dbutil.close(preparedStatement);
Dbutil.close(conn);
}
}
}
public abstract class TableImp implements ITable {
@Override
public String buildFilds(boolean isUserFields, boolean isSingle) {
Field[] fields = getClass().getDeclaredFields();
StringBuilder sb = new StringBuilder();
for (Field field : fields) {
// 暂时设置主键,其实应该把主键单独拿出来
// 这里使用getName,也可以get(this)获取值
sb.append(isSingle ? isUserFields ? field.getName() : "?" : field.getName() + "=?").append(",");
}
return sb.toString().substring(0, sb.toString().length() - 1);
}
@Override
public String insertSql() {
StringBuilder sb = new StringBuilder();
sb.append(" insert to " + getTableName());
sb.append(" values( ");
sb.append(buildFilds(false, true));
sb.append(")");
return sb.toString();
}
@Override
public String deleteSql() {
return "delete form " + getTableName() + " where " + getTableKey() + "=?";
}
@Override
public String updateSql() {
StringBuilder sb = new StringBuilder();
sb.append(" update " + getTableName());
sb.append(" set ");
sb.append(buildFilds(true, false));
sb.append(" where " + getTableKey() + "=?");
return sb.toString();
}
@Override
public String querySql() {
return "select * from " + getTableName() + " where " + getTableKey() + "=?";
}
}
public class BookService implements IService {
IDao dao;
public BookService(IDao dao) {
super();
this.dao = dao;
}
@Override
public void insert(IBean bean) {
try {
dao.insert(bean);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void delete(int id) {
try {
dao.delete(id);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void update(int id, IBean bean) {
try {
dao.update(id, bean);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public List<IBean> query(int id) {
try {
return dao.query(id);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
public class TableUtil {
public static Object getFieldValue(Field tableField, IBean bean) throws Exception {
Map<String, Field> map = bean.getFieldsMap();
if (map.keySet().contains(tableField.getName())) {
// 使用getName,
return map.get(tableField.getName()).get(bean);
} else {
throw new Exception("确认bean注解是否与表结构对应: beanClass=" + bean.getClass().getSimpleName() + ", tableField=" + tableField.getName());
}
}
}
public class Dbutil {
public static void close(Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void fildBean(ResultSet rs, IBean bean) throws IllegalArgumentException, IllegalAccessException, SQLException{
Map<String, Field> fieldsMap = bean.getFieldsMap();
for (String fieldName : fieldsMap.keySet()) {
fieldsMap.get(fieldName).set(bean, rs.getObject(fieldName));
}
}
}
public class Book extends BaseBean{
@TableField(value="BOOK_ID")
String bookId;
@TableField(value="BOOK_NAME")
String bookName;
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
}
@Bean(value="BOOK")
public class BookTable extends TableImp{
public String BOOK_ID = "BOOK_ID";
public String BOOK_NAME = "BOOK_NAME";
@Override
public String getTableName() {
return "T_BOOK";
}
@Override
public String getTableKey() {
return BOOK_ID;
}
}
/**
* 实现table和bean的映射
*
* @author 16245
*
*/
public @interface Bean {
String value();
}
/**
* 自定义注解
* 实现bean和table字段的映射
*
* @author 16245
*
*/
public @interface TableField {
String value();
}
swing+mysql简单的项目就行,不用太复杂
不是的,3人一组,请问你有这个项目的代码吗,有空发一下,第一个小项目,自己写不出来
哦 不急的话等我晚上回家找找以前练习的有没有保存哇~