mybatis逆向工程与分页在springboot中怎么用
小编给大家分享一下mybatis逆向工程与分页在springboot中怎么用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
作为一家“创意+整合+营销”的成都网站建设机构,我们在业内良好的客户口碑。成都创新互联公司提供从前期的网站品牌分析策划、网站设计、成都网站建设、成都做网站、创意表现、网页制作、系统开发以及后续网站营销运营等一系列服务,帮助企业打造创新的互联网品牌经营模式与有效的网络营销方法,创造更大的价值。
项目运行环境为: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/*.xml #pagehelper分页插件 pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql
至此springboot环境搭建完毕!
逆向工程应用
首先应该注意到在pom文件中有配置逆向工程xml文件的位置:src/main/resources/generator/generatorConfig.xml
因而在src/main/resources下新建generator文件夹,并建立generatorConfig.xml文件,代码如下:
根据个人环境将配置文件中的配置进行更改,如数据库密码,包名,对应数据库表
所用的数据库表如下:
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` bigint(20) NOT NULL, `name` varchar(255) NOT NULL, `age` int(4) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES ('1', 'wanger', '22'); INSERT INTO `user` VALUES ('2', 'zhangsan', '18'); INSERT INTO `user` VALUES ('3', 'lisi', '23'); INSERT INTO `user` VALUES ('4', 'wangwu', '21');
配置完成后,右键项目,选择run as——Maven build——在下面两处分别填入:
Goals: mybatis-generator:generate -e
Profiles: generatorConfig.xml
如下图所示:
出现如下所示,代码生成成功,刷新项目即可。
[INFO] Generating Example class for table user
[INFO] Generating Record class for table user
[INFO] Generating Mapper Interface for table user
[INFO] Generating SQL Map for table user
[INFO] Saving file UserMapper.xml
[INFO] Saving file UserExample.java
[INFO] Saving file User.java
[INFO] Saving file UserMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
需要注意的是:逆向工程生成的代码不会覆盖,因而不能重复多次生成。
此处也有个小坑,逆向工程的代码生成后,启动项目会报如下错误:
***************************
APPLICATION FAILED TO START
***************************Description:
Field userMapper in com.luis.service.impl.UserServiceImpl required a bean of type 'com.luis.mapper.UserMapper' that could not be found.
Action:
Consider defining a bean of type 'com.luis.mapper.UserMapper' in your configuration.
解决方案:
1、给生成的mapper接口文件前加注解:@Mapper 即可解决。但需要给每一个mapper文件前加,繁琐,因而有第二种类
解决办法
2、在启动类前加@MapperScan({"com.luis.mapper"}),其中com.luis.mapper为mapper文件的所在位置。
参考自:http://412887952-qq-com.iteye.com/blog/2392672
分页应用
逆向工程已经生成了entity类,及dao层的mapper接口与*mapper.xml文件,因而/只用编写service层与web层。
首先在UserService中编写接口,代码如下:
public interface UserService { User selectByName(String name); ListfindAllUser(int pageNum, int pageSize); }
在UserServiceImpl文件进行实现,代码如下所示:
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User selectByName(String name) { UserExample example = new UserExample(); Criteria criteria = example.createCriteria(); criteria.andNameEqualTo(name); Listusers = userMapper.selectByExample(example); if (users != null && users.size() > 0) { return users.get(0); } return null; } /** * pageNum 开始页数 * pageSize 每页显示的数据条数 */ @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 ListquerUser() { 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中怎么用”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
文章标题:mybatis逆向工程与分页在springboot中怎么用
当前链接:http://myzitong.com/article/ghoscc.html