栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis逆向工程与分页在springboot中的应用及遇到坑

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis逆向工程与分页在springboot中的应用及遇到坑

最近在项目中应用到springboot与mybatis,在进行整合过程中遇到一些坑,在此将其整理出来,便于以后查阅与复习。

 项目运行环境为:eclispe+jdk1.8+maven

搭建Spring Boot环境

首先建立maven project,在生成的pom文件中加入依赖,代码如下:


 org.springframework.boot
 spring-boot-starter-parent
 1.5.2.RELEASE
  


 UTF-8
 UTF-8
 1.8


 
  org.springframework.boot
  spring-boot-starter-web
 
 
  org.springframework.boot
  spring-boot-starter-test
 
 
  org.mybatis.spring.boot
  mybatis-spring-boot-starter
  1.3.0
 
 
  mysql
  mysql-connector-java
  runtime
 
 
 
   com.github.pagehelper
   pagehelper-spring-boot-starter
   1.2.1
 
 
 
  com.alibaba
  druid
  1.0.29
 


 
  
   org.springframework.boot
   spring-boot-maven-plugin
  
  
  
   org.mybatis.generator
   mybatis-generator-maven-plugin
   1.3.2
   
    ${basedir}/src/main/resources/generator/generatorConfig.xml
    true
    true
   
  
 

配置好依赖后进行maven install,此时注意的坑:

 坑一:启动maven install报错:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) on project springboot-mybatis: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find main class -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

报错原因是没有启动类

 解决方法:编写启动类Main.java正常运行!

@SpringBootApplication
public class Main {
 public static void main(String[] args) {
  SpringApplication.run(Main.class, args);
 }
}

坑二:启动maven install报错:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project springboot-mybatis: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

报错原因:项目的java环境与电脑环境不符合,例如我的新建项目运行环境为J2SE-1.5,报错是在我将其改为jre1.8之后

 解决方案:

 右键项目——build path——Configure build path——Libraries——双击Jre System Libraries如下图所示:

选择Alternate JRE 如2处的下拉框只有jre,点击3处的Install JREs,依次经过add——Standard VM——next——Directory,选择本机的jdk位置点击finish

在install JREs位置将默认勾选更改jdk,如下图所示,并保存。

再次maven install项目正常。如下所示:

[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.351 s
[INFO] Finished at: 2018-09-05T21:20:48+08:00
[INFO] Final Memory: 23M/181M
[INFO] ------------------------------------------------------------------------

在src/main/resources新建文件:application.yml,内容如下所示:

server:
 port: 8080
spring:
 datasource:
  name: test
  url: jdbc:mysql://localhost:3306/test
  username: root
  password: 123456
  driver-class-name: com.mysql.jdbc.Driver
## 该配置节点为独立的节点
mybatis:
 mapper-locations: classpath:mapper
 @Override
 public List findAllUser(int pageNum, int pageSize) {
  //将参数传给方法实现分页
  PageHelper.startPage(pageNum, pageSize);
  UserExample example = new UserExample();
  List list = userMapper.selectByExample(example);
  return list;
 }
}

最后在controller层对查询结果进行接收,UserController代码如下:

@Controller
@RestController
public class UserController {
 @Autowired
 private UserService userService;
 @RequestMapping("/test")
 public User querUserByName() {
  User user = userService.selectByName("luis");
  System.out.println(user.toString());
  return user;
 }
 @RequestMapping("/list")
 public List querUser() {
  List list = userService.findAllUser(1, 2);
  //获取分页信息
  PageInfo pageInfo = new PageInfo<>(list);
  System.out.println("total:" + pageInfo.getTotal());
  System.out.println("pages:" + pageInfo.getPages());
  System.out.println("pageSize:" + pageInfo.getPageSize());
  return list;
 }
}

测试

此前在项目编写过程中已经对可能出现的错误进行了总结,最后,对项目的功能进行测试,通过加@RestController注解将数据传输到浏览器中。

 测试mybatis与springboot,浏览器输入http://localhost:8080/test,浏览器输出:

{"id":1,"name":"wanger","age":22}

分页测试,浏览器输入http://localhost:8080/list,浏览器输出:

[{"id":1,"name":"wanger","age":22},{"id":2,"name":"zhangsan","age":18}]

eclipse输出:

total:4
pages:2
pageSize:2

项目搭建完毕,具体代码参见github

总结

以上所述是小编给大家介绍的mybatis逆向工程与分页在springboot中的应用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/140066.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号