1. 程式人生 > >初學mybatis的理解

初學mybatis的理解

public class TestUserMapper {
private SqlSession session = null;
@Before
public void beforeTestSaveUserBean(){
session = DBTools.getSession();
}








@Test
public void queryPageUserBean(){
IUserMapper um = session.getMapper(IUserMapper.class);
Map<String,Object> params = new HashMap<String, Object>();
params.put("userName", "量");
params.put("sex", "男");
//拼裝分頁物件
PageBean<UserBean> page = new PageBean<UserBean>();
page.setPage(1);
page.setRows(10);

params.put("index", (page.getPage()-1)* page.getRows());
params.put("rows",page.getRows());
int count = um.selectCountUserBeanByCondition(params);
List<UserBean> users = um.selectUserBeanByCondition(params);


page.setList(users);
page.setTotalRows(count);


System.out.println(page);
}



@Ignore
public void testBatchDeleteUserBean(){
IUserMapper um = session.getMapper(IUserMapper.class);
List<Integer> ids = Arrays.asList(14,15,16,17);
int rows = 0;
try {
rows = um.batchDeleteUserBean(ids);
session.commit();
System.out.println(rows);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
session.rollback();
}





}





@Ignore
public void testSelectUserBeanByInfo(){
IUserMapper um = session.getMapper(IUserMapper.class);
UserBean user = um.getUserBeanByNameAndPassword("小黑", "654321");
System.out.println(user);
}


@Ignore
public void testDeleteUserBeanById(){
IUserMapper um = session.getMapper(IUserMapper.class);
int rows = 0;
try {
rows = um.deleteUserBean(2);
session.commit();
} catch (Exception e) {
// TODO: handle exception
session.rollback();
}
Assert.assertEquals(rows,1);
}

@Ignore
public void testUpdateUserBean(){
IUserMapper um = session.getMapper(IUserMapper.class);
int rows = 0;
try {
UserBean user = new UserBean("小黑","654321","女",6000.0);
//執行修改,並返回影響的行數
rows = um.updateUserBean(user, 1);
session.commit();//事務提交
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
session.rollback();
}
Assert.assertEquals(rows, 1);
}

@Ignore
public void testSaveUserBean(){

IUserMapper um = session.getMapper(IUserMapper.class);
UserBean user = new UserBean("小方","123456","男",6000.0);
int rows = 0;
try {
rows = um.saveUserBean(user);
//事務提交
session.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
session.rollback();
}

Assert.assertEquals(rows, 1);

}

@After
public void afterTestSaveUserBean(){
session.close();
}
}