MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
现在主流的开源 ORM 框架主要是 Mybatis 和 JPA 这两个开源框架,下面我们就来分别看一下这两个开源框架的优势。
那么又想具有 Mybatis 的的优势, 又想具有 JPA 对象操作的的优势。这个时候,Mybatis Plus 就应运而生了。
创建一张简单的学生表用于数据库操作。
drop table if exists tb_student;
create table tb_student
(
id bigint unsigned auto_increment comment '主键',
username varchar(64) comment '请求号',
address varchar(64) comment '商户号',
create_time datetime comment '创建时间',
update_time datetime comment '修改时间',
primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET utf8;
insert into tb_student(username, address, create_time, update_time) values ('zhangsan', 'xxxxxx', now(), now());
insert into tb_student(username, address, create_time, update_time) values ('lisi', 'yyyyyy', now(), now());
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
cn.carlzone.test
fintech-test
0.0.1-SNAPSHOT
fintech-test
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
com.baomidou
mybatis-plus-boot-starter
3.3.2
org.projectlombok
lombok
mysql
mysql-connector-java
5.1.21
fintech-notice-client
org.springframework.boot
spring-boot-maven-plugin
application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.username=carl
spring.datasource.password=123456
@Data
@Accessors(chain = true)
@TableName("tb_student")
public class Student {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@TableField("username")
private String username;
@TableField("address")
private String address;
@TableField("create_time")
private Date createTime;
@TableField("update_time")
private Date updateTime;
}
@Mapper
public interface StudentMapper extends BaseMapper {
}
public interface StudentDao extends IService {
int deleteStudentById(Long id);
int updateAddressById(String address, Long id);
int saveStudent(Student student);
Student findStudentById(Long id);
List findStudentsByCreateTime(Date crateTime);
List findAllStudent();
}
@Repository(value = "studentDao")
public class StudentDaoImpl extends ServiceImpl implements StudentDao {
@Resource
private StudentMapper mapper;
@Override
public int deleteStudentById(Long id) {
return mapper.deleteById(id);
}
@Override
public int updateAddressById(String address, Long id) {
return mapper.update(new Student().setAddress(address).setUpdateTime(new Date()),
new UpdateWrapper().eq("id", id));
}
@Override
public int saveStudent(Student student) {
return mapper.insert(student);
}
@Override
public Student findStudentById(Long id) {
return mapper.selectById(id);
}
@Override
public List findStudentsByCreateTime(Date crateTime) {
return super.list(new QueryWrapper().le("create_time", crateTime));
}
@Override
public List findAllStudent() {
return super.list();
}
}
@RestController
@RequestMapping("student")
public class StudentController {
@Resource(name = "studentDao")
private StudentDao studentDao;
@RequestMapping("all")
public Map students(){
Map result = new HashMap<>();
result.put("code", 200);
result.put("message", "success");
result.put("data", studentDao.findAllStudent());
return result;
}
@RequestMapping("address")
public Map address(String address, Long id) {
studentDao.updateAddressById(address, id);
Map result = new HashMap<>();
result.put("code", 200);
result.put("message", "success");
return result;
}
}