在SSM框架中的DAO層就是MyBatis中的Mapper,Mapper分為兩部分:Mapper接口(JAVA文件)和Mapper映射文件(XML文件)。DAO開發(Mapper開發)有兩種方式:原始DAO開發和Mapper代理代碼。今天就來介紹一下這兩種方式。
1、原始DAO開發
原始DAO開發就是不僅需要創建DAO接口,還需要創建其實現類。代碼如下:
DAO接口中的代碼如下:
public interface EmployeeDao { Employee findEmployeeById(int empNo) throws Exception; List<Employee> findEmployeeByName(String name) throws Exception; void insertEmployee(Employee employee) throws Exception; void updateEmployee(Employee employee) throws Exception; void deleteEmployee(int empNo) throws Exception; }
DAO實現類中的代碼如下:
public class EmployeeDaoImpl implements EmployeeDao { private SqlSessionFactory factory; // 通過構造方法注入SqlSessionFactory對象 public EmployeeDaoImpl(SqlSessionFactory factory) throws Exception { this.factory = factory; } @Override public Employee findEmployeeById(int empNo) throws Exception { SqlSession session = factory.openSession(); Employee employee = session.selectOne("test.findEmployeeById", empNo); session.close(); return employee; } @Override public List<Employee> findEmployeeByName(String name) throws Exception { SqlSession session = factory.openSession(); List<Employee> list = session.selectList("test.findEmployeeByName", name); session.close(); return list; } @Override public void insertEmployee(Employee employee) throws Exception { SqlSession session = factory.openSession(); session.insert("test.insertEmployee", employee); session.commit(); session.close(); } @Override public void updateEmployee(Employee employee) throws Exception { SqlSession session = factory.openSession(); session.update("test.updateEmployee", employee); session.commit(); session.close(); } @Override public void deleteEmployee(int empNo) throws Exception { SqlSession session = factory.openSession(); session.delete("test.deleteEmployee", empNo); session.commit(); session.close(); } }
測試類代碼如下:
public class EmployeeDaoImplTest { private SqlSessionFactory factory; // 此方法在執行下面的所有方法之前執行 @Before public void setUp() throws Exception { // 創建SqlSessionFactory // MyBatis配置文件 String resource = "SqlMapConfig.xml"; // 得到配置文件流 InputStream is = Resources.getResourceAsStream(resource); // 創建會話工廠,傳入MyBatis配置文件的信息 factory = new SqlSessionFactoryBuilder().build(is); } @Test public void testFindEmployeeById() throws Exception { // 創建UserDao對象 EmployeeDao dao = new EmployeeDaoImpl(factory); // 調用UserDao對象中的方法 Employee employee = dao.findEmployeeById(7369); System.out.println(employee); } }
從上面的代碼中可以看到,原始DAO開發方法存在以下三個問題:
- DAO接口實現類中有大量的重復代碼,如果把這些代碼提取出來,可以大大減輕程序員的工作量。
- 使用SqlSession的各個方法時都需要將statement的id以硬編碼的方式寫入到JAVA代碼中,不利於維護。
- 在調用SqlSession的各個方法時,即使參數類型錯誤,在編譯階段也不會報錯,不利於程序debug。
針對上面的問題,我們推出Mapper代理方法開發DAO。
2、Mapper代理開發
MyBatis可以自動生成Mapper接口的代理對象(即Mapper實現類),我們可以利用這一點來簡化原始DAO開發的代碼。
在Mapper代理開發DAO時,需要遵循以下四條開發規范:
- Mapper.xml中的namespace是Mapper接口的全路徑地址;
- Mapper接口中的方法名和Mapper.xml文件中statement的id相同;
- Mapper接口中方法的輸入參數類型和Mapper.xml中statement的parameterType相同;
- Mapper接口中方法的返回值類型和Mapper.xml中statement的resultType相同。
以下是代碼:
Mapper.xml中的namespace代碼:
<mapper namespace="com.itgungnir.hellomybatis.mapper.EmployeeMapper"> …… </mapper>
Mapper接口中的代碼:
public interface EmployeeMapper { Employee findEmployeeById(int empNo) throws Exception; List<Employee> findEmployeeByName(String name) throws Exception; void insertEmployee(Employee employee) throws Exception; void updateEmployee(Employee employee) throws Exception; void deleteEmployee(int empNo) throws Exception; }
測試代碼:
public class EmployeeMapperTest { private SqlSessionFactory factory; // 此方法在執行下面的所有方法之前執行 @Before public void setUp() throws Exception { // 創建SqlSessionFactory // MyBatis配置文件 String resource = "SqlMapConfig.xml"; // 得到配置文件流 InputStream is = Resources.getResourceAsStream(resource); // 創建會話工廠,傳入MyBatis配置文件的信息 factory = new SqlSessionFactoryBuilder().build(is); } @Test public void testFindEmployeeById() throws Exception { SqlSession session = factory.openSession(); EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); Employee employee = mapper.findEmployeeById(7369); session.close(); System.out.println(employee); } }
由於Mapper代理方式簡單,又避免了代碼的硬編碼,因此現在通常都使用這種方式。
