开篇
dao层是直接和数据库打交道的,我们所熟悉的是面向接口编程即编写接口然后编写不同的实现类。
正文
传统实例
pojo类:
user:
public class User {
//属性名和数据库表的字段对应
private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
}
第一步:编写dao接口
public interface UserDao {
//根据id查询用户信息
public User findUserById(int id) throws Exception;
//删除用户信息
public void deleteUser(int id) throws Exception;
}
第二步:接口实现
public class UserDaoImpl implements UserDao {
// 需要向dao实现类中注入SqlSessionFactory
// 这里通过构造方法注入
private SqlSessionFactory sqlSessionFactory;
public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
@Override
public User findUserById(int id) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = sqlSession.selectOne("test.findUserById", id);
// 释放资源
sqlSession.close();
return user;
}
@Override
public void deleteUser(int id) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行插入操作
sqlSession.delete("test.deleteUser", id);
// 提交事务
sqlSession.commit();
// 释放资源
sqlSession.close();
}
}
第三步:测试
public class UserDaoImplTest {
private SqlSessionFactory sqlSessionFactory;
// 此方法是在执行testFindUserById之前执行
@Before
public void setUp() throws Exception {
// 创建sqlSessionFactory
// mybatis配置文件
String resource = "SqlMapConfig.xml";
// 得到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂,传入mybatis的配置文件信息
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
}
@Test
public void testFindUserById() throws Exception {
// 创建UserDao的对象
UserDao userDao = new UserDaoImpl(sqlSessionFactory);
// 调用UserDao的方法
User user = userDao.findUserById(1);
System.out.println(user);
}
}
传统dao层开发的问题
1、使用sqlsession调用方法时statement的id是写死的
2、调用sqlsession时传入变量,由于sqlsession方法使用的是泛型,即使变量传入的类型不是配置文件中规定的编译阶段不报错。
使用mapper代理的方法开发dao层
第一步:mapper接口,相当于dao层接口
package cn.itcast.mybatis.mapper;
public interface UserMapper{
//根据id查询用户信息
public User findUserById(int id) throws Exception;
//删除用户信息
public void deleteUser(int id) throws Exception;
}
第二步:编写mapper.xml
规则:
1、在mapper.xml中namespace等于mapper接口地址
2、mapper接口中的方法名和mapper.xml中statement的id一致
3、mapper接口中的方法输入参数类型和mapper.xml中statement的parameterType指定的类型一致。
4、mapper接口中的方法返回值类型和mapper.xml中statement的resultType指定的类型一致。
第三步:测试
public class UserMapperTest {
private SqlSessionFactory sqlSessionFactory;
// 此方法是在执行testFindUserById之前执行
@Before
public void setUp() throws Exception {
// 创建sqlSessionFactory
// mybatis配置文件
String resource = "SqlMapConfig.xml";
// 得到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建会话工厂,传入mybatis的配置文件信息
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void testFindUserById() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
//创建UserMapper对象,mybatis自动生成mapper代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//调用userMapper的方法
User user = userMapper.findUserById(1);
System.out.println(user);
}
}
总结
使用mapper代理的方式编写dao层相对传统方法从代码量上来说简单的,但是mapper接口的参数只能有一个,所以如果需要多个参数需要包装pojo类来满足不同的业务需求。