发表于: 2021-11-28 23:39:27
0 795
今天完成到事,测试通用mapper 和 pagehelp
package com.artist.service.Impl;
import com.artist.entity.Student;
import com.artist.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageRowBounds;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
import static org.junit.Assert.*;
public class StudentServiceImplTest {
private ApplicationContext ac;
@Autowired
private StudentService studentService;
@Before
public void test() {
ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
studentService = (StudentService) ac.getBean("studentService");
}
@Test
public void selectAll() {
List<Student> list = studentService.selectAll();
System.out.println(list);
}
@Test
public void selectOne() {
Student select1 = new Student(null, "赵子", null, null, "北京");
Student selectResult = studentService.selectOne(select1);
System.out.println(selectResult);
}
@Test
public void saveStudent() {
//创建实体类对象要保存到数据库到数据
Student student = new Student(null, "dengKeRong", "男", 3, "湖南");
//执行插入操作
studentService.saveStudent(student);
}
@Test
public void testInsertSelective() {
//创建实体类对象要保存到数据库到数据
Student student = new Student(null, "artist", "男", null, null);
//执行插入操作
studentService.saveStudentSelective(student);
}
@Test
public void updateBYPrimaryKey() {
Student student = new Student(27, "王三", "女", 1, "长沙");
studentService.updateBYPrimaryKey(student);
}
@Test
public void testUpdateByPrimaryKeySelective() {
//创建实体类对象要更新到数据库到数据
Student student = new Student(8, "王三", "女", 1, null);
studentService.updateBYPrimaryKeySelective(student);
}
/* @Test
public void testDeleteByPrimaryKey() {
Integer sid = 30;
studentService.deleteByPrimaryKey(sid);
}*/
@Test
public void testSelectByExample1() {
// criteria01 cariteria02
// 目标:WHERE (sname= ? AND sex =?) OR (sname = ? AND birthday = ?)
// 1.创建Example对象
Example example = new Example(Student.class);
// 2 通过Example 创建Criteria对象
Example.Criteria criteria01 = example.createCriteria();
Example.Criteria criteria02 = example.createCriteria();
// 3 在两个Criteria 对象中分别设置查询条件
//property参数: 实体类到属性名
//value参数:实体类到属性值
}
/*分页查询*/
@Test
public void testSelectByExample() {
PageHelper.startPage(1, 2);
Example example = new Example(Student.class);
Example.Criteria criteria = example.createCriteria();
criteria.toString();
example.orderBy("sid").asc();
List<Student> list = studentService.selectExample(example);
//分页信息对象
PageInfo pageInfo = new PageInfo<Student>(list);
System.out.println("总记录熟:"+pageInfo.getTotal());
System.out.println("总页数:"+pageInfo.getPages());
System.out.println("单前页号:"+pageInfo.getPageNum());
System.out.println("页大小:"+pageInfo.getPageSize());
for (Student student : list) {
System.out.println(student);
}
}
@Test
public void selectStudentById() {
Integer sid=8;
System.out.println(studentService.selectStudentById(sid));
}
}
评论