SpringBoot+MybatisPlus 增删改查学习第三章 (C#转JAVA)

package com.example.demo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Person;
import com.example.demo.mapper.PersonMapper;
import com.example.demo.service.PersonService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
class Demo1ApplicationTests {

@Test
void contextLoads() {
}

private String customName="5555";
@Autowired
PersonService personService;
@Autowired
PersonMapper personMapper;
@Test
void testAdd(){
QueryWrapper wrapper = new QueryWrapper();
Person person = new Person();
// person.setId(1);
person.setName("小周");
person.setSex("男");
person.setHobby("吃");
person.setAge(18);
personMapper.insert(person);
}
@Test
void testDel(){
personMapper.deleteById(1);
}
@Test
void testQuery(){
Person person = personMapper.selectById(1);
System.out.println(person);
}
@Test
void testQueryByName(){
Person person = personMapper.queryPersonByName("小王");
System.out.println(person);
}
@Test
void testUpdate(){
Person person = personMapper.selectById(2);
System.out.println(person);
// person.setId(2);
person.setName("小王1");
person.setSex("男1");
person.setHobby("喝1");
person.setAge(181);
personMapper.updateById(person);
System.out.println(person);
}
@Test
void testAddList(){
try {
List<Person> list = new ArrayList<>();
Person person = new Person();
// person.setId(11);
person.setName("小王");
person.setSex("男");
person.setHobby("喝");
person.setAge(18);
list.add(person);
Person person2 = new Person();
// person2.setId(12);
person2.setName("小李");
person2.setSex("男");
person2.setHobby("吃");
person2.setAge(18);
list.add(person2);
personService.saveBatch(list);
}catch (Exception ex)
{
System.out.print(ex);
}
}
}
posted @ 2024-04-16 16:05  上位机李工  阅读(20)  评论(0)    收藏  举报