MybatisPlus

1.建数据库:

CREATE TABLE User
(
  `id` bigint NOT NULL COMMENT '主键ID',
  `NAME` varchar(30) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '姓名',
  `age` int DEFAULT NULL COMMENT '年龄',
  `email` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '邮箱',
  PRIMARY KEY (`id`)
)

2.在pom.xml文件中添加依赖:mybatisplus依赖+mysql依赖+lombok依赖(idea需要安装lombok插件)

lombok可以用来简化实体类的开发,实体类不用再写每一个属性的get,set方法,只需要添加一个注解即可

3.在application.properties文件中配置数据库信息

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

4.Dao包下创建数据表所对应的实体类

@Data
//这个注解来自于lombok依赖,使得我们不用手动写每个属性的get和set方法
public class User
{
    private Long id;
    private String name;
    private  Integer age;
    private  String email;
}

5.Mapper包下创建一个UserMapper接口

@Repository
public interface UserMapper extends  BaseMapper<User>
{


}

这个是最核心的:创建一个UserMapper接口,这个接口继承于BaseMapper

这个接口的实现类的对象为userMapper

通过调用这个对象的方法实现对数据表的增删改查:

userMapper.selectList():对数据表进行查询

userMapper.insert():往数据表中插入一行数据

userMapper.updateById():根据id修改一行数据

6.进行测试:查询数据表中全部数据

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    private UserMapper userMapper;

	@Test
	public void fingAll ()
	{
		List<User> users=userMapper.selectList(null);
		System.out.println(users);
	}
}

查询结果:

如果想看底层的sql语句实现,在application.properties文件中添加配置信息:

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

再次运行,得到:

 如果想要查询其中几行的数据:

	@Test
	public void SelectRows ()
	{
		List<User> users=userMapper.selectBatchIds(Arrays.asList(1,3,5));
		System.out.println(users);
	}

 相当于执行以下sql语句:

SELECT id,name,age,email FROM user WHERE id IN ( 1, 3 , 5 )

 查询结果:



[User(id=1, name=John, age=18, email=test1@baomidou.com, version=null), 
 User(id=3, name=Tom, age=28, email=test3@baomidou.com, version=null), 
 User(id=5, name=Billie, age=24, email=test5@baomidou.com, version=null)]

 以上代码实现了查询数据表中全部数据的功能,接下来依次演示如何使用mybatisplus实现对数据库的增删改查

2.用mybatis-plus实现插入操作

只需修改测试代码即可:

@SpringBootTest
class DemoApplicationTests 
{

    @Autowired
    private UserMapper userMapper;

    @Test
	public void  Add()
	{
		User user=new User();
		user.setName("lucy");
		user.setAge(20);
		user.setEmail("1234@qq.com");
		int num=userMapper.insert(user);
	}
}	

3.用mybatis-plus实现修改操作

只需修改测试代码即可:

@SpringBootTest
class DemoApplicationTests 
{

    @Autowired
    private UserMapper userMapper;

    @Test
	public void Update()
	{
		User user=new User();

		user.setId(6L);

		user.setName("new_Lucy");

		int result=userMapper.updateById(user);
	}

    
}	

4.用mybatis-plus实现条件查询:

只需修改测试代码即可:

    @Autowired
    private UserMapper userMapper;


	@Test
	public void Condition_Select()
	{
		Map map=new HashMap();
		map.put("name","Jack");
		map.put("age",20);

		List users=userMapper.selectByMap(map);
		System.out.println(users);
	}

输出name=jack,age=20的那一行:

[User(id=2, name=Jack, age=20, email=test2@baomidou.com)]

5.用mybatis-plus实现分页查询:

配置分页查询的插件:在Config包下创建一个配置类,里面引入这样的一个插件

@Configuration
@MapperScan("com.example.demo.mapper")
public class MybatisPlusConfig
{
   @Bean
   public PaginationInnerInterceptor paginationInnerInterceptor()
   {
       return new PaginationInnerInterceptor();
   }
}

然后修改测试代码即可:

@SpringBootTest
class DemoApplicationTests
{


    @Autowired
    private UserMapper userMapper;

	@Test
	public void SelectPage()
	{
		Page page=new Page(1,3);//第一个参数表示当前页,第二个参数表示每页记录数
		Page userPage=userMapper.selectPage(page,null);

		//得到总页数
		long pages=userPage.getPages();
		//得到当前在第几页
		long current=userPage.getCurrent();
		//得到表中总记录数
		long total=userPage.getTotal();

		List  records=userPage.getRecords();

		System.out.println(pages);
		System.out.println(current);
		System.out.println(total);
		System.out.println(records);
	}
}

6.用mybatis-plus实现根据id删除某一行

只需修改测试代码即可:

    @Autowired
    private UserMapper userMapper;

	@Test
	public void Delete()
	{
		int row=userMapper.deleteById(1L);
		System.out.println(row);
	}

批量删除:

    @Autowired
    private UserMapper userMapper;

	@Test
	public void Delete()
	{
		int result=userMapper.deleteBatchIds(Arrays.asList(8,9,10));
		System.out.println(result);
	}

条件删除:

    @Autowired
    private UserMapper userMapper;

	@Test
	public void Delete()
	{
		HashMap map=new HashMap();
		map.put("name","Helen");
		map.put("age",18);
		int result=userMapper.deleteByMap(map);
		System.out.println(result);
	}

 

7.利用Wrapper进行复杂查询:

    @Autowired
    private UserMapper userMapper;

	public void testSelect()
	{
		QueryWrapper  queryWrapper=new QueryWrapper();

		queryWrapper.ge("age",21);//年龄大于等于21

		List  users=userMapper.selectList(queryWrapper);
	}
    @Autowired
    private UserMapper userMapper;

	public void testSelect()
	{
		QueryWrapper  queryWrapper=new QueryWrapper();

		queryWrapper.eq("name","Tom");//取出name=Tom的一行

		List  users=userMapper.selectList(queryWrapper);
	}
    @Autowired
    private UserMapper userMapper;

	public void testSelect()
	{
		QueryWrapper  queryWrapper=new QueryWrapper();

		queryWrapper.between("name",24,28);//年龄在24到28这个区间内

		List  users=userMapper.selectList(queryWrapper);
	}

还可以利用Wrapper类进行模糊查询等

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值