mybatisではmapper.xml方式で動的sqlを使用しない
7297 ワード
Mapperインタフェースのコード(com.dao.Activity Mapper)
Providerインタフェースのコード(com.dao.provider.Activity Mapper Provider)
データベースddl
@SelectProvider(type=ActivityMapperProvider.class,method="select")
public List<Activity> getActivityAll(Map map) ;
Providerインタフェースのコード(com.dao.provider.Activity Mapper Provider)
public String select(final Map<String,Object> map){
return new SQL(){
{
SELECT("*");
FROM("t_activity");
StringBuffer sb = new StringBuffer();
if(map.get("ac_type") != null){
sb.append(" and ac_type= '"+map.get("ac_type")+"'");
}
if(!sb.toString().equals("")){
WHERE(sb.toString().replace("and", ""));
}
}
}.toString();
}
データベースddl
drop table if exists t_activity;
create table if not exists t_activity(
ac_id int(11) not null primary key auto_increment comment ' id ',
ac_title varchar(20) comment ' ',
ac_content varchar(225) comment ' ',
ac_place varchar(40) comment ' ',
ac_starttime timestamp default '2017-01-01 00:00:00' on update current_timestamp comment ' ',
ac_endtime timestamp default '2017-01-01 00:00:00' on update current_timestamp comment ' ',
ac_host varchar(20) comment ' ',
ac_guest varchar(20) comment ' ',
ac_money decimal(7,2) default 0 comment ' ,0 , ',
ac_limitnum int(10) comment ' ',
ac_fixperson varchar(20) comment ' ',
ac_type tinyint(1) default 0 comment ' ,0 ,1 ',
ac_status tinyint(1) default 0 comment ' ,0 ,1 ,2 ',
ac_applystart timestamp default '2017-01-01 00:00:00' on update current_timestamp comment ' ',
ac_applyend timestamp default '2017-01-01 00:00:00' on update current_timestamp comment ' ',
ac_createtime timestamp default '2017-01-01 00:00:00' on update current_timestamp comment ' ',
ac_modifytime timestamp default '2017-01-01 00:00:00' on update current_timestamp comment ' '
);