数据库表(student)如下:
使用 mybaits generator插件生成 mapper,service,和实体类
目录结构如下:
StudentMapper中代码
@Repository
public interface StudentMapper extends BaseMapper<Student> {
}
StudentService中代码
public interface StudentService extends IService<Student> {
}
StudentServiceImpl中代码
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
}
编写测试类
@Autowired
private StudentMapper studentMapper;
@Test
void contextLoads() {
//c查询全部用户
//参数是一个wrapper
List student = studentMapper.selectList(null);
student.forEach(System.out::println);
}
//插入信息
@Test
public void testInsert(){
Student student = new Student();
student.setName("jiang");
student.setPassword(("1234j"));
int result = studentMapper.insert(student);
System.out.println(result);
System.out.println(student);
}
//更新信息
@Test
public void testUpdate(){
Student student = new Student();
student.setId(3);
student.setName("123");
int i = studentMapper.updateById(student); //通过条件拼接sql语句
System.out.println(i);
}
//删除信息
@Test
public void testdelete(){
int i=4;
studentMapper.deleteById(i);
//System.out.println(student);
}