一个轻巧的hibernate连接模块,用于开发。
-
Hibernate-plus
名字有点张扬了
hibrnate强化集成,用于以最小的程度连接你的数据库,为你带来最舒服的数据持久化。
封装了常用方法,也保留了Session的获取。内置3种数据库驱动:
H2
MySQL
Sqlit使用
在你的项目中引用
maven:<dependency> <groupId>cn.chahuyun</groupId> <artifactId>hibernate-plus</artifactId> <version>1.0.15</version> <type>module</type> </dependency>
gradle:
implementation("cn.chahuyun:hibernate-plus:1.0.15")
然后创建配置文件
//这里是你的启动类 Configuration configuration = HibernatePlusService.createConfiguration(Test.class); configuration.setDriveType(DriveType.MYSQL); configuration.setAddress("localhost:3306/test"); configuration.setAutoReconnect(true); configuration.setUser("root"); configuration.setPassword("123456"); //configuration.setPackageName("cc.cb.entity");
对于驱动类型DriveType,目前只提供了3种类型
- H2
- MYSQL
- SQLITE
对于java插件的实现:
EconomyConfig config = HuYanEconomy.config; Configuration configuration = HibernatePlusService.createConfiguration(economy.getClass()); configuration.setPackageName("cn.chahuyun.economy.entity"); DriveType dataType = config.getDataType(); configuration.setDriveType(dataType); Path dataFolderPath = economy.getDataFolderPath(); switch (dataType) { case MYSQL: configuration.setAddress(config.getMysqlUrl()); configuration.setUser(config.getMysqlUser()); configuration.setPassword(config.getMysqlPassword()); break; case H2: configuration.setAddress(dataFolderPath.resolve("HuYanEconomy.h2").toString()); break; case SQLITE: configuration.setAddress(dataFolderPath.resolve("HuYanEconomy").toString()); break; } HibernatePlusService.loadingService(configuration);
对于kotlin插件的实现:
val configuration = HibernatePlusService.createConfiguration(plugin::class.java) configuration.classLoader = plugin::class.java.classLoader configuration.packageName = "cn.chahuyun.authorize.entity" configuration.driveType = dataType when (dataType) { MYSQL -> { configuration.address = mysqlUrl configuration.user = mysqlUser configuration.password = mysqlPassword } H2 -> configuration.address = HuYanAuthorize.INSTANCE.dataFolderPath.resolve("authorize.h2.mv.db").toString() SQLITE -> configuration.address = HuYanAuthorize.INSTANCE.dataFolderPath.resolve("authorize.mv.db").toString() } HibernatePlusService.loadingService(configuration)
对于实体映射,可以填写
packageName
,进行指定包扫描,模板将会自动将带有Entity
的实体添加到映射目录中。当然你也可以不填写,那么我将会根据你的
ClassLoader
自动扫描"entry", "entity", "entities", "model", "models", "bean", "beans", "dto"
几个包名下面的实体。给定参数之后就可以通过HibernatePlusService来创建hibernate服务了
HibernatePlusService.loadingService(configuration);
成功后即可使用HibernateFactory来进行数据操作,我这里封装了几个常用的简单操作
List<MyUser> myUsers = HibernateFactory.selectList(MyUser.class); log.info("==========list============="); for (MyUser myUser : myUsers) { log.info(myUser.toString()); } log.info("==========================="); MyUser myUser = new MyUser(); myUser.setName("张"); myUser.setSex(123); Integer id = HibernateFactory.merge(myUser).getId(); log.info("==========one============="); MyUser selectOne = HibernateFactory.selectOne(MyUser.class, id); log.info(selectOne.toString()); log.info("===========================");
更复杂的操作请自行获取
SessionFactory
去创建。SessionFactory session = HibernateFactory.getSession();
自定义使用
你也可以不使用我给你的推荐配置,只需要在resources目录下填写
hibernate.properties
就行,然后通过HibernatePlusService.loadingService(Test.class);
就可以使用自定义配置进行连接