Mysql frame --- HMySql

Java Database Framework

When I was learning java database framework, first use the Hibernate, but until now, I could almost forget it, after all, almost two years did not touch the stuff, and then later been MyBatis. Because it is simple.

But the article does not cover them both, I introduce a framework of MySql, but I do not know what to call it. He HMySql temporarily called it.

Beginner's mind is an enhancement to java reflection, annotations understanding of the design, there are many bug, Wang pointed out that the source code will be given follow-up

Uses: fast freshman class, many students will not set up a jdbc operations, hoping to help it complete the class-based.

Download Link

https://www.houxinlin.com/mysql-frame.jar

Because it is the student, there is no money to increase bandwidth, download speed may be slow, ah ~ ~ ~ is very slow very slow kind of slow, but also look wait

00 integrated into the project

1: In the project root directory create lib folder, and paste into the jar package, right-click the jar package ---- Build Path --- Add to Build Path

image.png

image.png

2: Integrated jar, also need to create a h-mysql.xml file in the root directory of the project, to the configuration database, configuration is as follows

<?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>

(This is all the configuration information, and it looks very little)

01 Create a data table

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 written in Java bean

There are several Note:
1: based on the need to add annotations @TableName, to name mapping table
2: need to inherit BeanSupport class
3: primary key to increase @PrimaryKey
. 4: @FieldName field indicating the corresponding data table column names

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 test

Note that:
1: must be called HDbManager.getInstance () init (); initialized, if not created h-mysql.xml file it may report an exception...


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 Exception Handling

1: If there are no empty java bean constructor, you may tell me the wrong
solution: increase the empty constructor

image.png

2: no new h-mysql.xml file in the root directory of the project
solution: build h-mysql.xml

image.png

3: the node configuration file name error
Solution: Check the name of a node in accordance with the following code

image.png

<?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: less important node configuration
Solution: Depending on the error message, check the configuration file write less nodes

image.png

Guess you like

Origin www.cnblogs.com/HouXinLin/p/10994377.html