MybatisPlus-从入门到入土2

中国东北人开发的 所以叫mp.baomidou.com 苞米豆

问题

注意:
//  1.application.yml 怎么写
//  2.实体类 不要在写4类8中了 用包装类  int->integer long->LONG    serializable接口 序列化 
正常来说 都应该实现 序列化接口 
//  3.dao接口 继承 BaseMapper<User> 泛型写实体类
//  测试 在下面的test测试类中  添加@Autowired 测试是否成功运行
 // 增加数据 创建对象  添加 

//  .分页查询  1.创建拦截器  在测试类 创建方法  在new Page<x,x> 调用方法 sout调获取的对象

1.基础环境创建

1.模块创建
ider--->file-->new-->project-->Spring lnitializr--> 
不选择default 默认
-->选择custom定制网址  http://start.springboot.io/
2.配置相关信息
group  群组 --->com.itheima

artifact 名称路径-->mybatis_plus

type 类型选择-->Maven Project

java Version java版本-->8

Package 包-->com.itheima
3.选择技术集
Sql->Mysql driver
  1. 手动添加MyBatisPlus和druid数据源起步依赖

idea欧美的产品 很多中国制造的都没有 只能手动添加

<!--MyBatisPlus-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>
<!--druid数据源-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>
5.添加domain实体类
@Data //注解 简化set get 方法
public class User {
    private Long id;
    private String name;
    private String password;
    private Integer age;
    private String tel;
}
6.设置application.yml的参数
#增加type的时候 记的先在pom导入druid的依赖
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC
    username: root
    password: 320321
    type: com.alibaba.druid.pool.DruidDataSource

  #开启日志  快捷键mylog  
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

2.创建dao和测试类

1.创建dao

mybatisPlus和普通的mybatis区别于 简化的sql的写入 因为继承了baseMapper<实体类> 记得加泛型

@Mapper  //mybatis特有 
public interface UserDao extends BaseMapper<User>{

}
2.创建测试类
@SpringBootTest
class SpringmybatisplusApplicationTests {
@Autowired
private UserDao userDao;
 
//查询所有
@Test
    public void testSelectAll(){
    List<User> userList = userDao.selectList(null);
    System.out.println(userList);
}
 
//增加数据
    @Test
    public void testSave(){
        User user = new User();
        user.setName("哎呦这是小李哦");
       user.setAge(25);
        user.setPassword("320321");
        user.setTel("1008611");
        userDao.insert(user);
 
    }
//修改数据
    @Test
    public void testUpdate(){
        User user = new User();
        user.setId(1L);
        user.setAge(88);
        user.setName("狂铁");
        userDao.updateById(user);
 
 
    }
//根据id查询
    @Test
    public void GetById(){
        User user = userDao.selectById(1L);
        System.out.println(user);
    }
//删除数据
    @Test
    public void DeleteById(){
    userDao.deleteById(123456L);


    }
   

创建分页查询

  1. 先创建拦截器--

@Configuration
public class MybatisPlusConfig {
    //和springmvc拦截器区别
   //spring mvc 是拦截路径 
   //mybatis plus 是自己内部
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //1 创建MybatisPlusInterceptor拦截器对象
        MybatisPlusInterceptor mpInterceptor=new MybatisPlusInterceptor();
        //2 添加分页拦截器
        mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mpInterceptor;
    }
}
2.分页查询
//分页查询
@Test
void testSelectPage(){
    //1 创建IPage分页对象,设置分页参数
    IPage<User> page=new Page<>(1,3);//第一页  3条数据
    //2 执行分页查询
    userDao.selectPage(page,null);
    //3 获取分页结果
    System.out.println("当前页码值:"+page.getCurrent());
    System.out.println("每页显示数:"+page.getSize());
    System.out.println("一共多少总页数:"+page.getPages());
    System.out.println("一共多少总条数:"+page.getTotal());
    System.out.println("当前页数据:"+page.getRecords());
}

附加知识

实体类字段映射和表名映射不一致

@Data
@TableName("tbl_user")  //设置当前类对应的数据库表名称
public class User {
    /*
        id为Long类型,因为数据库中id为bigint类型,
        并且mybatis有自己的一套id生成方案,生成出来的id必须是Long类型
     */
    private Long id;
    private String name;
    @TableField(value = "pwd",select = false) //关闭查看权限 查出值为null
    private String password;
    private Integer age;
    private String tel;
    @TableField(exist = false) //表示online字段不参与CRUD操作
    private Boolean online;
}

id生成策略

注解:@TableId  // 属性注解 定义javabean的主键属性上方(id)
@TableId(type=IdType.xxx)

2.主键生成的策略有5种方式?

1.//@TableId(type=IdType.Auto):使用数据库id自增策略控制id生成
2.//@TableId(type=IdType.NONE):不设置id生成策略
3.//@TableId(type=IdType.INPUT):设置手工输入id
4.// @TableId(type= IdType.ASSIGN_ID):雪花算法生成id,(可兼容数值和字符串)
5.//@TableId(type= IdType.ASSIGN_UUID):以uuid生成算法作为id自增

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值