如下爲部分代碼示例(具體見文檔)html
mybatis中的xml示例java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"
>
<mapper namespace=
"com.uplooking.mapper.UserMapper"
>
<resultMap id=
"BaseResultMap"
type=
"com.uplooking.pojo.User"
>
<id column=
"uid"
property=
"uid"
jdbcType=
"INTEGER"
/>
<result column=
"uname"
property=
"uname"
jdbcType=
"VARCHAR"
/>
<result column=
"rname"
property=
"rname"
jdbcType=
"VARCHAR"
/>
<result column=
"pwd"
property=
"pwd"
jdbcType=
"VARCHAR"
/>
<result column=
"position"
property=
"position"
jdbcType=
"VARCHAR"
/>
<result column=
"mobile"
property=
"mobile"
jdbcType=
"VARCHAR"
/>
<result column=
"email"
property=
"email"
jdbcType=
"VARCHAR"
/>
<result column=
"create_time"
property=
"createTime"
jdbcType=
"TIMESTAMP"
/>
<result column=
"usr_flag"
property=
"usrFlag"
jdbcType=
"VARCHAR"
/>
<result column=
"gid"
property=
"gid"
jdbcType=
"INTEGER"
/>
<result column=
"aid"
property=
"aid"
jdbcType=
"INTEGER"
/>
</resultMap>
<sql id=
"Example_Where_Clause"
>
<where >
<foreach collection=
"oredCriteria"
item=
"criteria"
separator=
"or"
>
<
if
test=
"criteria.valid"
>
<trim prefix=
"("
suffix=
")"
prefixOverrides=
"and"
>
<foreach collection=
"criteria.criteria"
item=
"criterion"
>
<choose >
<when test=
"criterion.noValue"
>
and ${criterion.condition}
</when>
<when test=
"criterion.singleValue"
>
and ${criterion.condition} #{criterion.value}
</when>
<when test=
"criterion.betweenValue"
>
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test=
"criterion.listValue"
>
and ${criterion.condition}
<foreach collection=
"criterion.value"
item=
"listItem"
open=
"("
close=
")"
separator=
","
>
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</
if
>
</foreach>
</where>
</sql>
<sql id=
"Update_By_Example_Where_Clause"
>
<where >
<foreach collection=
"example.oredCriteria"
item=
"criteria"
separator=
"or"
>
<
if
test=
"criteria.valid"
>
<trim prefix=
"("
suffix=
")"
prefixOverrides=
"and"
>
<foreach collection=
"criteria.criteria"
item=
"criterion"
>
<choose >
<when test=
"criterion.noValue"
>
and ${criterion.condition}
</when>
<when test=
"criterion.singleValue"
>
and ${criterion.condition} #{criterion.value}
</when>
<when test=
"criterion.betweenValue"
>
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test=
"criterion.listValue"
>
and ${criterion.condition}
<foreach collection=
"criterion.value"
item=
"listItem"
open=
"("
close=
")"
separator=
","
>
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</
if
>
</foreach>
</where>
</sql>
<sql id=
"Base_Column_List"
>
uid, uname, rname, pwd, position, mobile, email, create_time, usr_flag, gid, aid
</sql>
<select id=
"selectByExample"
resultMap=
"BaseResultMap"
parameterType=
"com.uplooking.pojo.UserExample"
>
select
<
if
test=
"distinct"
>
distinct
</
if
>
<include refid=
"Base_Column_List"
/>
from t_user
<
if
test=
"_parameter != null"
>
<include refid=
"Example_Where_Clause"
/>
</
if
>
<
if
test=
"orderByClause != null"
>
order by ${orderByClause}
</
if
>
</select>
<select id=
"selectByPrimaryKey"
resultMap=
"BaseResultMap"
parameterType=
"java.lang.Integer"
>
select
<include refid=
"Base_Column_List"
/>
from t_user
where uid = #{uid,jdbcType=INTEGER}
</select>
<delete id=
"deleteByPrimaryKey"
parameterType=
"java.lang.Integer"
>
delete from t_user
where uid = #{uid,jdbcType=INTEGER}
</delete>
<delete id=
"deleteByExample"
parameterType=
"com.uplooking.pojo.UserExample"
>
delete from t_user
<
if
test=
"_parameter != null"
>
<include refid=
"Example_Where_Clause"
/>
</
if
>
</delete>
<insert id=
"insert"
parameterType=
"com.uplooking.pojo.User"
>
insert into t_user (uid, uname, rname,
pwd, position, mobile,
email, create_time, usr_flag,
gid, aid)
values (#{uid,jdbcType=INTEGER}, #{uname,jdbcType=VARCHAR}, #{rname,jdbcType=VARCHAR},
#{pwd,jdbcType=VARCHAR}, #{position,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{usrFlag,jdbcType=VARCHAR},
#{gid,jdbcType=INTEGER}, #{aid,jdbcType=INTEGER})
</insert>
<insert id=
"insertSelective"
parameterType=
"com.uplooking.pojo.User"
>
insert into t_user
<trim prefix=
"("
suffix=
")"
suffixOverrides=
","
>
<
if
test=
"uid != null"
>
uid,
</
if
>
<
if
test=
"uname != null"
>
uname,
</
if
>
<
if
test=
"rname != null"
>
rname,
</
if
>
<
if
test=
"pwd != null"
>
pwd,
</
if
>
<
if
test=
"position != null"
>
position,
</
if
>
<
if
test=
"mobile != null"
>
mobile,
</
if
>
<
if
test=
"email != null"
>
email,
</
if
>
<
if
test=
"createTime != null"
>
create_time,
</
if
>
<
if
test=
"usrFlag != null"
>
usr_flag,
</
if
>
<
if
test=
"gid != null"
>
gid,
</
if
>
<
if
test=
"aid != null"
>
aid,
</
if
>
</trim>
<trim prefix=
"values ("
suffix=
")"
suffixOverrides=
","
>
<
if
test=
"uid != null"
>
#{uid,jdbcType=INTEGER},
</
if
>
<
if
test=
"uname != null"
>
#{uname,jdbcType=VARCHAR},
</
if
>
<
if
test=
"rname != null"
>
#{rname,jdbcType=VARCHAR},
</
if
>
<
if
test=
"pwd != null"
>
#{pwd,jdbcType=VARCHAR},
</
if
>
<
if
test=
"position != null"
>
#{position,jdbcType=VARCHAR},
</
if
>
<
if
test=
"mobile != null"
>
#{mobile,jdbcType=VARCHAR},
</
if
>
<
if
test=
"email != null"
>
#{email,jdbcType=VARCHAR},
</
if
>
<
if
test=
"createTime != null"
>
#{createTime,jdbcType=TIMESTAMP},
</
if
>
<
if
test=
"usrFlag != null"
>
#{usrFlag,jdbcType=VARCHAR},
</
if
>
<
if
test=
"gid != null"
>
#{gid,jdbcType=INTEGER},
</
if
>
<
if
test=
"aid != null"
>
#{aid,jdbcType=INTEGER},
</
if
>
</trim>
</insert>
<select id=
"countByExample"
parameterType=
"com.uplooking.pojo.UserExample"
resultType=
"java.lang.Integer"
>
select count(*) from t_user
<
if
test=
"_parameter != null"
>
<include refid=
"Example_Where_Clause"
/>
</
if
>
</select>
<update id=
"updateByExampleSelective"
parameterType=
"map"
>
update t_user
<set >
<
if
test=
"record.uid != null"
>
uid = #{record.uid,jdbcType=INTEGER},
</
if
>
<
if
test=
"record.uname != null"
>
uname = #{record.uname,jdbcType=VARCHAR},
</
if
>
<
if
test=
"record.rname != null"
>
rname = #{record.rname,jdbcType=VARCHAR},
</
if
>
<
if
test=
"record.pwd != null"
>
pwd = #{record.pwd,jdbcType=VARCHAR},
</
if
>
<
if
test=
"record.position != null"
>
position = #{record.position,jdbcType=VARCHAR},
</
if
>
<
if
test=
"record.mobile != null"
>
mobile = #{record.mobile,jdbcType=VARCHAR},
</
if
>
<
if
test=
"record.email != null"
>
email = #{record.email,jdbcType=VARCHAR},
</
if
>
<
if
test=
"record.createTime != null"
>
create_time = #{record.createTime,jdbcType=TIMESTAMP},
</
if
>
<
if
test=
"record.usrFlag != null"
>
usr_flag = #{record.usrFlag,jdbcType=VARCHAR},
</
if
>
<
if
test=
"record.gid != null"
>
gid = #{record.gid,jdbcType=INTEGER},
</
if
>
<
if
test=
"record.aid != null"
>
aid = #{record.aid,jdbcType=INTEGER},
</
if
>
</set>
<
if
test=
"_parameter != null"
>
<include refid=
"Update_By_Example_Where_Clause"
/>
</
if
>
</update>
<update id=
"updateByExample"
parameterType=
"map"
>
update t_user
set uid = #{record.uid,jdbcType=INTEGER},
uname = #{record.uname,jdbcType=VARCHAR},
rname = #{record.rname,jdbcType=VARCHAR},
pwd = #{record.pwd,jdbcType=VARCHAR},
position = #{record.position,jdbcType=VARCHAR},
mobile = #{record.mobile,jdbcType=VARCHAR},
email = #{record.email,jdbcType=VARCHAR},
create_time = #{record.createTime,jdbcType=TIMESTAMP},
usr_flag = #{record.usrFlag,jdbcType=VARCHAR},
gid = #{record.gid,jdbcType=INTEGER},
aid = #{record.aid,jdbcType=INTEGER}
<
if
test=
"_parameter != null"
>
<include refid=
"Update_By_Example_Where_Clause"
/>
</
if
>
</update>
<update id=
"updateByPrimaryKeySelective"
parameterType=
"com.uplooking.pojo.User"
>
update t_user
<set >
<
if
test=
"uname != null"
>
uname = #{uname,jdbcType=VARCHAR},
</
if
>
<
if
test=
"rname != null"
>
rname = #{rname,jdbcType=VARCHAR},
</
if
>
<
if
test=
"pwd != null"
>
pwd = #{pwd,jdbcType=VARCHAR},
</
if
>
<
if
test=
"position != null"
>
position = #{position,jdbcType=VARCHAR},
</
if
>
<
if
test=
"mobile != null"
>
mobile = #{mobile,jdbcType=VARCHAR},
</
if
>
<
if
test=
"email != null"
>
email = #{email,jdbcType=VARCHAR},
</
if
>
<
if
test=
"createTime != null"
>
create_time = #{createTime,jdbcType=TIMESTAMP},
</
if
>
<
if
test=
"usrFlag != null"
>
usr_flag = #{usrFlag,jdbcType=VARCHAR},
</
if
>
<
if
test=
"gid != null"
>
gid = #{gid,jdbcType=INTEGER},
</
if
>
<
if
test=
"aid != null"
>
aid = #{aid,jdbcType=INTEGER},
</
if
>
</set>
where uid = #{uid,jdbcType=INTEGER}
</update>
<update id=
"updateByPrimaryKey"
parameterType=
"com.uplooking.pojo.User"
>
update t_user
set uname = #{uname,jdbcType=VARCHAR},
rname = #{rname,jdbcType=VARCHAR},
pwd = #{pwd,jdbcType=VARCHAR},
position = #{position,jdbcType=VARCHAR},
mobile = #{mobile,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=TIMESTAMP},
usr_flag = #{usrFlag,jdbcType=VARCHAR},
gid = #{gid,jdbcType=INTEGER},
aid = #{aid,jdbcType=INTEGER}
where uid = #{uid,jdbcType=INTEGER}
</update>
<!-- 登錄 -->
<select id=
"findByName"
parameterType=
"String"
resultMap=
"BaseResultMap"
>
select * from t_user where uname=#{value}
</select>
<!-- 根據部門查詢 -->
<select id=
"findByGroup"
resultMap=
"BaseResultMap"
>
select * from t_user where gid =#{gid}
<
if
test=
"name!= null and name!='' "
>
and (uname like CONCAT(
'%'
,CONCAT(#{name},
'%'
)) or rname like CONCAT(
'%'
,CONCAT(#{name},
'%'
)))
</
if
>
</select>
</mapper>
|
service層示例sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
@Autowired
private
AttendanceMapper attendanceMapper;
/**
* 用戶打卡
*/
public
String insertAttendance(Attendance attendance2) {
// TODO Auto-generated method stub
return
attendanceMapper.insertSelective(attendance2)>
0
?
"插入成功"
:
"插入失敗"
;
}
/**
* 按條件查詢打卡記錄
*/
public
PageInfo findByPage(
int
pageNum, String check_date, String check_month, String reason,
int
uid) {
List<Attendance> attendances=
new
ArrayList<Attendance>();
if
(!
""
.equals(check_date)&&
""
.equals(check_month)) {
PageHelper.startPage(pageNum,
5
);
attendances=attendanceMapper.findByDateAndReason(check_date,reason,uid);
}
else
if
(
""
.equals(check_date)&&!
""
.equals(check_month)) {
PageHelper.startPage(pageNum,
5
);
attendances=attendanceMapper.findByMonthAndReason(check_month,reason,uid);
}
else
if
(
""
.equals(check_date)&&
""
.equals(check_month)&&!
""
.equals(reason)) {
PageHelper.startPage(pageNum,
5
);
attendances=attendanceMapper.findByReason(reason,uid);
}
else
{
PageHelper.startPage(pageNum,
5
);
attendances=attendanceMapper.findAll(uid);
}
PageInfo<Attendance> pageinfo =
new
PageInfo<Attendance>(attendances);
return
pageinfo;
}
|
Controller層mybatis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
@Controller
public
class
AttendanceController {
@Autowired
private
AttendanceService attendanceService;
/**
* 我的考勤明細
* @param check_date
* @param check_month
* @param reason
* @param req
* @param resp
* @param pageNum
* @return
*/
@RequestMapping
(
"findAttendance"
)
public
String findAttendance(String check_date,String check_month,String reason,
HttpServletRequest req,HttpServletResponse resp,
@RequestParam
(value=
"pageNum"
,defaultValue=
"1"
)
int
pageNum) {
User user = (User) req.getSession().getAttribute(
"user"
);
//月考勤對象
MonthCount m =
new
MonthCount();
if
(
""
.equals(check_month)) {
String today = SimpleDateFormatUtil.smt2.format(
new
Date());
String thisMonth = today.substring(
0
,
7
);
m.setTotalCount(attendanceService.findByMonthAndIdAndType(thisMonth,user.getUid(),
0
)) ;
m.setOnTimeCount(attendanceService.findByMonthAndIdAndType(thisMonth,user.getUid(),
1
));
m.setOutTimeWithoutReason(attendanceService.findByMonthAndIdAndType(thisMonth,user.getUid(),
2
));
m.setOutTimeWithReasonAndWithoutAgree(attendanceService.findByMonthAndIdAndType(thisMonth,user.getUid(),
3
));
m.setOutTimeWithReasonAndWithAgree(attendanceService.findByMonthAndIdAndType(thisMonth,user.getUid(),
4
));
}
else
{
m.setTotalCount(attendanceService.findByMonthAndIdAndType(check_month,user.getUid(),
0
)) ;
m.setOnTimeCount(attendanceService.findByMonthAndIdAndType(check_month,user.getUid(),
1
));
m.setOutTimeWithoutReason(attendanceService.findByMonthAndIdAndType(check_month,user.getUid(),
2
));
m.setOutTimeWithReasonAndWithoutAgree(attendanceService.findByMonthAndIdAndType(check_month,user.getUid(),
3
));
m.setOutTimeWithReasonAndWithAgree(attendanceService.findByMonthAndIdAndType(check_month,user.getUid(),
4
));
}
PageInfo pageInfo= attendanceService.findByPage(pageNum,check_date,check_month,reason,user.getUid());
req.setAttribute(
"monthCount"
, m);
req.setAttribute(
"date"
, check_date);
req.setAttribute(
"month"
, check_month);
req.setAttribute(
"reason"
, reason);
req.setAttribute(
"pageInfo"
, pageInfo);
return
"attendance/attendance-list"
;
}
|
原連接:https://www.cnblogs.com/xiaohuomiao/p/10930800.htmlapp