最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MybatisPlus,無(wú)XML分分鐘實(shí)現(xiàn)CRUD操作

 更新時(shí)間:2020年08月24日 11:21:21   作者:cjx不吃包子  
這篇文章主要介紹了MybatisPlus,無(wú)XML分分鐘實(shí)現(xiàn)CRUD操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

不講太多理論知識(shí),官網(wǎng)都有,直接上手。

1.測(cè)試表

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
 `id` bigint(20) UNSIGNED NOT NULL,
 `name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '用戶名',
 `password` varchar(18) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '密碼',
 `age` int(4) DEFAULT NULL COMMENT '年齡',
 `create_time` datetime(0) DEFAULT NULL COMMENT '創(chuàng)建時(shí)間',
 `update_time` datetime(0) DEFAULT NULL COMMENT '更新時(shí)間',
 PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

引入依賴

<!--mybatis-plus核心包-->
<dependency>
 <groupId>com.baomidou</groupId>
 <artifactId>mybatis-plus-boot-starter</artifactId>
 <version>3.0.5</version>
</dependency>
<!--mysql-->
<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
</dependency>

配置文件,這里我用的是boot項(xiàng)目

spring:
 datasource:
 username: cjx
 password: cjx19950616
 url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
 driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
 mapper-locations: classpath:/mapper/*.xml
 #configuration:
 #map-underscore-to-camel-case: true

其實(shí)簡(jiǎn)單的crud甚至不需要mybatis-plus的配置,我這里沒(méi)刪除,本文也并沒(méi)有用到配置文件。

實(shí)體類

 public class User{
  private Long id;
  private String name;
  private String password;
  private int age;
  private Date createTime;
  private Date updateTime;

 ...
  getter && setter
  ...
  toString()
 }

分頁(yè)插件

@Configuration
public class MyBtaisPlusConfig {
 /**
  * 分頁(yè)插件
  * @return
  */
 @Bean
 public PaginationInterceptor paginationInterceptor() {
  System.out.println("加載分頁(yè)插件");

  return new PaginationInterceptor();
 }
}

編寫一個(gè)我們的接口

@Component
public interface UserDao extends BaseMapper<User> {
 Integer deleteById(Long id);
}

接下來(lái)就可以愉快的測(cè)試了,在我們的測(cè)試類里先注入dao

@Autowired

private UserDao userMapper;

新增測(cè)試

/**
 * 新增
 */
@Test
void save(){

 User u = new User();
 u.setAge(100);
 u.setName("測(cè)試數(shù)據(jù)3");
 u.setPassword("cjx1111");
 u.setAge(25);
 int row = userMapper.insert(u);
 System.out.println(row);
}

是不是很簡(jiǎn)單?只需要這樣就可以完成新增了,既然有單個(gè)插入,那少不了也有批量插入

批量新增測(cè)試

/**
 * 批量插入
 */
@Test
void save(){
 List<User> us = new LinkedList<User>();
  for (int i = 0;i < 50;i++){
   User u = new User();
   u.setName("測(cè)試數(shù)據(jù)"+i);
   u.setAge((int)(Math.random()*90+10));
   u.setPassword("mima"+i);
   us.add(u);
  }
  userService.saveBatch(us);
}

其中用到了userService,我這里貼一下代碼,需要我們接口繼承Iservice,實(shí)現(xiàn)類繼承ServiceImple<M,T>,這樣就可以直接調(diào)用mybatis-plus為我們提供的現(xiàn)成方法了。

public interface IUserService extends IService<User> {

}

實(shí)現(xiàn)類

@Service public class UserServiceImpl extends ServiceImpl<UserDao, User>implements IUserService {

}

修改

/**
 * 修改
 */
@Test
void updateById(){

 User u = new User();
 //修改name
 u.setId(1L);
 u.setName("修改下數(shù)據(jù)--");
 u.setPassword("123");
 u.setAge(99);
 int row = userMapper.updateById(u);
 System.out.println(row);
}
/**
 * 通過(guò)id查詢
 */
@Test
void selectById(){
 //1.通過(guò)id來(lái)查詢
 User u = userMapper.selectById(1l);
 System.out.println(u);

}

mybatis-plus有個(gè)特別好用的就是它的條件構(gòu)造器,可以幫助我們構(gòu)造常用的sql,具體用法我會(huì)貼在文章最后面。

/**
 * 通過(guò)條件構(gòu)造器查詢
 */
@Test
void selectByWrapper(){

 //3.通過(guò)條件構(gòu)造器wrapper
 QueryWrapper qw = new QueryWrapper();
 qw.like("name","測(cè)試");
 qw.lt("age",50);//小于
 qw.gt("age",15);//大于

 List<User> us = userMapper.selectList(qw);
 us.stream().forEach(System.out::println);

}

既然有查詢,自然少不了我們最關(guān)心的分頁(yè)

/**
 * 通過(guò)條件構(gòu)造器和page分頁(yè)查詢
 */
@Test
void selectByWrapperAndPage(){

 //4.通過(guò)條件構(gòu)造器wrapper和Page分頁(yè)查詢
 QueryWrapper qw = new QueryWrapper();
 qw.like("name","測(cè)試");
 qw.lt("age",50);//小于
 qw.gt("age",15);//大于
 qw.orderByDesc("age");

 IPage<User> result = userMapper.selectPage(new Page<>(2,10),qw);
/**
 * 查詢總記錄數(shù)
 */
@Test
void selectCount(){

 //5.通過(guò)條件查詢總條數(shù)
 QueryWrapper qw = new QueryWrapper();
 qw.like("name","測(cè)試");
 qw.lt("age",50);//小于
 qw.gt("age",15);//大于
 qw.orderByDesc("age");

 int count = userMapper.selectCount(qw);
 System.out.println(count);

}

刪除

/**
 * 通過(guò)id刪除
 */
@Test
void delete(){

 int row = userMapper.deleteById(27l);
 System.out.println(row);
}

/**
 * 通過(guò)id批量刪除
 */
@Test
void deleteBatch(){

 int row = userMapper.deleteBatchIds(Arrays.asList(67l,44l,37l,220l));
 System.out.println(row);
}
/**
 * 通過(guò)條件刪除
 */
@Test
void deleteByWrapper(){

 QueryWrapper<User> qw = new QueryWrapper<>();
 qw.gt("age",70);
 //先來(lái)看看有多少數(shù)據(jù)
 int row = userMapper.selectCount(qw);
 System.out.println(row);
 //刪除
 int deleteRow = userMapper.delete(qw);
 System.out.println(deleteRow);
}

今天就到這,后續(xù)會(huì)給大家分享下mybatis-plus提供的sql性能執(zhí)行分析插件、樂(lè)觀鎖插件以及字段的自動(dòng)填充。

補(bǔ)充知識(shí):Mybatis-plus 自動(dòng)生成代碼后xml文件和mapper映射不到的原因

報(bào)了如下錯(cuò)誤

找了很久都沒(méi)找到原因

2020-06-08 18:48:12 |ERROR |http-nio-8130-exec-3 |GlobalExceptionHandler.java:25 |service.base.handler.GlobalExceptionHandler |org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): service.used.mapper.ClassifyMapper.selectNestedListByParentId
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:50)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedMapperMethod$0(MybatisMapperProxy.java:101)
	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedMapperMethod(MybatisMapperProxy.java:100)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:95)
	at com.sun.proxy.$Proxy98.selectNestedListByParentId(Unknown Source)
	at xyz.oneadd.platform.service.used.service.impl.ClassifyServiceImpl.nestedList(ClassifyServiceImpl.java:25)
	at xyz.oneadd.platform.service.used.service.impl.ClassifyServiceImpl$$FastClassBySpringCGLIB$$85da3417.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:685)
	at xyz.oneadd.platform.service.used.service.impl.ClassifyServiceImpl$$EnhancerBySpringCGLIB$$3b7bb86f.nestedList(<generated>)
	at xyz.oneadd.platform.service.used.controller.api.ApiClassifyController.classifyList(ApiClassifyController.java:34)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1591)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

原因是pom中沒(méi)有添加bulid依賴

添加依賴后解決問(wèn)題

<build>
  <resources>
   <resource>
    <directory>src/main/java</directory>
    <includes>
     <include>**/*.xml</include>
    </includes>
    <filtering>false</filtering>
   </resource>
  </resources>
 </build>

以上這篇MybatisPlus,無(wú)XML分分鐘實(shí)現(xiàn)CRUD操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring中@RequestParam使用及遇到的一些坑

    Spring中@RequestParam使用及遇到的一些坑

    @RequestParam 主要用于將請(qǐng)求參數(shù)區(qū)域的數(shù)據(jù)映射到控制層方法的參數(shù)上,下面這篇文章主要給大家介紹了關(guān)于Spring中@RequestParam使用及遇到的一些坑,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • IDEA解決springboot熱部署失效問(wèn)題(推薦)

    IDEA解決springboot熱部署失效問(wèn)題(推薦)

    熱部署,就是在應(yīng)用正在運(yùn)行的時(shí)候升級(jí)軟件,卻不需要重新啟動(dòng)應(yīng)用。這篇文章主要介紹了IDEA解決springboot熱部署失效問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java基礎(chǔ)學(xué)習(xí)之集合底層原理

    Java基礎(chǔ)學(xué)習(xí)之集合底層原理

    今天帶大家回顧Java基礎(chǔ)的相關(guān)知識(shí),文中對(duì)集合底層原理作了非常詳細(xì)的圖文介紹,對(duì)Java初學(xué)者有非常好的幫助,需要的朋友可以參考下
    2021-05-05
  • java實(shí)現(xiàn)隨機(jī)生成UUID

    java實(shí)現(xiàn)隨機(jī)生成UUID

    這篇文章主要介紹了java實(shí)現(xiàn)隨機(jī)生成UUID的函數(shù)代碼,有需要的小伙伴可以參考下。
    2015-07-07
  • 使用?Spring?AI?+?Ollama?構(gòu)建生成式?AI?應(yīng)用的方法

    使用?Spring?AI?+?Ollama?構(gòu)建生成式?AI?應(yīng)用的方法

    通過(guò)集成SpringBoot和Ollama,本文詳細(xì)介紹了如何構(gòu)建生成式AI應(yīng)用,首先,介紹了AI大模型服務(wù)的兩種實(shí)現(xiàn)方式,選擇使用ollama進(jìn)行部署,隨后,通過(guò)SpringBoot+SpringAI來(lái)實(shí)現(xiàn)應(yīng)用構(gòu)建,本文為開發(fā)者提供了一個(gè)實(shí)用的指南,幫助他們快速入門生成式AI應(yīng)用的開發(fā)
    2024-11-11
  • 基于java HashMap插入重復(fù)Key值問(wèn)題

    基于java HashMap插入重復(fù)Key值問(wèn)題

    這篇文章主要介紹了基于java HashMap插入重復(fù)Key值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • SpringBoot Logback日志記錄到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法

    SpringBoot Logback日志記錄到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法

    這篇文章主要介紹了SpringBoot Logback日志記錄到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • java向上轉(zhuǎn)型與向下轉(zhuǎn)型詳解

    java向上轉(zhuǎn)型與向下轉(zhuǎn)型詳解

    這篇文章主要為大家詳細(xì)介紹了java向上轉(zhuǎn)型與向下轉(zhuǎn)型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Spring Boot 集成 ElasticSearch應(yīng)用小結(jié)

    Spring Boot 集成 ElasticSearch應(yīng)用小結(jié)

    這篇文章主要介紹了Spring Boot 集成 ElasticSearch應(yīng)用小結(jié),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-11-11
  • Spring Boot常見(jiàn)外部配置文件方式詳析

    Spring Boot常見(jiàn)外部配置文件方式詳析

    這篇文章主要給大家介紹了關(guān)于Spring Boot常見(jiàn)外部配置文件方式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評(píng)論

白水县| 措勤县| 二手房| 荥阳市| 象山县| 普宁市| 澄江县| 齐齐哈尔市| 台安县| 左云县| 河西区| 永靖县| 简阳市| 四会市| 冷水江市| 连云港市| 东宁县| 施秉县| 阿拉善右旗| 鞍山市| 得荣县| 彰化县| 澄迈县| 临清市| 凌云县| 信宜市| 大田县| 澄迈县| 东至县| 泰州市| 扶风县| 清涧县| 临武县| 特克斯县| 富裕县| 津市市| 彭州市| 广州市| 平湖市| 报价| 四平市|