美文网首页
java测试类

java测试类

作者: 小郭米 | 来源:发表于2018-10-22 19:00 被阅读19次

public class MybatisFirst { 

   public SqlSessionFactory getSqlSessionFactory() throws IOException{ 

       // mybatis配置文件 

       String resource = "config/SqlMapConfig.xml"; 

       // 得到配置文件流 

       InputStream inputStream = Resources.getResourceAsStream(resource); 

       // 创建会话工厂,传入mybatis的配置文件信息 

       SqlSessionFactory sqlSessionFactory = newSqlSessionFactoryBuilder().build(inputStream); 

       return sqlSessionFactory; 

   } 

   // 根据id查询用户信息,得到一条记录结果 

   @Test 

   public void findPersonByIdTest() throws IOException { 

       // 通过工厂得到SqlSession 

       SqlSession sqlSession = this.getSqlSessionFactory().openSession(); 

       // 通过SqlSession操作数据库 

       // 第一个参数:映射文件中statement的id,等于=namespace+"."+statement的id 

       // 第二个参数:指定和映射文件中所匹配的parameterType类型的参数 

       // sqlSession.selectOne结果 是与映射文件中所匹配的resultType类型的对象 

       // selectOne查询出一条记录 

       Person Person = sqlSession.selectOne("test.findPersonById",1); 

       System.out.println(Person); 

       // 释放资源 

       sqlSession.close(); 

   } 

   // 根据用户名称模糊查询用户列表 

   @Test 

   public void findPersonByNameTest() throws IOException { 

       // 通过工厂得到SqlSession 

       SqlSession sqlSession = this.getSqlSessionFactory().openSession(); 

       // list中的Person和映射文件中resultType所指定的类型一致 

       List list =sqlSession.selectList("test.findPersonByName", "小明"); 

       System.out.println("信息:" + list); 

       sqlSession.close(); 

   } 

   // 添加用户信息 

   @Test 

   public void insertPersonTest() throws IOException { 

       // 通过工厂得到SqlSession 

       SqlSession sqlSession = this.getSqlSessionFactory().openSession(); 

       // 插入用户对象 

       Person Person = new Person(); 

       Person.setPersonname("王小军"); 

       Person.setBirthday(new Date()); 

       Person.setSex("1"); 

       Person.setAddress("河南郑州"); 

       sqlSession.insert("test.insertPerson", Person); 

       // 提交事务 

       sqlSession.commit(); 

       // 获取用户信息主键 

       System.out.println(Person.getId()); 

       // 关闭会话 

       sqlSession.close(); 

   } 

   // 根据id删除 用户信息 

   @Test 

   public void deletePersonTest() throws IOException { 

       // 通过工厂得到SqlSession 

       SqlSession sqlSession = this.getSqlSessionFactory().openSession(); 

       // 传入id删除 用户 

       sqlSession.delete("test.deletePerson", 49); 

       // 提交事务 

       sqlSession.commit(); 

       // 关闭会话 

       sqlSession.close(); 

   } 

   // 更新用户信息 

   @Test 

   public void updatePersonTest() throws IOException { 

       // 通过工厂得到SqlSession 

       SqlSession sqlSession = this.getSqlSessionFactory().openSession(); 

       // 更新用户信息 

       Person Person = new Person(); 

       // 必须设置id 

       Person.setId(41); 

       Person.setPersonname("王大军"); 

       Person.setBirthday(new Date()); 

       Person.setSex("2"); 

       Person.setAddress("河南郑州"); 

       sqlSession.update("test.updatePerson", Person); 

       // 提交事务 

       sqlSession.commit(); 

       // 关闭会话 

       sqlSession.close(); 

   } 

���M��

相关文章

网友评论

      本文标题:java测试类

      本文链接:https://www.haomeiwen.com/subject/ykxnzftx.html