修改 UserBean 的代码
接口:
-
package com.self.mapper;
-
-
import com.self.pojo.User;
-
-
import java.util.List;
-
-
public interface UserMapper {
-
-
public User query(String id);
-
-
public User queryByName(String name);
-
-
public List<User> queryAll();
-
-
public void updateById(User use);
-
-
public void insert(User user);
-
-
public void deleteById(String id);
-
-
}
User:
-
package com.self.pojo;
-
-
-
import org.apache.ibatis.type.Alias;
-
-
-
public class User {
-
-
private String id;
-
private String username;
-
private String password;
-
private String roleId;
-
-
public String getId() {
-
return id;
-
}
-
-
public void setId(String id) {
-
this.id = id;
-
}
-
-
public String getUsername() {
-
return username;
-
}
-
-
public void setUsername(String username) {
-
this.username = username;
-
}
-
-
public String getPassword() {
-
return password;
-
}
-
-
public void setPassword(String password) {
-
this.password = password;
-
}
-
-
public String getRoleId() {
-
return roleId;
-
}
-
-
public void setRoleId(String roleId) {
-
this.roleId = roleId;
-
}
-
-
-
public String toString() {
-
return "User{" +
-
"id='" + id + '\'' +
-
", username='" + username + '\'' +
-
", password='" + password + '\'' +
-
", roleId='" + roleId + '\'' +
-
'}';
-
}
-
}
测试:
-
package com.self.test;
-
-
import com.self.mapper.UserMapper;
-
import com.self.pojo.User;
-
import org.apache.ibatis.io.Resources;
-
import org.apache.ibatis.session.SqlSession;
-
import org.apache.ibatis.session.SqlSessionFactory;
-
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
-
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.util.List;
-
-
public class Test {
-
public static void main(String[] args) throws IOException {
-
-
String resource = "mybatis-config.xml";
-
-
InputStream is = Resources.getResourceAsStream(resource);
-
-
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
-
-
SqlSession session = sqlSessionFactory.openSession();
-
-
UserMapper mapper = session.getMapper(UserMapper.class);
-
-
// User user = new User();
-
// user.setId("1");
-
// user.setUsername("qiweifeng");
-
// user.setPassword("123456");
-
// user.setRoleId("01");
-
//
-
// user.setId("2");
-
// user.setUsername("hejinde");
-
// user.setPassword("123456");
-
// user.setRoleId("02");
-
// mapper.insert(user);
-
// session.commit();
-
//
-
// User use1 = mapper.query("1");
-
// System.out.println(use1);
-
//
-
// User use2 = mapper.queryByName("qiweifeng");
-
// System.out.println(use2);
-
-
// List<User> list = mapper.queryAll();
-
// System.out.println(list);
-
-
User use = new User();
-
use.setId("1");
-
use.setUsername("qwf");
-
use.setPassword("111111");
-
use.setRoleId("3");
-
mapper.updateById(use);
-
session.commit();
-
//
-
// mapper.deleteById("1");
-
// session.commit();
-
-
}
-
-
}