一,介绍
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。
MyBatis 与JPA对比
- jpa是对象与对象之间的映射,而mybatis是对象和结果集的映射。
- jpa移植性比较好,不用关心用什么数据库,因为mybatis自由写sql语句,所以当项目移植的时候还需要对sql进行修改。
- 当需要修改字段的时候mybatis改起来特别费事,而jpa就相对简单。
- 如果用hibernate学习起来比较费时间,而mybatis相对来说比较简单,也可以用springdata,但个人觉得springdata只适合单表。
二,实现过程和示例代码
(1)Maven引包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
(2)配置
- yml配置 src/main/resources/application.yml
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/esystem?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
username: root
password: root
mybatis:
configuration:
map-underscore-to-camel-case: true
useColumnLabel: true
config-location:
classpath: config/mybatis-config.xml
type-aliases-package: com.eceibs.report.entity
- mybatis配置 src/main/resources/config/mybatis-config.xml
<configuration>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
(3)Mapper映射
package com.eceibs.report.mapper;
import com.eceibs.report.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper{
@Select("select * from user where id = #{id}")
public User findId(Integer id);
@Select("select * from user")
public List<User> findAll();
}
(4)各层代码编写
- entity
package com.eceibs.report.entity;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.List;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull(message="企业编号不能为空!")
@Column(length=11)
private Integer companyId;
@NotNull(message="登录名不能为空!")
@Column(length=64)
private String loginName;
@NotNull(message="用户名不能为空!")
@Column(length=64)
private String username;
@NotNull(message="部门ID不能为空!")
@Column(length=64)
private Integer departmentId;
public void setId(Integer id) {
this.id = id;
}
public void setCompanyId(Integer companyId) {
this.companyId = companyId;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public void setUsername(String username) {
this.username = username;
}
public void setDepartmentId(Integer departmentId) {
this.departmentId = departmentId;
}
public Integer getId() {
return id;
}
public Integer getCompanyId() {
return companyId;
}
public String getLoginName() {
return loginName;
}
public String getUsername() {
return username;
}
public Integer getDepartmentId() {
return departmentId;
}
}
- service
package com.eceibs.report.service;
import com.eceibs.report.entity.User;
import com.github.pagehelper.PageInfo;
import java.util.List;
public interface UserService {
public List<User> list();
public User getUserById(Integer id);
public PageInfo<User> findByPage(Integer page,Integer size);
}
- service.impl
package com.eceibs.report.service.impl;
import com.eceibs.report.mapper.UserMapper;
import com.eceibs.report.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.eceibs.report.service.UserService;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> list() {
List<User> list= userMapper.findAll();
return list;//拿到list集合
}
@Override
public User getUserById(Integer id){
User user = userMapper.findId(id);
return user;
}
@Override
public PageInfo<User> findByPage(Integer page ,Integer size){
PageHelper.startPage(page, size);
List<User> userList = userMapper.findAll();
PageInfo<User> pageInfo = new PageInfo<>(userList);
return pageInfo;
}
}
- controller
package com.eceibs.report.controller;
import com.eceibs.report.entity.CommentLesson;
import com.eceibs.report.entity.Department;
import com.eceibs.report.entity.Resources;
import com.eceibs.report.entity.User;
import com.eceibs.report.exception.RRException;
import com.eceibs.report.service.CommentLessonService;
import com.eceibs.report.service.DepartmentService;
import com.eceibs.report.service.ResourcesService;
import com.eceibs.report.service.UserService;
import com.eceibs.report.util.RedisUtil;
import com.eceibs.report.util.Result;
import com.eceibs.report.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class DemoController {
@Resource
private UserService userService;
@RequestMapping("/hello")
public String Demotest(){
return "hello,world";
}
@RequestMapping("/user")
public List<User> getAll() throws Exception{
List<User> userList = userService.list();
return userList;
}
@GetMapping("/userinfo/{id}")
public Result<User> getUserById(@PathVariable("id") Integer id){
User user = userService.getUserById(id);
return ResultUtil.success(user);
}
@GetMapping("/userinfos")
public User getUserByIds(Integer id){
User user = userService.getUserById(id);
return user;
}
}
(5)测试
三,小结
由于mybatis的灵活性,故在开发报表服务比较适合使用该框架。