Пример #1
0
 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();
   }
 }
Пример #2
0
 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();
   }
 }
Пример #3
0
  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();
    }
  }
Пример #4
0
  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();
    }
  }
Пример #5
0
  /** 测试增加,增加后,必须提交事务,否则不会写入到数据库. */
  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();
    }
  }