内容:
Spring Boot2 + Mybatis 整合
Mybatis Generator自动生成代码
Mybatis PageHelper分页插件
创建maven项目
修改pom.xml 注意springboot、druid、pageHelper的版本号
4.0.0
com.sid
springboot
1.0-SNAPSHOT
jar
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.apache.commons
commons-lang3
3.4
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
5.1.38
com.alibaba
druid-spring-boot-starter
1.1.9
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.2
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
创建启动类App.java
package com.sid;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.sid.mapper")//将项目中对应的mapper类的路径加进来就可以了
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
创建application.yml
server:
port: 8088
servlet:
context-path: /sid
spring:
mvc:
view:
prefix: /
suffix: .html
datasource:
url: jdbc:mysql://localhost:3306/sid
username: root
password: root
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
## 该配置节点为独立的节点,不是在在spring的节点下
mybatis:
mapper-locations: classpath:mapping/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.sid.model # 注意:对应实体类的路径
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #控制台打印sql
#pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
在resources/generator下创建文件generatorConfig.xml
项目目录
数据库中的表
CREATE TABLE `user` (
`id` bigint(32) NOT NULL COMMENT '用户ID',
`name` varchar(30) NOT NULL COMMENT '用户名',
`password` varchar(30) NOT NULL COMMENT '密码',
`mobile_phone` varchar(20) NOT NULL COMMENT '手机号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
mybatis自动生成代码的插件使用方式
run
然会就会自动生成UserMapper.xml、 User.java 、UserMapper.java
UserController.java
package com.sid.controller;
import com.github.pagehelper.PageInfo;
import com.sid.model.User;
import com.sid.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@RequestMapping(value = "/add")
public int addUser(User user){
return userService.addUser(user);
}
@ResponseBody
@RequestMapping(value = "/delete")
public int deleteUser(String id){
return userService.deleteUser(id);
}
@ResponseBody
@RequestMapping(value = "/update")
public int updateUser(User user){
return userService.updateUser(user);
}
@ResponseBody
@RequestMapping(value = "/selectAll")
public PageInfo selectAll(int pageNum, int pageSize){
return userService.selectAll( pageNum, pageSize);
}
}
UserService.java
package com.sid.service;
import com.github.pagehelper.PageInfo;
import com.sid.model.User;
public interface UserService {
int addUser(User user);
int deleteUser(String id);
int updateUser(User user);
/*
* pageNum 开始页数
* pageSize 每页显示的数据条数
* */
PageInfo selectAll(int pageNum, int pageSize);
}
UserServiceImpl.java
package com.sid.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sid.mapper.UserMapper;
import com.sid.model.User;
import com.sid.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public int addUser(User user) {
return userMapper.insertSelective(user);
}
@Override
public int deleteUser(String id) {
return userMapper.deleteByPrimaryKey(id);
}
@Override
public int updateUser(User user) {
return userMapper.updateByPrimaryKey(user);
}
/*
* pageNum 开始页数
* pageSize 每页显示的数据条数
* */
@Override
public PageInfo selectAll(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize,"id desc"); //开始起始页
List userList = userMapper.selectAll(); // 获取数据
PageInfo page = new PageInfo<>(userList); // 实例化PageInfo
return page;
}
}
UserMapper.java
package com.sid.mapper;
import com.sid.model.User;
import org.springframework.stereotype.Component;
import java.util.List;
@Mapper
public interface UserMapper {
int deleteByPrimaryKey(String id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(String id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
List selectAll();
}
UserMapping.xml
id, name, password, mobile_phone
delete from user
where id = #{id,jdbcType=BIGINT}
insert into user (id, name, password,
mobile_phone)
values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{mobilePhone,jdbcType=VARCHAR})
insert into user
id,
name,
password,
mobile_phone,
#{id,jdbcType=BIGINT},
#{name,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{mobilePhone,jdbcType=VARCHAR},
update user
name = #{name,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
mobile_phone = #{mobilePhone,jdbcType=VARCHAR},
where id = #{id,jdbcType=BIGINT}
update user
set name = #{name,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
mobile_phone = #{mobilePhone,jdbcType=VARCHAR}
where id = #{id,jdbcType=BIGINT}
User.java
package com.sid.model;
public class User {
private Long id;
private String name;
private String password;
private String mobilePhone;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getMobilePhone() {
return mobilePhone;
}
public void setMobilePhone(String mobilePhone) {
this.mobilePhone = mobilePhone == null ? null : mobilePhone.trim();
}
}
添加11条数据到数据库中
查询
结果
打开mybatis执行的SQL打印会在后台看见
还是执行了一次select count(0)