public class TestPerson {
private SqlSession session;
private static SqlSessionFactory sqlSessionFactory;
@BeforeClass
public static void testBeforeClass() throws Exception{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Before
public void testBefore() {
try {
session = sqlSessionFactory.openSession();
} catch (Exception e) {
e.printStackTrace();
}
}
@After
public void testAfter() {
session.close();
}
/*
* 查询一个人
*/
@Test
public void testselectOnePerson() {
try {
Person person = session.selectOne("com.jlb.mapper.PersonMapper.selectOnePerson", 1);
System.out.println(person);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 查询所有人
*/
@Test
public void testselectAllPerson() {
try {
List<Person> list = session.selectList("com.jlb.mapper.PersonMapper.selectAllPerson");
for (Object object : list) {
System.out.println(object);
}
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 新增一个人
*/
@Test
public void testinsertOnePerson() {
try {
Person person = new Person();
person.setName("xiaoming");
person.setAge(22);
int i = session.insert("com.jlb.mapper.PersonMapper.insertOnePerson", person);
session.commit();
System.out.println(i);
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 修改一个人
*/
@Test
public void testupdateOnePerson() {
try {
Person person = new Person();
person.setName("老莫");
person.setId(4);
int i = session.insert("com.jlb.mapper.PersonMapper.updateOnePerson", person);
session.commit();
System.out.println(i);
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 修改一个人
*/
@Test
public void testdeleteOnePerson() {
try {
int i = session.delete("com.jlb.mapper.PersonMapper.deleteOnePerson", 3);
System.out.println(i);
session.commit();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|