mybatis 是一款优秀的持久层框架,它支持定制化 sql、存储过程以及高级映射。mybatis 避免了几乎所有的 jdbc 代码和手动设置参数以及获取结果集。mybatis 可以使用简单的 xml 或注解来配置和映射原生类型、接口和 java 的 pojo(plain old java objects,普通老式 java 对象)为数据库中的记录。
1)sql语句与代码分离,存放于xml配置文件中,方便管理 2)用逻辑标签控制动态sql的拼接,灵活方便 3)查询的结果集与java对象自动映射 4)编写原生态sql,接近jdbc 5)简单的持久化框架,框架不臃肿简单易学
mybatis专注于sql本身,是一个足够灵活的dao层解决方案。
对性能的要求很高,或者需求变化较多的项目,mybatis将是不错的选择。
采用druid连接池,该连接池。
<!-- mybatis依赖 --> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>1.3.2</version> </dependency> <!-- mybatis的分页插件 --> <dependency> <groupid>com.github.pagehelper</groupid> <artifactid>pagehelper</artifactid> <version>4.1.6</version> </dependency>
mybatis: # mybatis配置文件所在路径 config-location: classpath:mybatis.cfg.xml type-aliases-package: com.boot.mybatis.entity # mapper映射文件 mapper-locations: classpath:mapper/*.xml
这里就不贴代码了。
// 增加 int insert(imginfo record); // 组合查询 list<imginfo> selectbyexample(imginfoexample example); // 修改 int updatebyprimarykeyselective(imginfo record); // 删除 int deletebyprimarykey(integer imgid);
@service public class imginfoserviceimpl implements imginfoservice { @resource private imginfomapper imginfomapper ; @override public int insert(imginfo record) { return imginfomapper.insert(record); } @override public list<imginfo> selectbyexample(imginfoexample example) { return imginfomapper.selectbyexample(example); } @override public int updatebyprimarykeyselective(imginfo record) { return imginfomapper.updatebyprimarykeyselective(record); } @override public int deletebyprimarykey(integer imgid) { return imginfomapper.deletebyprimarykey(imgid); } }
@restcontroller public class imginfocontroller { @resource private imginfoservice imginfoservice ; // 增加 @requestmapping("/insert") public int insert(){ imginfo record = new imginfo() ; record.setuploaduserid("a123"); record.setimgtitle("博文图片"); record.setsystemtype(1) ; record.setimgtype(2); record.setimgurl("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4"); record.setlinkurl("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4"); record.setshowstate(1); record.setcreatedate(new date()); record.setupdatedate(record.getcreatedate()); record.setremark("知了"); record.setbenable("1"); return imginfoservice.insert(record) ; } // 组合查询 @requestmapping("/selectbyexample") public list<imginfo> selectbyexample(){ imginfoexample example = new imginfoexample() ; example.createcriteria().andremarkequalto("知了") ; return imginfoservice.selectbyexample(example); } // 修改 @requestmapping("/updatebyprimarykeyselective") public int updatebyprimarykeyselective(){ imginfo record = new imginfo() ; record.setimgid(11); record.setremark("知了一笑"); return imginfoservice.updatebyprimarykeyselective(record); } // 删除 @requestmapping("/deletebyprimarykey") public int deletebyprimarykey() { integer imgid = 11 ; return imginfoservice.deletebyprimarykey(imgid); } }
http://localhost:8010/insert http://localhost:8010/selectbyexample http://localhost:8010/updatebyprimarykeyselective http://localhost:8010/deletebyprimarykey
<?xml version="1.0" encoding="utf-8" ?> <!doctype configuration public "-//mybatis.org//dtd config 3.0//en" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <plugins> <!--mybatis分页插件--> <plugin interceptor="com.github.pagehelper.pagehelper"> <property name="dialect" value="mysql"/> </plugin> </plugins> </configuration>
@override public pageinfo<imginfo> querypage(int page,int pagesize) { pagehelper.startpage(page,pagesize) ; imginfoexample example = new imginfoexample() ; // 查询条件 example.createcriteria().andbenableequalto("1").andshowstateequalto(1); // 排序条件 example.setorderbyclause("create_date desc,img_id asc"); list<imginfo> imginfolist = imginfomapper.selectbyexample(example) ; pageinfo<imginfo> pageinfo = new pageinfo<>(imginfolist) ; return pageinfo ; }
http://localhost:8010/querypage
github地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 码云地址:知了一笑 https://gitee.com/cicadasmile/spring-boot-base
如对本文有疑问, 点击进行留言回复!!
VideoStudio多相机编辑器怎么用? VideoStudio多相机的使用方法
网友评论