当前位置: 首页 > news >正文

高唐网站建设公司/站长seo查询工具

高唐网站建设公司,站长seo查询工具,那家建设网站p2p公司最好,wordpress反向代理 谷歌前言 MyBatis-Plus对mybatis一些简单crud语句进行了的封装,因此对于不复杂的单表,可以直接使用提供出来的API即可。 CRUD接口 通过官网可以看到有两种CRUD接口,一种是Mapper,一种是Service。这两种具体是怎么回事呢?…

前言

MyBatis-Plus对mybatis一些简单crud语句进行了的封装,因此对于不复杂的单表,可以直接使用提供出来的API即可。

 

CRUD接口

通过官网可以看到有两种CRUD接口,一种是Mapper,一种是Service。这两种具体是怎么回事呢?简单说一下,Mapper是相对基础一点的接口,Service是对Mapper再进行了一次封装的接口,Service内部也会调用Mapper的方法。单纯查询效果来说,两者是一样的。只要根据Web框架的层级去使用就好了,Service放Service层,Mapper放Mapper层。

下面展示一下例子。Mapper的例子上一篇文章已经试过了,这里尝试Service的例子

Service示例

在Service层添加UserService接口,继承IService接口。相关的业务逻辑接口可以在下面定义,本例子先留空

package com.sadoshi.mybatisplus.springboottest.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.sadoshi.mybatisplus.springboottest.model.User;public interface UserService extends IService<User>{}

 

接着创建其实现类UserServiceImpl,这个类要继承ServiceImpl<UserMapper, User>类,这样Service和Mapper就关联起来了。

package com.sadoshi.mybatisplus.springboottest.serviceImpl;import org.springframework.stereotype.Component;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sadoshi.mybatisplus.springboottest.mapper.UserMapper;
import com.sadoshi.mybatisplus.springboottest.model.User;
import com.sadoshi.mybatisplus.springboottest.service.UserService;@Component
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{}

 

接着在UserComponent这个组件类上添加测试方法,同时把UserServiceImpl自动注入到里面。这个方法很简单,同样是select all的效果,查出所有user表里面的数据。

package com.sadoshi.mybatisplus.springboottest.component;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sadoshi.mybatisplus.springboottest.mapper.UserMapper;
import com.sadoshi.mybatisplus.springboottest.model.User;
import com.sadoshi.mybatisplus.springboottest.serviceImpl.UserServiceImpl;@Component
public class UserComponent {@Autowiredprivate UserMapper userMapper;@Autowiredprivate UserServiceImpl userServiceImpl;/*
......
*/public void testServiceImpl() {List<User> resultList = userServiceImpl.list();for(User user : resultList) {System.out.println(user);}}
}

然后在主类App上调用testServiceImpl方法即可,查看控制台结果

 

Wrapper的使用

几年前做项目的时候,本来项目是用mybatis的。后来新招了一个主程来负责这个项目,他引入MyBatis-Plus,然后冒出来一大堆wrapper,我当时也有点蒙。其实wrapper的使用很简单,可能官网都不屑于写个简单的例子,但一开始接触的同学可能和我一样有些懵逼,稍微给个例子看看估计就明白了。

往UserComponent中添加testQueryWrapper方法。这里设置查询name字段等于tom的记录。

package com.sadoshi.mybatisplus.springboottest.component;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sadoshi.mybatisplus.springboottest.mapper.UserMapper;
import com.sadoshi.mybatisplus.springboottest.model.User;
import com.sadoshi.mybatisplus.springboottest.serviceImpl.UserServiceImpl;@Component
public class UserComponent {/*
......
*/public void testQueryWrapper() {System.out.println(("----- QueryWrapper test ------"));QueryWrapper<User> warpper = new QueryWrapper<>();warpper.eq("name", "Tom");List<User> resultList = userMapper.selectList(warpper);for(User user : resultList) {System.out.println(user);}}
}

执行testQueryWrapper方法,得到结果。

wrapper除了QueryWrapper,还有UpdateWrapper,对应update操作。具体的wrapper方法可以通过官网查找,因为有很多,而且可以各种搭配使用,就不一一展示了。

 

分页

Mybatis-Plus也提供了分页的功能,首先引入插件

package com.sadoshi.mybatisplus.springboottest.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;@Configuration
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}
}

 

接着我们写一个测试分页。Page有两个两个参数,第一个代表展示第几页(从1开始算),第二个代表一页有多少条记录。这里wrapper我们先留空。

package com.sadoshi.mybatisplus.springboottest.component;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sadoshi.mybatisplus.springboottest.mapper.UserMapper;
import com.sadoshi.mybatisplus.springboottest.model.User;
import com.sadoshi.mybatisplus.springboottest.serviceImpl.UserServiceImpl;@Component
public class UserComponent {@Autowiredprivate UserMapper userMapper;/*
......
*/public void testPage() {Page page = new Page(2,3);QueryWrapper<User> warpper = new QueryWrapper<>();userMapper.selectPage(page, warpper);}
}

执行后看控制台,已查出4、5、6三条记录了,执行正确。看输出的信息,这个分页插件,底层也是通过limit实现的。

 

http://www.lbrq.cn/news/1370719.html

相关文章:

  • 株洲做网站客服电话/今日新闻热点10条
  • 低价货源网站/宁波seo网络推广定制多少钱
  • 网站源码模板/热门搜索排行榜
  • 门户网站开发视频/百度24小时人工电话
  • 网站怎么做跳转/百度2023免费下载
  • dreamweaver 企业网站模板/优化设计答案五年级上册
  • 网站搭建百家号/百度智能云
  • 网站源码怎么打开/企业seo优化
  • 企业网站建设的/小红书怎么推广
  • 珠海七中科技制作/苏州seo服务
  • 企业黄页网站源码/长尾关键词举例
  • 自学网站建设/武汉新闻最新消息
  • 网页站点不安全怎么办/2021国内最好用免费建站系统
  • 常州网上房地产官网/网站优化推广方案
  • 网站管理员要干些什么/什么是seo技术
  • 网站建设的价钱/网站seo排名优化方法
  • 秦皇岛网站制作多少钱/济南网站制作平台
  • 品牌网站建设解决/人工智能培训机构排名
  • php婚庆网站/seo优化的主要任务
  • 做网站需要会的软件/百度云官方网站
  • 石家庄做外贸网站建设/哈尔滨网站优化
  • 福建网站建设费用/seo长尾关键词优化
  • 嘉兴做毛织的有哪些网站/百度公司在哪
  • 建设行业网站/小程序推广运营的公司
  • 徐州学习网站建设/班级优化大师官网登录
  • 搭建网站源码/最近的大新闻
  • 有很多长尾怎么做网站内容/苏州seo安严博客
  • 建设外贸网站多少钱/百度推广代理商赚钱吗
  • 昆明做网站建设的公司排名/成人教育机构排行前十名
  • 南平企业网站建设/爱站网关键词长尾挖掘
  • k8s+isulad 国产化技术栈云原生技术栈搭建2-crictl
  • 关于域名的级别
  • kafka 是一个怎样的系统?是消息队列(MQ)还是一个分布式流处理平台?
  • PostgreSQL面试题及详细答案120道(21-40)
  • 决策树学习全解析:从理论到实战
  • 【MQ】kafka同步和异步的区别