- Notifications
You must be signed in to change notification settings - Fork3
MyBatis 是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架,但并不是完整的orm框架。 MyBatisDao扩展了MyBatis的orm部分,能像Hibernate那样使用MyBatis,同时不影响mybatis的其他部分。
License
NotificationsYou must be signed in to change notification settings
wulizhong/mybatis-dao
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
MyBatis 是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架,但并不是完整的orm框架。MyBatisDao扩展了MyBatis的orm部分,能像Hibernate那样使用MyBatis,同时不影响mybatis的其他部分。
- 支持java类和数据库表的映射。
- 支持一对一、一对多、多对多关联。
- 支持懒加载、立即加载模式。
- 支持分页
- 支持选择操作的字段。
- 支持动态表名。
- 支持多数据源。
@Configuration@MapperScan("org.mybatis.dao.mapper")publicclassMybatisDaoConfig {@Bean(name ="dao")@PrimarypublicDaodao(@Qualifier("daoMapper")DaoMapperdaoMapper)throwsException {returnnewDao(daoMapper,newDaoConfig() {@OverridepublicDataBasegetDataBase() {// TODO Auto-generated method stubreturnDataBase.MYSQL;}}); }@Bean(name ="interceptors")@PrimarypublicInterceptor[]interceptors(){returnnewInterceptor[]{newDaoPlugin()}; }}
@Table//声明了Blog类所对应的数据库表publicclassBlog {@Id//数据库表 id主键,如果是oracle数据库需要加@Seq注解,指定id的自增序列privateintid;@Column("title")//数据库中表中所对应的字段,如果数据库表字段和类当中的字段相同可以省略,默认会把驼峰命名的字段转换为下划线,如 msgId-->msg_idprivateStringtitle;privateStringcontent;privateintauthorId;//省略set get方法}
省略建表过程
@RestControllerpublicclassUserController {@AutowiredprivateDaodao;//将dao注入进来}
@RequestMapping("/insertBlog")publicBloginsertBlog() {Blogb =newBlog();b.setTitle("插入测试");//save方法会将Blog关联的对象一起插入到数据库中intcount =dao.insert(b);//insert方法只会插入Blog对象//int count = dao.save(b);//将Blog对象插入到blog_2表中//int count = dao.insert(b, "blog_2");System.out.println("插入数据条数:" +count);returnb;}
@RequestMapping("/getBlog")publicBloggetBlog() {//查询id为5的对象,会查询出关联对象Blogb =dao.find(Blog.class,5);//查询id为5的对象,只查询Blog对象,不关联查询//Blog b = dao.selectOne(Blog.class, 5);//按条件查询,查询title=abc id>3对象,会查询出关联对象Blogb2 =dao.find(Blog.class,Cnd.where("title","=","abc").and("id",">",3));//按条件查询,查询title=abc id>3对象,不关联查询//Blog b2 = dao.selectOne(Blog.class, Cnd.where("title", "like", "%abc%").and("id", ">", 3));//查询id为5的对象,并且只查询title、content的值Blogb3 =dao.selectOne(Blog.class,FieldFilter.include("title","content"),5);//Blog b3 = dao.selectOne(Blog.class, FieldFilter.include("title","content"), Cnd.where("id", "=", 5));//查询id为5的对象,并且只除了title,"content"之外所有的的值Blogb4 =dao.selectOne(Blog.class,FieldFilter.exclude("title","content"),5);//Blog b4 = dao.selectOne(Blog.class, FieldFilter.exclude("title","content"), Cnd.where("id", "=", 5));//从blog_2表中查询id为5的对象Blogb5 =dao.selectOne(Blog.class,"blog_2",5);//Blog b5 = dao.selectOne(Blog.class, "blog_2", Cnd.where("id", "=", 5));//从blog_2表中查询id为5的对象,并且只查询title、content的值Blogb6 =dao.selectOne(Blog.class,"blog_2",FieldFilter.include("title","content"),5);//Blog b6 = dao.selectOne(Blog.class, "blog_2", FieldFilter.include("title","content"), Cnd.where("id", "=", 5));//从blog_2表中查询id为5的对象,并且只除了title,"content"之外所有的的值Blogb7 =dao.selectOne(Blog.class,"blog_2",FieldFilter.exclude("title","content"),5);//Blog b7 = dao.selectOne(Blog.class, "blog_2", FieldFilter.exclude("title","content"), Cnd.where("id", "=", 5));returnb;}
@RequestMapping("/getBlogs")publicList<Blog>getBlogs(){//查询 title中包含abc字符并且id大于5的记录,按照title排序,第2页的每页10条的数据,会查询关联对象List<Blog>blogs =dao.query(Blog.class,Cnd.where("title","like","%abc%").and("id",">",5).orderBy("title"),newPage(2,10));//从blog_2表中查询 title中包含abc字符并且id大于5的记录只包含title字段的值,按照title排序,第2页的每页10条的数据//List<Blog> blogs = dao.selectList(Blog.class,"blog_2",FieldFilter.include("title"), Cnd.where("title", "like", "%abc%").and("id", ">", 5).orderBy("title"),new Page(2, 10));returnblogs;}
@RequestMapping("/updateBlog")publicBlogupdateBlog() {Blogb =dao.find(Blog.class,5);b.setContent("修改之后的内容3!");b.setTitle("修改测试");//更新blog对象dao.update(b);//只更新title和content字段dao.update(b,FieldFilter.include("title","content"));b =dao.selectOne(Blog.class,"blog_2",5);b.setContent("修改之后的内容3!");b.setTitle("修改测试");//只更新blog_2表中的title和content字段dao.update(b,"blog_2",FieldFilter.include("title","content"));returnb;}
@RequestMapping("/deleteBlog")publicvoiddeleteBlog() {Blogb =dao.selectOne(Blog.class,"blog_2",1);dao.delete(b);dao.delete(Blog.class,Cnd.where("id","=",2));dao.delete(Blog.class,"blog_2",Cnd.where("id","=",2));}
邮箱rz_wlz@126.com有问题可邮件联系我!