基于注解和反射的Java ORM框架(3)-极速CRUD操作

来源:互联网 发布:ioc java 编辑:程序博客网 时间:2024/05/21 11:13

最近比较忙,琐事较多,在激烈的看球过程中抽出了些时间,基本把Panda ORM写完了,只在mysql上测试了下,问题不大,本篇先讲下Panda ORM的用法,后面会陆续讲解如何实现的。

Panda ORM用法很简单:在有数据库的基础上,Panda ORM只需要定义跟数据库表同样结构的实体类,并为表的外键、主键添加注解,即可实现实体对应的增、删、改、查操作。下面进行具体的演示:

一,新建测试数据库pandaormtest

新建两张表user和role如下,注意取消了外键约束,使用代码控制外键逻辑(User表的userRole指向Role表的roleId)。

/*Navicat MySQL Data TransferSource Server         : 本机数据库Source Server Version : 50552Source Host           : 127.0.0.1:3306Source Database       : pandaormtestTarget Server Type    : MYSQLTarget Server Version : 50552File Encoding         : 65001Date: 2017-03-16 17:03:08*/SET FOREIGN_KEY_CHECKS=0;-- ------------------------------ Table structure for `role`-- ----------------------------DROP TABLE IF EXISTS `role`;CREATE TABLE `role` (  `roleId` int(10) NOT NULL AUTO_INCREMENT,  `roleName` varchar(200) DEFAULT NULL,  PRIMARY KEY (`roleId`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;-- ------------------------------ Records of role-- ----------------------------INSERT INTO `role` VALUES ('1', '校长');INSERT INTO `role` VALUES ('2', '教师');INSERT INTO `role` VALUES ('3', '学生');-- ------------------------------ Table structure for `user`-- ----------------------------DROP TABLE IF EXISTS `user`;CREATE TABLE `user` (  `userId` int(10) NOT NULL AUTO_INCREMENT,  `userRole` int(10) DEFAULT NULL,  `userName` varchar(20) DEFAULT NULL,  `userPassword` varchar(20) DEFAULT NULL,  PRIMARY KEY (`userId`),  KEY `fk_user_role` (`userRole`)) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;-- ------------------------------ Records of user-- ----------------------------INSERT INTO `user` VALUES ('1', '1', '张一', '1234');INSERT INTO `user` VALUES ('2', '2', '李四', '1234');INSERT INTO `user` VALUES ('3', '2', '赵四', '1234');INSERT INTO `user` VALUES ('4', '3', '王五', '1234');INSERT INTO `user` VALUES ('5', '3', '陈五', '1234');INSERT INTO `user` VALUES ('6', '3', '龙五', '1234');INSERT INTO `user` VALUES ('7', '3', '1', '1234');INSERT INTO `user` VALUES ('8', '3', '1', '1234');INSERT INTO `user` VALUES ('9', '3', '1', '1234');INSERT INTO `user` VALUES ('10', '3', '1', '1234');INSERT INTO `user` VALUES ('11', '3', '1', '1234');INSERT INTO `user` VALUES ('12', '3', '1', '1234');

二,新建测试Web Project,并导入Panda ORM框架jar包

新建PandOrmDemo,并在Web-INF/lib下放入panda-orm.jar。该jar包是Panda ORM框架生成的jar包,所有类库均在panda.orm下。

别忘了既然要连接mysql数据库,要将mysql-connector-java-5.1.39-bin.jar也放到lib下哦。

三,建立数据库配置文件config.properties

在src下建立config.properties,Panda ORM会默认读取该配置文件中的参数,代码如下:

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/pandaormtest?useUnicode=true&characterEncoding=utf-8user=rootpassword=Pass1234

注意别写成

这下面是错的。driver="com.mysql.jdbc.Driver"url= "jdbc:mysql://127.0.0.1:3306/pandaormtest?useUnicode=true&characterEncoding=utf-8";user="root";password="Pass1234";

四,建立实体类

在包entity下建立User和Role实体类,注意实体类名需要和表名一一对应,而实体类的属性需要和表的列名一一对应,代码如下:

package entity;public class User {    private String userId;    private Role userRole;    private String userName;    private String userPassword;    //省略get set}package entity;public class Role {    private String roleId;    private String roleName;    //省略get set}

五,为主键、外键添加注解

此处需要注意两点:1,外键列的注解需要将主表的主键名作为参数;2,主键列如果不是自增长需要配置AutoIncrement.FALSE参数(自增长则默认为AutoIncrement.TRUE,不需要配置。因为注解的参数不支持布尔类型,所以自定义了enum 类型的AutoIncrement)。

package entity;import panda.orm.annotation.ForeignKey;import panda.orm.annotation.Key;public class User {    @Key    private String userId;    @ForeignKey("roleId")    private Role userRole;    private String userName;    private String userPassword;    //省略get set}package entity;import panda.orm.annotation.Key;public class Role {    @Key    private String roleId;    private String roleName;    //省略get set}

六,直接使用框架中的EntityOperation进行数据库操作测试

package test;import java.util.List;import entity.Role;import entity.User;import panda.orm.operation.EntityOperation;public class Main {    public static void main(String[] args) throws Exception{        EntityOperation oper=new EntityOperation(User.class);        //输出        List<User> users=oper.selectAll();        for(User user:users){            System.out.println(user.getUserName()+"|"+user.getUserRole().getRoleName());        }        //新增        User tempUser=new User();        tempUser.setUserName("熊猫");        Role role=new Role();        role.setRoleId("1");        tempUser.setUserRole(role);        oper.add(tempUser);        //输出        users=oper.selectAll();        for(User user:users){            System.out.println(user.getUserName()+"|"+user.getUserRole().getRoleName());        }    }}

输入如下,可见已经实现了在具有外键的情况下的更新和查询

张一|校长李四|教师赵四|教师王五|学生陈五|学生龙五|学生1|学生1|学生1|学生1|学生1|学生1|学生张一|校长熊猫|校长李四|教师赵四|教师王五|学生陈五|学生龙五|学生1|学生1|学生1|学生1|学生1|学生1|学生
0 0