一 NoticeDao
package org.fkit.hrm.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.fkit.hrm.dao.provider.NoticeDynaSqlProvider;
import org.fkit.hrm.domain.Notice;
import static org.fkit.hrm.util.common.HrmConstants.NOTICETABLE;
public interface NoticeDao {
// 动态查询
@SelectProvider(type=NoticeDynaSqlProvider.class,method="selectWhitParam")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="CREATE_DATE",property="createDate",javaType=java.util.Date.class),
@Result(column="USER_ID",property="user",
one=@One(select="org.fkit.hrm.dao.UserDao.selectById",
fetchType=FetchType.EAGER))
})
List<Notice> selectByPage(Map<String, Object> params);
@SelectProvider(type=NoticeDynaSqlProvider.class,method="count")
Integer count(Map<String, Object> params);
@Select("select * from "+NOTICETABLE+" where ID = #{id}")
Notice selectById(int id);
// 根据id删除公告
@Delete(" delete from "+NOTICETABLE+" where id = #{id} ")
void deleteById(Integer id);
// 动态插入公告
@SelectProvider(type=NoticeDynaSqlProvider.class,method="insertNotice")
void save(Notice notice);
// 动态修改公告
@SelectProvider(type=NoticeDynaSqlProvider.class,method="updateNotice")
void update(Notice notice);
}
二 NoticeDynaSqlProvider
package org.fkit.hrm.dao.provider;
import java.util.Map;
import org.apache.ibatis.jdbc.SQL;
import org.fkit.hrm.domain.Notice;
import static org.fkit.hrm.util.common.HrmConstants.NOTICETABLE;
public class NoticeDynaSqlProvider {
// 分页动态查询
public String selectWhitParam(Map<String, Object> params){
String sql = new SQL(){
{
SELECT("*");
FROM(NOTICETABLE);
if(params.get("notice") != null){
Notice notice = (Notice)params.get("notice");
if(notice.getTitle() != null && !notice.getTitle().equals("")){
WHERE(" title LIKE CONCAT ('%',#{notice.title},'%') ");
}
if(notice.getContent() != null && !notice.getContent().equals("")){
WHERE(" content LIKE CONCAT ('%',#{notice.content},'%') ");
}
}
}
}.toString();
if(params.get("pageModel") != null){
sql += " limit #{pageModel.firstLimitParam} , #{pageModel.pageSize} ";
}
return sql;
}
// 动态查询总数量
public String count(Map<String, Object> params){
return new SQL(){
{
SELECT("count(*)");
FROM(NOTICETABLE);
if(params.get("notice") != null){
Notice notice = (Notice)params.get("notice");
if(notice.getTitle() != null && !notice.getTitle().equals("")){
WHERE(" title LIKE CONCAT ('%',#{notice.title},'%') ");
}
if(notice.getContent() != null && !notice.getContent().equals("")){
WHERE(" content LIKE CONCAT ('%',#{notice.content},'%') ");
}
}
}
}.toString();
}
// 动态插入
public String insertNotice(Notice notice){
return new SQL(){
{
INSERT_INTO(NOTICETABLE);
if(notice.getTitle() != null && !notice.getTitle().equals("")){
VALUES("title", "#{title}");
}
if(notice.getContent() != null && !notice.getContent().equals("")){
VALUES("content", "#{content}");
}
if(notice.getUser() != null && notice.getUser().getId() != null){
VALUES("user_id", "#{user.id}");
}
}
}.toString();
}
// 动态更新
public String updateNotice(Notice notice){
return new SQL(){
{
UPDATE(NOTICETABLE);
if(notice.getTitle() != null && !notice.getTitle().equals("")){
SET(" title = #{title} ");
}
if(notice.getContent() != null && !notice.getContent().equals("")){
SET(" content = #{content} ");
}
if(notice.getUser() != null && notice.getUser().getId() != null){
SET(" user_id = #{user.id} ");
}
WHERE(" id = #{id} ");
}
}.toString();
}
}