作用于mybatis的mapper的单表增删改查(mybatisv3.2.4+,无xml配置配置哦)。
1、entity配置packagecom.demo.domain;importjava.util.Date;importjavax.annotation.Resource;importorg.apache.ibatis.annotations.Result;importorg.apache.ibatis.annotations.SelectKey;importorg.apache.ibatis.type.JdbcType;@ResourcepublicclassBlog{privateIntegerid;privateStringtitle;privateDatepublishtime;privateStringcontent;publicvoidsetId(Integerid){this.id=id;}publicvoidsetTitle(Stringtitle){this.title=title;}publicvoidsetPublishtime(Datepublishtime){this.publishtime=publishtime;}publicvoidsetContent(Stringcontent){this.content=content;}//callidentity()//SELECTLAST_INSERT_ID()@Result(id=true)@SelectKey(statement="SELECTLAST_INSERT_ID()",before=false,keyProperty="id",keyColumn="id",resultType=Integer.class)publicIntegergetId(){returnid;}publicStringgetTitle(){returntitle;}@Result(jdbcType=JdbcType.TIMESTAMP)publicDategetPublishTime(){returnpublishtime;}publicStringgetContent(){returncontent;}}Result可以配置TypeHandler
2、mybatis配置publicvoidt(){Objectb=null;//Mapper添加MapperRegistryProxyproxy=MapperRegistryProxy.use(sqlSessionFactory);proxy.addMapper(BlogMapper.class);SqlSessionsqlSession=sqlSessionFactory.openSession(true);BlogMappermapper=sqlSession.getMapper(BlogMapper.class);}MapperRegistryProxy注册mapper模板.
3、spring整合<beanclass="com.mapperdb.spring.MapperScannerConfigurerSupprot"><propertyname="basePackage"value="com.demo.mapper"/><propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/></bean>其中MapperScannerConfigurerSupprot是mapper模板的扫描器.
4、mapper使用方式packagecom.demo.mapper;importcom.demo.domain.Blog;importcom.mapperdb.mapper.Mapper;publicinterfaceBlogMapperextendsMapper<Blog>{}其中BlogMapper继承已经定义好的mapper模板.还可以实现自己的mapper模板.
5、注意事项text建议和mapper模板和mybatis的mapper不放在同一个包中,方便扫描。
评论