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());
}
}



