public void selectByIdNamespaceWay(int id) { SqlSession session = sqlSessionFactory.openSession(); try { User user = (User) session.selectOne("com.frames.mybatis.inter.UserMapper.selectUserByID", id); System.out.println(user.getUserAddress()); System.out.println(user.getUserName()); } finally { session.close(); } }
public void selectByIdInterfaceWay(int id) { SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation = session.getMapper(IUserOperation.class); User user = userOperation.selectUserByID(id); System.out.println(user.getUserAddress()); System.out.println(user.getUserName()); } finally { session.close(); } }
public void getUserList0(String userName) { SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation = session.getMapper(IUserOperation.class); List<User> users = userOperation.selectUsers0(userName); for (User user : users) { System.out.println(user.getId() + ":" + user.getUserName() + ":" + user.getUserAddress()); } } finally { session.close(); } }
public void updateUser(int id, String address) { // 先得到用户,然后修改,提交。 SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation = session.getMapper(IUserOperation.class); User user = userOperation.selectUserByID(id); user.setUserAddress(address); userOperation.updateUser(user); session.commit(); } finally { session.close(); } }
/** 测试增加,增加后,必须提交事务,否则不会写入到数据库. */ public void addUser(User user) { SqlSession session = sqlSessionFactory.openSession(); try { IUserOperation userOperation = session.getMapper(IUserOperation.class); int i = userOperation.addUser(user); System.out.println(i); session.commit(); System.out.println("当前增加的用户 id为:" + user.getId()); } finally { session.close(); } }