今天给大家推荐的是一个超轻量级的orm,可以根据java实体自动生成和修改表结构,提供BasicDaoImpl<T,ID>作为抽象父类,通过泛型和反射可提供子类的增删改查和分页功能。通过Filter和Order等面向对象的API进行条件查询和分页,本框架基于Spring配置,可以无缝隙和Spring集成。是想用hibernate或jpa但又不像学习他的繁琐api,想用mybatis但又不想写sql和创建表结构的程序猿的不二选择!!!!
不废话了,源码地址:https://github.com/Mryong/jorm jar包下载地址:http://download.csdn.net/detail/mr_yong/8127779
本框架由maven构建,下载下来通过pom导入项目,在本地mysql创建一个名为jorm的数据库,可以只跑test包下的单元测试,表就自动好了!!!
好吧,还是写点示例代码吧。。。。。。
首先吧,你歹要创建一个pojo,唉,也就是一个entity,比如:
package org.jerry.jorm.test.entiry; import org.jerry.jorm.annotation.Entity; import org.jerry.jorm.annotation.Id; import org.jerry.jorm.annotation.Table; /** * Created by yong_pliang on 14/10/31. */ @Entity @Table(name = "t_person") public class Person { public enum Gender { female, male } private String id; private Gender gender = Gender.female; private String name; private Integer age; private String address; @Id public String getId() { return id; } public void setId(String id) { this.id = id; } public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
然后已要创建一个dao吧,作为一个务实派,无不喜欢所有的dao有些一个接口,当然你也可以写,并且框架已经提供了一个BaicDao<T,ID>作为公共接口,你可以继承这个接口啦!
好了,我们来写一个PersonDao
package org.jerry.jorm.test.dao; import org.jerry.jorm.BasicDaoImpl; import org.jerry.jorm.test.entiry.Person; import org.springframework.stereotype.Repository; /** * Created by yong_pliang on 14/10/31. */ @Repository public class PersonDao extends BasicDaoImpl<Person,String> { }
精呆了有木有!!!!这个dao什么都不用写!!只要 extends BasicDaoImpl<Person,String> ,告诉父类,你这Perosn实体的Dao,Person的ID是String类型!
good,下面来看看我们能做什么了!
package org.jerry.jorm.test; import org.jerry.jorm.Filter; import org.jerry.jorm.Order; import org.jerry.jorm.test.dao.PersonDao; import org.jerry.jorm.test.entiry.Person; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; /** * Created by yong_pliang on 14/10/31. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") @TransactionConfiguration(defaultRollback = false) @Transactional public class PersonDaoTester { @Resource private PersonDao personDao; @Test public void test1() { Person person = new Person(); person.setId("1"); person.setGender(Person.Gender.female); person.setName("zhangsan"); person.setAge(12); personDao.save(person); } @Test public void test2() { Person person = personDao.find("1"); System.out.println(person); } @Test public void test3() { List<Filter> filters = new ArrayList<Filter>(); filters.add(Filter.eq("name", "zhangsan")); List<Person> persons = personDao.findList(filters); System.out.println(persons); } @Test public void test4() { List<Person> persons = personDao.findList(null, Order.asc("id")); System.out.println(persons); } @Test public void test5() { List<Filter> filters = new ArrayList<Filter>(); filters.add(Filter.eq("name", "zhangsan")); long count = personDao.count(filters.toArray(new Filter[]{})); System.out.println(count); } }
增删该查神马分页都有啦!!!当然还有许多其他功能,请联系我哦,QQ:76094586
时间: 2024-11-08 05:48:04