首先扫描实体,通过反射抽取将实体类中的属性抽取出来,再分析操作的表,表中的字段即实体类的属性,最终生成对应的sql语句注入Mybatis容器中,所以mybatis操作的表及表中的字段由实体类及其属性决定。
创建流程:
a>配置application.yml
spring:
#配置数据源信息
datasource:
#配置数据源类型
type: com.zaxxer.hikari.HikariDataSource
#配置连接数据库信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf8&useSSL=false
username: root
password: 123456
注意:
1、驱动类driver-class-name
spring boot 2.0(内置jdbc5驱动),驱动类使用:
driver-class-name: com.mysql.jdbc.Driver
spring boot 2.1及以上(内置jdbc8驱动),驱动类使用:
driver-class-name: com.mysql.cj.jdbc.Driver
否则运行测试用例的时候会有 WARN 信息
2、连接地址url
MySQL5.7版本的url:
jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
MySQL8.0版本的url:
jdbc:mysql://localhost:3306/mybatis_plus?
serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
否则运行测试用例报告如下错误:
java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or
represents more
b>添加实体
@Data //lombok注解
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
User类编译之后的结果:
c>添加mapper
BaseMapper是MyBatis-Plus提供的模板mapper,其中包含了基本的CRUD方法,泛型为操作的
实体类型
public interface UserMapper extends BaseMapper<User> {}
d>启动类
在Spring Boot启动类中添加@MapperScan注解,扫描mapper包
@SpringBootApplication
@MapperScan("com.atguigu.mybatisplus.mapper")
public class MybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusApplication.class, args);
}
}
e>测试
@SpringBootTest
public class MybatisPlusTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelectList(){
//selectList()根据MP内置的条件构造器查询一个list集合,null表示没有条件,即查询所有
userMapper.selectList(null).forEach(System.out::println);
}
}
常用sql方法:
BaseMapper接口:

• 增
@RequestMapping("/add") //增加数据
public Boolean add(int id,int age,String name,String email){
User user= new User();
user.setId(id);
user.setName(name);
user.setAge(age);
user.setEmail(email);
userMapper.insert(user);
return true;
}
• 删
@RequestMapping("/del") //删除
public boolean del(int id){
userMapper.deleteById(id);//根据id删除
Map<String,Object> map=new HashMap<>();
map.put("name","zhangsan");
map.put("age","23");
userMapper.deleteByMap(map);//根据map集合中所设置的条件删除
List<Long> list = Arrays.asList(1L, 2L, 3L);//Arrays中的方法,将传入的数据生成集合
userMapper.deleteBatchIds(list);//通过多个id批量删除
return true;
}
• 改
@RequestMapping("/modify") //修改
public User modify(int id,String name,int age){
User user = userMapper.selectById(id); //查询数据
user.setName(name); //修改数据
user.setAge(age);
userMapper.updateById(user);
User user1=new User();
user1.setId(id);
user1.setName(name);
user1.setAge(age);
userMapper.updateById(user1); //自动以ID为条件进行修改
return user;
}
• 查
@RequestMapping("/search") //查询
public List<User> select(){
User user=userMapper.selectById(1L); //根据ID查询用户信息
List<Long> list1 = Arrays.asList(1L, 2L, 3L);
userMapper.selectBatchIds(list1); //根据多个ID查新多个数据
Map<String,Object> map=new HashMap<>();
map.put("age",20);
List<User> users = userMapper.selectByMap(map);//根据map集合中所设置的条件查询用户信息
users.forEach(System.out::println);
List<User> users1 = userMapper.selectList(null);//查询所有数据
QueryWrapper wrapper = new QueryWrapper();
wrapper.eq("name","zhangsan");//根据条件column==val进行查询
List<User> list = userMapper.selectList(wrapper);
wrapper.ge("age", "18"); //根据条件column>=val进行查询
List<User> list2 = userMapper.selectList(wrapper);
return list;
}
• 自定义功能
在resourse下创建mapper目录,mapper目录下存放自定义映射文件
• 条件构造器:
QueryWrapper 的列名匹配使用的是 “数据库中的字段名(一般是下划线规则)”
LambdaQueryWrapper 的列名匹配使用的是“Lambda的语法,偏向于对象”
LambdaQueryWrapper的优点
不用写“列名”,而是使用纯java的方式,避免了拼写错误(LambdaQueryWrapper的写法如果有错,则在编译期就会报错,而QueryWrapper需要运行的时候调用该方法才会报错)
• 条件构造器常用方法:
QueryWrapper:查询条件的封装
wrapper.eq("数据库字段名", "条件值"); //相当于where条件
wrapper.between("数据库字段名", "区间一", "区间二");//相当于范围内使用的between
wrapper.like("数据库字段名", "模糊查询的字符"); //模糊查询like
wrapper.groupBy("数据库字段名"); //相当于group by分组
wrapper.in("数据库字段名", "包括的值,分割"); //相当于in
wrapper.orderByAsc("数据库字段名"); //升序
wrapper.orderByDesc("数据库字段名");//降序
wrapper.ge("数据库字段名", "要比较的值"); //大于等于
wrapper.le("数据库字段名", "要比较的值"); //小于等于
UpdateWrapper : Update 条件封装
AbstractLambdaWrapper : 使用Lambda 语法
LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper
LambdaUpdateWrapper : Lambda 更新封装Wrapper
IService接口:(假设实体类为User)

service层接口可以继承IService接口,IService的使用(需要另外两个接口baseMapper和ServiceImpl的配合):
①mapper接口继承basemapper接口:
public interface UserMapper extends BaseMapper<User> {
}
②service接口继承Iservice:
public interface IUserService extends IService<User> {
}
③service接口的实现类继承ServiceImpl<继承Basemapper的mapper,实体类>,实现IUserService接口:
@Service
public class IUserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
}
④接下来就可以调用IService中的方法啦




被折叠的 条评论
为什么被折叠?



