當前位置:編程學習大全網 - 源碼下載 - 妳好,老師讓用Java編寫壹個圖書管理系統,用MySQL,妳好我想看壹下代碼文件,有空可以發壹下嗎

妳好,老師讓用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();

}

  • 上一篇:什麽是暴漲經典K線金蛤蟆的技術點?
  • 下一篇:用Python編寫壹個程序,判斷用戶輸入的八位信用卡號碼是否合法?
  • copyright 2024編程學習大全網