前言
mybatis自带代码生成插件,我们来试试如何使用
创建空项目
我们通过IDEA的Spring Intializr先创建一个sbm-generator的项目
目录结构
标红部分是代码生成器自动生成的代码,如何生成的请往下看
添加依赖
pom.xml引入代码生成依赖:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.mrcoder
sbm-generator
0.0.1
sbm-generator
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
mysql
mysql-connector-java
runtime
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.3
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
Generate MyBatis Artifacts
generate
org.mybatis.generator
mybatis-generator-core
1.3.2
添加配置
application.yml:
spring:
datasource:
url: jdbc:mysql://192.168.145.131:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
#pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
建库
建立test库,建表:
CREATE TABLE `person` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `person` VALUES ('1', '1', '1');
INSERT INTO `person` VALUES ('2', '2', '2');
INSERT INTO `person` VALUES ('3', '3', '3');
INSERT INTO `person` VALUES ('4', '4', '4');
INSERT INTO `person` VALUES ('5', '5', '5');
INSERT INTO `person` VALUES ('6', '6', '6');
INSERT INTO `person` VALUES ('7', '7', '7');
INSERT INTO `person` VALUES ('8', '8', '8');
INSERT INTO `person` VALUES ('9', '9', '9');
INSERT INTO `person` VALUES ('10', '10', '10');
INSERT INTO `person` VALUES ('11', '11', '11');
mybatis-generator的使用
在pom.xml我们配置了代码生成器配置的目录即
接下来我们就在对应的
/src/main/resources/generator/目录下创建此文件(generator文件夹请手动创建) generatorConfig.xml:
相关配置的含义在代码中均有注释,请根据实际情况修改。
配置完成后,我们可以运行代码生成器mybatis-generator了,直接在IDEA右侧双击mybatis-generator:generate即可
运行成功会生成entity、mapper目录以及resources下的mapper目录。(参照上图目录结构)
完善
想要项目真正跑起来我们还得完善controller和service
我们在项目下右键新增controller和service的package,
service下再新增implpackage,
service/PersonService.java:
package com.mrcoder.sbmgenerator.service;
import com.mrcoder.sbmgenerator.entity.Person;
import java.util.List;
public interface PersonService {
int addPerson(Person person);
List findAllPerson(int pageNum, int pageSize);
}
service/impl/PersonServiceImpl.java:
package com.mrcoder.sbmgenerator.service.impl;
import com.github.pagehelper.PageHelper;
import com.mrcoder.sbmgenerator.entity.Person;
import com.mrcoder.sbmgenerator.mapper.PersonMapper;
import com.mrcoder.sbmgenerator.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service(value = "personService")
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonMapper personMapper;//这里会报错,但是并不会影响
@Override
public int addPerson(Person person) {
return personMapper.insert(person);
}
/*
* 这个方法中用到了我们开头配置依赖的分页插件pagehelper
* pageNum 开始页数
* pageSize 每页显示的数据条数
* */
@Override
public List findAllPerson(int pageNum, int pageSize) {
//将参数传给这个方法就可以实现物理分页了,非常简单。
PageHelper.startPage(pageNum, pageSize);
return personMapper.selectAllPerson();
}
}
mapper/PersonMapper.java:
package com.mrcoder.sbmgenerator.mapper;
import com.mrcoder.sbmgenerator.entity.Person;
import java.util.List;
public interface PersonMapper {
int insert(Person record);
int insertSelective(Person record);
//selectAllPerson是自己新增的查询方法
List selectAllPerson();
}
entity/Person.java:
package com.mrcoder.sbmgenerator.entity;
public class Person {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
controller/PersonController.java:
package com.mrcoder.sbmgenerator.controller;
import com.mrcoder.sbmgenerator.entity.Person;
import com.mrcoder.sbmgenerator.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class PersonController {
@Autowired
private PersonService personService;
@ResponseBody
@RequestMapping(value = "/all/{pageNum}/{pageSize}", produces = {"application/json;charset=UTF-8"})
public Object findAllPerson(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){
return personService.findAllPerson(pageNum,pageSize);
}
}
运行
运行项目,访问
项目地址
https://github.com/MrCoderStack/SpringBootDemo/tree/master/sbm-generator
https://gitee.com/MrCoderStack/SpringBootDemo/tree/master/sbm-generator