- MyBatis中有两种开发DAO的方法:两种方式程序猿都需要写映射配置文件
(1) 一种是原始的DAO开发方法:程序猿需要写DAO接口和接口实现类
(2) 另一种是Mapper代理的方法:程序猿只需要写mapper接口(mapper接口相当于DAO接口)
- 两种开发DAO的方法中程序猿都需要编写映射配置文件,但是两种方式下的映射配置文件的命名与位置是有所不同的:约定俗称的东西
基于原始DAO开发方法
- 程序猿需要编写DAO接口与实现类
- 需要向DAO实现类中注入SqlSessionFactory,在方法体内通过SqlSessionFactory创建SqlSession对象
根据表结构编写实体类
package com.ycom.mybatis.po;
public class User {
private Integer id;
private String username;
private String birthday;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
编写DAO接口
package com.ycom.mybatis.dao;
import com.ycom.mybatis.po.User;
public interface IUserDAO {
public User findById(int id) throws Exception;
public boolean addUser(User user) throws Exception;
public boolean deleteById(int id) throws Exception;
}
根据DAO接口编写实现类
package com.ycom.mybatis.dao.impl