栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

个人笔记(Mybatis-Plus)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

个人笔记(Mybatis-Plus)

1.maven依赖包:

        
        
            com.baomidou
            mybatis-plus
            3.3.2
        

2. applicationContext:

    
    
    
    
        

 3.Mybatis-config.xml:




    
        
        
    
    
        
        
    

 4. MyBatis-Plus核心注解(在实体类(entity)中使用)

@TableName //-将实体类与表名映射

@Tableld //-说明对应属性是表的主键

@TableField //-设置属性与列名的对应关系

案例:

@TableName("test") //说明实体对应哪一张表
public class Test {
    @TableId(type = IdType.AUTO)  //主键
    @TableField("id")
    private Integer id;

    @TableField("content")  //如果字段名与属性名相同或者符合驼峰命名转换规则,则TableField可省略
    private String content;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

5. 接口(mapper)设置,注意导入接口BaseMapper:

package com.imooc.reader.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imooc.reader.entity.Test;


public interface TestMapper extends BaseMapper {
    public void insertSample();
}

6.接口对应的xml文件:(案例)





6. BaseMapper接口核心API6 

 案例:

package com.imooc.reader.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.reader.entity.Test;
import com.imooc.reader.mapper.TestMapper;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class) //junit4运行的时候自动初始化IOC容器
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
//上面@ 的两项是Test文件专用的注解
public class MyBatisPlusTest {
    @Resource
    private TestMapper testMapper;

    @org.junit.Test
    public void testInsert(){
        Test test=new Test();
        test.setContent("MyBatis Plus测试1");
        testMapper.insert(test);

    }
    @org.junit.Test
    public void testUpdate(){
        Test test=testMapper.selectById(48);
        test.setContent("wwx");
        testMapper.updateById(test);
    }
    @org.junit.Test
    public void testDelete(){
        testMapper.deleteById(48);
    }
    @org.junit.Test
    public void testSelect(){
//        查询条件构造器
        QueryWrapper queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("id",49);   //id值等于49
        queryWrapper.ge("id",5); //id值大于5
        List list=testMapper.selectList(queryWrapper);
        System.out.println(list.get(0).getContent());
        System.out.println("================================");
    }
}

//选择mysql数据库的表中所有行的数据:

    @GetMapping("/index")
    public ModelAndView showIndex(){
        ModelAndView mav=new ModelAndView("/index");  //只需要文件名字,并不需要后缀,否则ftl文件中无法识别(jsp文件似乎可以)
        List categoryList=categoryService.selectAll();
        mav.addObject("categoryList",categoryList);
        return mav;
    }

//获取分页信息(案例)

impl:

package com.imooc.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.reader.entity.Book;
import com.imooc.reader.mapper.BookMapper;
import com.imooc.reader.service.BookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service("BookService")  //“BookService"与接口名称一致
@Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)  //该类的所有方法不需要开启“事物” ,只读,不改变数据库
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bookMapper;

    public IPage paging(Integer page, Integer rows) {
        Page p=new Page(page,rows);     //分页信息 page:第几页 ; rows:每页多少项
        IPage pageObject=bookMapper.selectPage(p,(new QueryWrapper()));   //new QueryWrapper() 选择条件构造器 ;p:分页信息
        return pageObject ;
    }


}

test:

package com.imooc.reader.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.imooc.reader.entity.Book;
import com.imooc.reader.service.BookService;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;


@RunWith(SpringJUnit4ClassRunner.class) //junit4运行的时候自动初始化IOC容器
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class BookServiceImplTest extends TestCase {
    @Resource
    private BookService bookService;
    @Test
    public void testPaging() {
        IPage pageObject= bookService.paging(3,10);
        List records=pageObject.getRecords();  //获取记录
        for (Book b:records){
            System.out.println(b.getBookId()+":"+b.getBookName());
        }
        System.out.println("总页数"+":"+pageObject.getPages());
        System.out.println("总记录数"+":"+pageObject.getTotal());
        System.out.println("当前页数"+":"+pageObject.getCurrent());
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/978128.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号