1.编写dao接口
-
public
interface DAO {
-
-
/**
-
* 保存对象
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object save(String str, Object obj) throws Exception;
-
-
/**
-
* 修改对象
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object update(String str, Object obj) throws Exception;
-
-
/**
-
* 删除对象
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object delete(String str, Object obj) throws Exception;
-
-
/**
-
* 查找对象
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object findForObject(String str, Object obj) throws Exception;
-
-
/**
-
* 查找对象
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object findForList(String str, Object obj) throws Exception;
-
-
/**
-
* 查找对象封装成Map
-
* @param s
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object findForMap(String sql, Object obj, String key , String value) throws Exception;
-
-
}
2.编写dao 实现类
-
public
class DaoSupport implements DAO {
-
-
@Resource(name =
"sqlSessionTemplate")
-
private SqlSessionTemplate sqlSessionTemplate;
-
-
/**
-
* 保存对象
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object save(String str, Object obj) throws Exception {
-
return sqlSessionTemplate.insert(str, obj);
-
}
-
-
/**
-
* 批量更新
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object batchSave(String str, List objs )throws Exception{
-
return sqlSessionTemplate.insert(str, objs);
-
}
-
-
/**
-
* 修改对象
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object update(String str, Object obj) throws Exception {
-
return sqlSessionTemplate.update(str, obj);
-
}
-
-
/**
-
* 批量更新
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public void batchUpdate(String str, List objs )throws Exception{
-
SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
-
//批量执行器
-
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,
false);
-
try{
-
if(objs!=
null){
-
for(
int i=
0,size=objs.size();i
-
sqlSession.update(str, objs.get(i));
-
}
-
sqlSession.flushStatements();
-
sqlSession.commit();
-
sqlSession.clearCache();
-
}
-
}
finally{
-
sqlSession.close();
-
}
-
}
-
-
/**
-
* 批量更新
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object batchDelete(String str, List objs )throws Exception{
-
return sqlSessionTemplate.delete(str, objs);
-
}
-
-
/**
-
* 删除对象
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object delete(String str, Object obj) throws Exception {
-
return sqlSessionTemplate.delete(str, obj);
-
}
-
-
/**
-
* 查找对象
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object findForObject(String str, Object obj) throws Exception {
-
return sqlSessionTemplate.selectOne(str, obj);
-
}
-
-
/**
-
* 查找对象
-
* @param str
-
* @param obj
-
* @return
-
* @throws Exception
-
*/
-
public Object findForList(String str, Object obj) throws Exception {
-
return sqlSessionTemplate.selectList(str, obj);
-
}
-
-
public Object findForMap(String str, Object obj, String key, String value) throws Exception {
-
return sqlSessionTemplate.selectMap(str, obj, key);
-
}
-
-
}
3.那么怎么使用呢?
-
public
class UserService {
-
-
@Resource(name =
"daoSupport")
-
private DaoSupport dao;
-
-
/*
-
*通过id获取数据
-
*/
-
public User getUserAndRoleById(String userid) throws Exception {
-
return (User) dao.findForObject(
"UserMapper.getUserAndRoleById", "font-family: Arial, Helvetica, sans-serif;">userid);
-
}
-
-
}
4. UserMapper.xml中的配置
ROLE_ID,
ROLE_NAME,
RIGHTS,
PARENT_ID,
ADD_QX,
DEL_QX,
EDIT_QX,
CHA_QX
#{ROLE_ID},
#{ROLE_NAME},
#{RIGHTS},
#{PARENT_ID},
#{ADD_QX},
#{DEL_QX},
#{EDIT_QX},
#{CHA_QX}
SYS_ROLE
insert into
(
) values (
)
update
set ROLE_NAME = #{ROLE_NAME}
where ROLE_ID = #{ROLE_ID}
delete from
where ROLE_ID=#{ROLE_ID}
update
set RIGHTS=#{RIGHTS}
where ROLE_ID=#{ROLE_ID}
update
set RIGHTS=#{rights}
where PARENT_ID=#{ROLE_ID}
update
set ADD_QX=#{value}
where ROLE_ID=#{ROLE_ID}
update
set DEL_QX=#{value}
where ROLE_ID=#{ROLE_ID}
update
set EDIT_QX=#{value}
where ROLE_ID=#{ROLE_ID}
update
set CHA_QX=#{value}
where ROLE_ID=#{ROLE_ID}
好了,就这样,是不是觉得很简单,直接释放出dao层,一个通用dao.