默认实现的dao
method | param | 说明 | return |
---|---|---|---|
save | obj | 存储 | |
find | class,id | 根据id找对象 | student 代理对象 |
update | obj | 更新对象 | |
delete | obj | 删除 | |
query | class,map,page,orderby | 查询page | page |
queryList | class,map,orderby | 查询list | list |
cqueryount | class,map | 查询行数 | 返回where下的行数 |
nativeQuery | sql | 执行自定义sql | 返回list |
nativeQuery | sql,class | 执行sql,返回list | 返回给定类型的list |
query | hql,map,page | hql返回page | page |
query | hql,map | hql返回list | list |
storedProcedureQuery | name,param | 调用存储过程 | 返回结果集 |
样例
##### Map<String,Object> param 说明
map 是sql 语句where 多个and条件的集合
如果 select * from student where name='zhangsan'
Map map=new HashMap();
map.put("name","zhangsan");
如果 select * from student where name='zhangsan' and group=1
map.put("group",1);
如果 select * from student where name='zhangsan' and group=1 and age>12
map.put("age_gt",12);//这里借鉴了jstl 表达式
其他还有 {“ne”,”gt”,”ge”,”lt”,”le”,”sw”,”ew”,”ic”,”nic”,”null”,”notnull”};
- ne
!=
- gt
>
- ge
>=
- lt
<
- le
<=
- sw
以什么开头 like
- ew
以什么结尾 like
- ic
中间 like
- nic
中间没有 like
- null
is null
- notnull
is not null
关联查询
class Student{
String name;
Integer age;
@ManyToOne
School schol;
setter...
getter...
}
class School{
String name;
Integer id;
@OneToMany(mappedBy="parent",fetch =FetchType.EAGER)
List<Student>students
setter...
getter...
}
现在我们要根据学生姓名为张三和学校name为第一中学 为条件进行查询
Map map=new HashMap();
map.put("name","张三");
map.put("schol.name","第一中学");
注意中间的点 以及是schol 不是school
如果是前端表单提交比如根据时间段进行数据查询 可以设置一个时间选择器的name为date_lt,一个是date_gt 无需自己手写sql spring mvc将参数注入到map中, 直接进行defaultService 调用即可.
Page query(Class clazz, Map param, Page page, String orderBy);
fn ModelAndView queryException(@RequestParam MultiValueMap param ,Page page){
var p = defaultService.query(SystemExceptionEntity,param,page,"id desc");
return new ModelAndView("system/query_exception").addObject("p",p);
}
这里查询异常表, 根据前台的表单参数 设置order by 为id desc