mybatis简单案例(含表结构).7z

  • q0_894277
    了解作者
  • 11.6KB
    文件大小
  • 7z
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-13 17:39
    上传日期
mybatis简单案例,含一对多和多对一查询以及增删改查; 数据库是mysql
mybatis简单案例(含表结构).7z
内容介绍
import com.fengxi.dao.StudentMapper; import com.fengxi.dao.TeacherMapper; import com.fengxi.pojo.Student; import com.fengxi.pojo.Teacher; import com.fengxi.utils.MybatisHelper; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.HashMap; import java.util.List; public class MyTest { @Test public void getStudentOneTest(){ SqlSession sqlSession = MybatisHelper.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); Student studentOne = mapper.getStudentOne(1); System.out.println(studentOne); sqlSession.close(); } @Test public void getStudentsTest(){ SqlSession sqlSession = MybatisHelper.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> students=mapper.getStudents(); for (Student student : students) { System.out.println(student); } sqlSession.close(); } @Test public void getTeachersTest(){ SqlSession sqlSession = MybatisHelper.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); List<Teacher> teachers=mapper.getTeachers(); for (Teacher teacher : teachers) { System.out.println(teacher); } sqlSession.close(); } @Test public void addStudentTest(){ SqlSession sqlSession = MybatisHelper.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); HashMap<String, Object> map = new HashMap<String, Object>();//万能Map map.put("id",5); map.put("s_name","测试"); map.put("s_hobby","测试数据"); map.put("tid","1"); int count = mapper.addStudent(map); if (count>=1) System.out.println("插入成功"); else System.out.println("失败"); sqlSession.close(); } @Test public void delStudentTest(){ SqlSession sqlSession = MybatisHelper.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); int count = mapper.delStudent(5); if (count>=2) System.out.println("删除成功"); else System.out.println("失败"); sqlSession.close(); } @Test public void updStudentTest(){ SqlSession sqlSession = MybatisHelper.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); int count = mapper.updStudent(5); if (count>=1) System.out.println("修改成功"); else System.out.println("失败"); sqlSession.close(); } @Test public void getStuAndTeaTest(){ SqlSession sqlSession = MybatisHelper.getSqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> stuAndTea = mapper.getStuAndTea(); for (Student student : stuAndTea) { System.out.println(student); } sqlSession.close(); } @Test public void getTeaAndStuTest(){ SqlSession sqlSession = MybatisHelper.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); List<Teacher> teaAndStu = mapper.getTeaAndStu(); for (Teacher teacher : teaAndStu) { System.out.println(teacher); } sqlSession.close(); } }
评论
    相关推荐