Java 數據庫框架java
在我學習java數據庫框架的時候,第一個用的是Hibernate,可是到如今,我可能已經快忘記它了,畢竟快兩年沒有碰的東西,後來一直再用MyBatis.由於它簡單.mysql
可是本文不會介紹它們倆,介紹一個個人MySql框架,可是我殊不知道叫它什麼.暫時就叫他HMySql吧.sql
初心是加強對java 反射、註解的理解,所設計的,還有不少bug,望指出,源碼後續會給出數據庫
用途:大一快課設了.不少同窗還不會jdbc操做,但願能夠藉助它,完成課設app
下載連接框架
https://www.houxinlin.com/mysql-frame.jaride
因爲是學生,沒有錢去增長帶寬,下載速度可能很慢,嗯~~~是很慢很慢的那種慢,還望耐心等待學習
00 集成到項目中測試
1:在項目根目錄下新建lib文件夾,把jar包粘貼進去,右擊jar包----Build Path---Add to Build Pathui
2:集成jar後,還須要在項目根目錄下建立一個h-mysql.xml文件,用來配置數據庫,配置以下
<?xml version="1.0" encoding="utf-8"?> <h-mysql> <database-cnf> <driver>com.mysql.cj.jdbc.Driver</driver> <user-name>root</user-name> <user-pass>hxl495594..</user-pass> <ip>localhost</ip> <database-name>homework1</database-name> </database-cnf> <configure> <!-- 是否打開Sql執行日誌 --> <logger>true</logger> </configure> </h-mysql>
(這是全部的配置信息了,它看起來不多)
01 建立數據表
DROP TABLE IF EXISTS `tb_user`; CREATE TABLE `tb_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', `user_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用戶名', `age` tinyint(4) UNSIGNED NULL DEFAULT NULL COMMENT '年齡', `blance` decimal(9, 2) NULL DEFAULT NULL COMMENT '餘額', `is_vip` enum('Y','N') CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '是否VIP', `register_timer` datetime(0) NULL DEFAULT NULL COMMENT '註冊時間', `girl_friend` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '女友名字', `phone_number` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手機號', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 121 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
02 編寫Java bean
這裏有幾個須要注意的:
1:須要再類上加@TableName註解,用來映射表名
2:須要繼承BeanSupport類,
3:主鍵上要增長@PrimaryKey
4:@FieldName表示字段對應的數據表中的列名
import com.houxinlin.annotation.FieldName; import com.houxinlin.annotation.PrimaryKey; import com.houxinlin.annotation.TableName; import com.houxinlin.dbimpl.BeanSupport; @TableName(tabName="tb_user") public class UserMapp extends BeanSupport{ @PrimaryKey @FieldName(fieldName="id") private int id; @FieldName(fieldName="user_name") private String userName; @FieldName(fieldName="age") private int age; @FieldName(fieldName="blance") private BigDecimal blance; @FieldName(fieldName="is_vip") private String isVip; @FieldName(fieldName="register_timer") private String registerTimer; @FieldName(fieldName="girl_friend") private String grilFriendName; public UserMapp() { super(); } @FieldName(fieldName="phone_number") private String phoneNumber; public UserMapp(int id, String userName, int age, BigDecimal blance, String isVip, String registerTimer, String grilFriendName, String phoneNumber) { super(); this.id = id; this.userName = userName; this.age = age; this.blance = blance; this.isVip = isVip; this.registerTimer = registerTimer; this.grilFriendName = grilFriendName; this.phoneNumber = phoneNumber; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public BigDecimal getBlance() { return blance; } public void setBlance(BigDecimal blance) { this.blance = blance; } public String getIsVip() { return isVip; } public void setIsVip(String isVip) { this.isVip = isVip; } public String getRegisterTimer() { return registerTimer; } public void setRegisterTimer(String registerTimer) { this.registerTimer = registerTimer; } public String getGrilFriendName() { return grilFriendName; } public void setGrilFriendName(String grilFriendName) { this.grilFriendName = grilFriendName; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } @Override public String toString() { return toJson(true); } }
03 測試
須要注意的是:
1:必須調用HDbManager.getInstance().init();進行初始化,若是沒有建立h-mysql.xml文件.它可能會報異常.
import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import com.houxinlin.db.HDbManager; import com.houxinlin.dbimpl.HxlDb; import com.houxinlin.frame.FilterCondition; public class Main { private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:dd"); public static void main(String[] args) { /** * 初始化數據庫 你必須這麼作!!!! */ HDbManager.getInstance().init(); UserMapp user = new UserMapp(0, "侯鑫林", 20, new BigDecimal("666"), "Y", sdf.format(new Date()), "喬羽祥", "1504889423"); // 保存 必須繼承BeanSupport,同比要有一個空構造方法 user.save(); /** * 查找全部用戶 */ List<UserMapp> lists = HxlDb.findAll(UserMapp.class); System.out.println("所有用戶---->" + lists); //根據條件查找 UserMapp us=null; us=HxlDb.findOneByCondition(UserMapp.class, new FilterCondition.Builder().addEquseToCodition("user_name", "侯鑫林").build()); System.out.println("查找指定姓名----->"+us); //根據Id查找 us=HxlDb.findByPrimaryId(UserMapp.class, 119); System.out.println("根據ID查找----->"+us); //修改 us.setBlance(new BigDecimal("8888")); HxlDb.upDataByPrimaryId(us, 118); System.out.println("修改後的值----->"+HxlDb.findByPrimaryId(UserMapp.class, 119)); /** * 根據條件刪除年齡大於10歲的人 */ HxlDb.deleteByCondition(UserMapp.class, new FilterCondition.Builder().addGreaterThan("age", 10).build()); System.out.println(HxlDb.findAll(UserMapp.class)); } }
04 異常處理
1:若是java bean中沒有空構造方法,可能會報一下錯誤
解決辦法:增長空構造方法
2:沒有在項目根目錄下新建h-mysql.xml文件
解決辦法:創建h-mysql.xml
3:配置文件節點名錯誤
解決辦法:根據如下代碼進行節點名檢查
<?xml version="1.0" encoding="utf-8"?> <h-mysql> <database-cnf> <driver>com.mysql.cj.jdbc.Driver</driver> <user-name>root</user-name> <user-pass>hxl495594..</user-pass> <ip>localhost</ip> <database-name>homework1</database-name> </database-cnf> <configure> <!-- 是否打開Sql執行日誌 --> <logger>true</logger> </configure> </h-mysql>
4:少重要節點配置
解決辦法:根據錯誤的信息,檢查配置文件中少寫的節點