Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MyBatis 是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架,但并不是完整的orm框架。MyBatisDao扩展了MyBatis的orm部分,能像Hibernate那样使用MyBatis,同时不影响mybatis的其他部分。

MyBatisDao特性

  1. 支持java类和数据库表的映射。
  2. 支持一对一、一对多、多对多关联。
  3. 支持懒加载、立即加载模式。
  4. 支持分页
  5. 支持选择操作的字段。
  6. 支持动态表名。
  7. 支持多数据源。

快速入门(以springboot为例)

配置MyBatisDao

@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()};    }}

创建bean类

@Table//声明了Blog类所对应的数据库表publicclassBlog {@Id//数据库表 id主键,如果是oracle数据库需要加@Seq注解,指定id的自增序列privateintid;@Column("title")//数据库中表中所对应的字段,如果数据库表字段和类当中的字段相同可以省略,默认会把驼峰命名的字段转换为下划线,如 msgId-->msg_idprivateStringtitle;privateStringcontent;privateintauthorId;//省略set get方法}

创建数据库对应的表blog

省略建表过程

创建UserConroller

@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有问题可邮件联系我!

About

MyBatis 是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架,但并不是完整的orm框架。 MyBatisDao扩展了MyBatis的orm部分,能像Hibernate那样使用MyBatis,同时不影响mybatis的其他部分。

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp