mybatis 基本
今天刚听说iBATIS改名为MyBatis,实际上就是iBATIS 3.0.1。以前都是用hibernate或者JPA,同时听说iBATIS 3与iBATIS 2 相比,变化挺大的,于是今天花了点时间学习了一下,做了个小例子。
?
准备工作:建立相应的数据库,表,添加一条记录用于测试。
?
第一步当然是建立项目,导入jar包,导入mybatis-3.0.1.jar,和mysql的jdbc驱动。
?
第二步 新建一个类User,包含主键,用户名,密码等属性
?
- package?cn.dcr.mybatis.entity; ??
- ??
-
public?class?User?{ ??
-
????private?Long?id; ??
-
????private?String?userName; ??
-
????private?String?password; ??
-
????private?int?age; ??
- ??
-
????public?Long?getId()?{ ??
-
????????return?id; ??
- ????} ??
- ??
-
????public?void?setId(Long?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?int?getAge()?{ ??
-
????????return?age; ??
- ????} ??
- ??
-
????public?void?setAge(int?age)?{ ??
-
????????this.age?=?age; ??
- ????} ??
- ??
- }??
package cn.dcr.mybatis.entity;
public class User {
private Long id;
private String userName;
private String password;
private int age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
pub