【Mybatis Plus-02】通用Service

【Mybatis Plus-02】通用Service,第1张

mybatis-plus的service需要继承IService,并指定实体类

public interface IUserService extends IService<User> {
   
}

ServiceTests.java

@SpringBootTest
@RunWith(SpringRunner.class)
public class ServiceTests {

    @Resource
    private IUserService userService;

    @Test
    public void C(){
        User user=new User();
        user.setUsername("test");
        user.setPassword("21321qweqwewqe");
        user.setNickName("普通用户");
        user.setAddress("河北");
        user.setEmail("3213dasdsa@3123.com");
        user.setBirthday(new Date());
        user.setAge(10);
        user.setHeight(180.88);
        boolean save = userService.save(user);
        //如果保存成功则返回true,数据库新增一条数据
        System.out.println(save);
    }
}

打印sql

数据库

@SpringBootTest
@RunWith(SpringRunner.class)
public class ServiceTests {

    @Resource
    private IUserService userService;

     @Test
    public void R(){
        User user = userService.getById(1);
        Sygaistem.out.println(user);
    }

打印sql

数据库记录

@SpringBootTest
@RunWith(SpringRunner.class)
public class ServiceTests {

    @Resource
    private IUserService userService;

     @Test
    public void U(){
        User user=new User();
        user.setUsername("test1");
        user.setPassword("123EQwqe");
        user.setNickName("测试用户");
        user.setAddress("山东");
        user.setEmail("21321wqewqe@354.com");
        user.setBirthday(new Date());
        user.setAge(20);
        user.setHeight(176.66);
        //saveOrUpdate更新或保存一条数据
        userService.saveOrUpdate(user);
    }

    @Test
    public void U2(){
        User user=new User();
        user.setId(5);
        user.setAddress("山西");
        //updateById根据id更新一条数据
        userService.updateById(user);
    }

打印sql
saveOrUpdate

数据库记录

updateById

数据库记录

@SpringBootTest
@RunWith(SpringRunner.class)
public class ServiceTests {

    @Resource
    private IUserService userService;

     @Test
    public void D(){
     	//根据id删除一条用户记录
        boolean b = userService.removeById(5);
        System.out.println(b);
    }

打印sql

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/799786.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存