pom文件
mysql
mysql-connector-java
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
org.mybatis.generator
mybatis-generator-core
1.3.7
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.7
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
mainClass上加上MapperScan注解,用来扫描生成的mapper类(也就是我们常说的dao)
@SpringBootApplication
@MapperScan("head.admin.modules.sys.mapper")
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
application.yml配置如下:
#注意冒号后面的空格不能省略,否则会造成不能识别
spring:
freemarker:
cache: false
suffix: .html
datasource:
url: jdbc:mysql://ip:3306/test?useUnicode=true&characterEncoding=UTF-8
username: xxx
password: xxx
mybatis:
mapper-locations: classpath:mapper/*.xml
logging:
level:
head.admin.modules.sys.mapper: DEBUG
pagehelper:
reasonable: true
support-methods-arguments: true
params: count=countSql
helper-dialect: mysql
generator配置文件
注意在新版本中要设置table中的catalog="head_security"属性,否则会在生成过程中报找不到主键信息的警告。
在generator中插件中添加-e参数,用于打印异常信息。
建表
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`user_Id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`phone` varchar(255) NOT NULL,
PRIMARY KEY (`user_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
执行generator插件的generate生命周期,会生成User.java、UserMapper.java、UserMapper.xml3个文件。
User.java
package head.admin.modules.sys.entity;
public class User {
private Integer userId;
private String userName;
private String password;
private String phone;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
}
UserMapper.java
package head.admin.modules.sys.mapper;
import head.admin.modules.sys.entity.User;
import java.util.List;
public interface UserMapper {
int deleteByPrimaryKey(Integer userId);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer userId);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
List selectUsers();
}
UserMapper.xml
user_Id, user_name, password, phone
delete from t_user
where user_Id = #{userId,jdbcType=INTEGER}
insert into t_user (user_Id, user_name, password,
phone)
values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR})
insert into t_user
user_Id,
user_name,
password,
phone,
#{userId,jdbcType=INTEGER},
#{userName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
update t_user
user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
where user_Id = #{userId,jdbcType=INTEGER}
update t_user
set user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR}
where user_Id = #{userId,jdbcType=INTEGER}
接着编写剩余的service、controller即可。
分页示例代码如下:
@Override
public PageInfo selectUsers(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List users = userMapper.selectUsers();
return new PageInfo<>(users);
}
只要传入pageNum和pageSize,传入List,返回PageInfo即可,免去了自己写分页逻辑的麻烦。返回结果示例: