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

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

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

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

1.測試表

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)建時間',
 `update_time` datetime(0) DEFAULT NULL COMMENT '更新時間',
 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項目

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

其實簡單的crud甚至不需要mybatis-plus的配置,我這里沒刪除,本文也并沒有用到配置文件。

實體類

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

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

分頁插件

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

  return new PaginationInterceptor();
 }
}

編寫一個我們的接口

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

接下來就可以愉快的測試了,在我們的測試類里先注入dao

@Autowired

private UserDao userMapper;

新增測試

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

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

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

批量新增測試

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

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

public interface IUserService extends IService<User> {

}

實現(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);
}
/**
 * 通過id查詢
 */
@Test
void selectById(){
 //1.通過id來查詢
 User u = userMapper.selectById(1l);
 System.out.println(u);

}

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

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

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

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

}

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

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

 //4.通過條件構(gòu)造器wrapper和Page分頁查詢
 QueryWrapper qw = new QueryWrapper();
 qw.like("name","測試");
 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.通過條件查詢總條數(shù)
 QueryWrapper qw = new QueryWrapper();
 qw.like("name","測試");
 qw.lt("age",50);//小于
 qw.gt("age",15);//大于
 qw.orderByDesc("age");

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

}

刪除

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

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

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

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

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

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

補充知識:Mybatis-plus 自動生成代碼后xml文件和mapper映射不到的原因

報了如下錯誤

找了很久都沒找到原因

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中沒有添加bulid依賴

添加依賴后解決問題

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

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

相關(guān)文章

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

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

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

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

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

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

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

    java實現(xiàn)隨機生成UUID

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

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

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

    基于java HashMap插入重復Key值問題

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

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

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

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

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

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

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

    Spring Boot常見外部配置文件方式詳析

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

最新評論

新竹市| 太原市| 牙克石市| 垫江县| 勐海县| 新巴尔虎左旗| 芦山县| 文昌市| 积石山| 南和县| 朝阳县| 环江| 东光县| 苗栗市| 基隆市| 太保市| 上思县| 楚雄市| 东台市| 鄯善县| 吕梁市| 吴堡县| 绥棱县| 隆昌县| 图片| 浮梁县| 通河县| 浦城县| 尼玛县| 临清市| 漾濞| 襄汾县| 卫辉市| 苏尼特左旗| 旬邑县| 宁安市| 清水县| 云南省| 台湾省| 东乌| 葫芦岛市|