import com.sun.source.tree.UsesTree;
import entity.Student;
import mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestStudent {
private SqlSessionFactory sqlSessionFactory;
@Before
public void init() throws IOException {
// mybatis 配置文件的文件名
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testFindAll() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.findAll();
list.forEach((e)->System.out.println(e));
}
@Test
public void testFindById(){
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.findById(2);
System.out.println(student);
}
@Test
public void testFindBy() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.findBy("王凯",7);
list.forEach((e)->System.out.println(e));
}
@Test
public void testFind() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student stu = new Student();
stu.setPhone("13852615963");
stu.setSname("王凯");
stu.setGid(7);
List<Student> list = mapper.find(stu);
list.forEach((e)->System.out.println(e));
}
@Test
public void testFindByName() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.findByName("凯");
list.forEach((e)->System.out.println(e));
}
@Test
public void testFindByName2() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.findByName("%"+"凯"+"%");
list.forEach((e)->System.out.println(e));
}
@Test
public void testQuery() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.query("","");
list.forEach((e)->System.out.println(e));
}
@Test
public void testUpdate() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
//将sid=2的学生的电话改为13920840987, 地址为 北京市海淀区
Student stu = new Student();
stu.setSid(2);
stu.setPhone("");
stu.setAddress("上海市浦东区");
mapper.update(stu);
System.out.println("修改成功");
session.commit();
List<Student> list = mapper.query("","");
list.forEach((e)->System.out.println(e));
}
@Test
public void testDel() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
//
int[] nums = {9,6,7,10,8};
mapper.delByIds(nums);
session.commit();
List<Student> list = mapper.query("","");
list.forEach((e)->System.out.println(e));
}
@Test
public void test2() throws IOException {
//获得SqlSession
SqlSession session = sqlSessionFactory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.queryWithGrade();
list.forEach((e)->System.out.println(e));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
mybatis01.rar (23个子文件)
mybatis01
pom.xml 1KB
src
test
java
TestStudent.java 5KB
TestGrade.java 1KB
main
resources
mapper
StudentMapper.xml 4KB
GradeMapper.xml 496B
db.properties 164B
mybatis-config.xml 1KB
log4j.properties 347B
java
mapper
GradeMapper.java 201B
StudentMapper.java 1KB
entity
Grade.java 384B
Student.java 575B
target
classes
mapper
StudentMapper.xml 4KB
GradeMapper.xml 496B
StudentMapper.class 871B
GradeMapper.class 206B
db.properties 164B
mybatis-config.xml 1KB
entity
Grade.class 2KB
Student.class 5KB
log4j.properties 347B
test-classes
TestGrade.class 2KB
TestStudent.class 6KB
generated-test-sources
test-annotations
generated-sources
annotations
共 23 条
- 1
资源评论
射手座的程序媛
- 粉丝: 1693
- 资源: 106
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功