在 foodie-dev-pojo 中的 com.imooc.pojo 包下面创建 UserBO 类,从前端传递到后端用于接收的数据体,定义为 xxxBO。

    1. package com.imooc.pojo.bo;
    2. /**
    3. * Created by 92578 on 2020/8/16 13:57
    4. **/
    5. public class UserBO {
    6. private String username;
    7. private String password;
    8. private String confirmPassword;
    9. public String getUsername() {
    10. return username;
    11. }
    12. public void setUsername(String username) {
    13. this.username = username;
    14. }
    15. public String getPassword() {
    16. return password;
    17. }
    18. public void setPassword(String password) {
    19. this.password = password;
    20. }
    21. public String getConfirmPassword() {
    22. return confirmPassword;
    23. }
    24. public void setConfirmPassword(String confirmPassword) {
    25. this.confirmPassword = confirmPassword;
    26. }
    27. }

    在 foodie-dev-service 模块下面 UserService 接口添加新的接口 createUser

    1. package com.imooc.service;
    2. import com.imooc.pojo.Users;
    3. import com.imooc.pojo.bo.UserBO;
    4. /**
    5. * @author 92578
    6. * @since 1.0
    7. */
    8. public interface UserService {
    9. /**
    10. * 判断用户名是否存在
    11. *
    12. * @param username 用户名
    13. * @return 是否存在
    14. */
    15. public boolean queryUsernameIsExist(String username);
    16. /**
    17. * 创建用户
    18. * @param userBO
    19. * @return
    20. */
    21. public Users createUser(UserBO userBO);
    22. }

    在 foodie-dev-common 模块下的 com.imooc.utils 包里创建 MD5Utils 类

    1. package com.imooc.utils;
    2. import org.apache.commons.codec.binary.Base64;
    3. import java.security.MessageDigest;
    4. public class MD5Utils {
    5. /**
    6. * @Title: MD5Utils.java
    7. * @Package com.imooc.utils
    8. * @Description: 对字符串进行md5加密
    9. */
    10. public static String getMD5Str(String strValue) throws Exception {
    11. MessageDigest md5 = MessageDigest.getInstance("MD5");
    12. String newstr = Base64.encodeBase64String(md5.digest(strValue.getBytes()));
    13. return newstr;
    14. }
    15. public static void main(String[] args) {
    16. try {
    17. String md5 = getMD5Str("imooc");
    18. System.out.println(md5);
    19. } catch (Exception e) {
    20. e.printStackTrace();
    21. }
    22. }
    23. }

    在 foodie-dev-common 模块下的 com.imooc.utils 包里创建 DateUtil 类

    1. package com.imooc.utils;
    2. import java.text.*;
    3. import java.util.*;
    4. import org.apache.commons.lang3.StringUtils;
    5. public class DateUtil {
    6. /**
    7. * Base ISO 8601 Date format yyyyMMdd i.e., 20021225 for the 25th day of December in the year 2002
    8. */
    9. public static final String ISO_DATE_FORMAT = "yyyyMMdd";
    10. /**
    11. * Expanded ISO 8601 Date format yyyy-MM-dd i.e., 2002-12-25 for the 25th day of December in the year 2002
    12. */
    13. public static final String ISO_EXPANDED_DATE_FORMAT = "yyyy-MM-dd";
    14. /**
    15. * yyyy-MM-dd hh:mm:ss
    16. */
    17. public static String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    18. public static String DATE_PATTERN = "yyyyMMddHHmmss";
    19. /**
    20. * 则个
    21. */
    22. private static boolean LENIENT_DATE = false;
    23. private static Random random = new Random();
    24. private static final int ID_BYTES = 10;
    25. public synchronized static String generateId() {
    26. StringBuffer result = new StringBuffer();
    27. result = result.append(System.currentTimeMillis());
    28. for (int i = 0; i < ID_BYTES; i++) {
    29. result = result.append(random.nextInt(10));
    30. }
    31. return result.toString();
    32. }
    33. protected static final float normalizedJulian(float JD) {
    34. float f = Math.round(JD + 0.5f) - 0.5f;
    35. return f;
    36. }
    37. /**
    38. * Returns the Date from a julian. The Julian date will be converted to noon GMT,
    39. * such that it matches the nearest half-integer (i.e., a julian date of 1.4 gets
    40. * changed to 1.5, and 0.9 gets changed to 0.5.)
    41. *
    42. * @param JD the Julian date
    43. * @return the Gregorian date
    44. */
    45. public static final Date toDate(float JD) {
    46. /* To convert a Julian Day Number to a Gregorian date, assume that it is for 0 hours, Greenwich time (so
    47. * that it ends in 0.5). Do the following calculations, again dropping the fractional part of all
    48. * multiplicatons and divisions. Note: This method will not give dates accurately on the
    49. * Gregorian Proleptic Calendar, i.e., the calendar you get by extending the Gregorian
    50. * calendar backwards to years earlier than 1582. using the Gregorian leap year
    51. * rules. In particular, the method fails if Y<400. */
    52. float Z = (normalizedJulian(JD)) + 0.5f;
    53. float W = (int) ((Z - 1867216.25f) / 36524.25f);
    54. float X = (int) (W / 4f);
    55. float A = Z + 1 + W - X;
    56. float B = A + 1524;
    57. float C = (int) ((B - 122.1) / 365.25);
    58. float D = (int) (365.25f * C);
    59. float E = (int) ((B - D) / 30.6001);
    60. float F = (int) (30.6001f * E);
    61. int day = (int) (B - D - F);
    62. int month = (int) (E - 1);
    63. if (month > 12) {
    64. month = month - 12;
    65. }
    66. int year = (int) (C - 4715); //(if Month is January or February) or C-4716 (otherwise)
    67. if (month > 2) {
    68. year--;
    69. }
    70. Calendar c = Calendar.getInstance();
    71. c.set(Calendar.YEAR, year);
    72. c.set(Calendar.MONTH, month - 1); // damn 0 offsets
    73. c.set(Calendar.DATE, day);
    74. return c.getTime();
    75. }
    76. /**
    77. * Returns the days between two dates. Positive values indicate that
    78. * the second date is after the first, and negative values indicate, well,
    79. * the opposite. Relying on specific times is problematic.
    80. *
    81. * @param early the "first date"
    82. * @param late the "second date"
    83. * @return the days between the two dates
    84. */
    85. public static final int daysBetween(Date early, Date late) {
    86. Calendar c1 = Calendar.getInstance();
    87. Calendar c2 = Calendar.getInstance();
    88. c1.setTime(early);
    89. c2.setTime(late);
    90. return daysBetween(c1, c2);
    91. }
    92. /**
    93. * Returns the days between two dates. Positive values indicate that
    94. * the second date is after the first, and negative values indicate, well,
    95. * the opposite.
    96. *
    97. * @param early
    98. * @param late
    99. * @return the days between two dates.
    100. */
    101. public static final int daysBetween(Calendar early, Calendar late) {
    102. return (int) (toJulian(late) - toJulian(early));
    103. }
    104. /**
    105. * Return a Julian date based on the input parameter. This is
    106. * based from calculations found at
    107. * <a href="http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html">Julian Day Calculations
    108. * (Gregorian Calendar)</a>, provided by Bill Jeffrys.
    109. *
    110. * @param c a calendar instance
    111. * @return the julian day number
    112. */
    113. public static final float toJulian(Calendar c) {
    114. int Y = c.get(Calendar.YEAR);
    115. int M = c.get(Calendar.MONTH);
    116. int D = c.get(Calendar.DATE);
    117. int A = Y / 100;
    118. int B = A / 4;
    119. int C = 2 - A + B;
    120. float E = (int) (365.25f * (Y + 4716));
    121. float F = (int) (30.6001f * (M + 1));
    122. float JD = C + D + E + F - 1524.5f;
    123. return JD;
    124. }
    125. /**
    126. * Return a Julian date based on the input parameter. This is
    127. * based from calculations found at
    128. * <a href="http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html">Julian Day Calculations
    129. * (Gregorian Calendar)</a>, provided by Bill Jeffrys.
    130. *
    131. * @param date
    132. * @return the julian day number
    133. */
    134. public static final float toJulian(Date date) {
    135. Calendar c = Calendar.getInstance();
    136. c.setTime(date);
    137. return toJulian(c);
    138. }
    139. /**
    140. * @param isoString
    141. * @param fmt
    142. * @param field Calendar.YEAR/Calendar.MONTH/Calendar.DATE
    143. * @param amount
    144. * @return
    145. * @throws ParseException
    146. */
    147. public static final String dateIncrease(String isoString, String fmt,
    148. int field, int amount) {
    149. try {
    150. Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone(
    151. "GMT"));
    152. cal.setTime(stringToDate(isoString, fmt, true));
    153. cal.add(field, amount);
    154. return dateToString(cal.getTime(), fmt);
    155. } catch (Exception ex) {
    156. return null;
    157. }
    158. }
    159. /**
    160. * Time Field Rolling function.
    161. * Rolls (up/down) a single unit of time on the given time field.
    162. *
    163. * @param isoString
    164. * @param field the time field.
    165. * @param up Indicates if rolling up or rolling down the field value.
    166. * @param expanded use formating char's
    167. * @throws ParseException if an unknown field value is given.
    168. */
    169. public static final String roll(String isoString, String fmt, int field,
    170. boolean up) throws ParseException {
    171. Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone(
    172. "GMT"));
    173. cal.setTime(stringToDate(isoString, fmt));
    174. cal.roll(field, up);
    175. return dateToString(cal.getTime(), fmt);
    176. }
    177. /**
    178. * Time Field Rolling function.
    179. * Rolls (up/down) a single unit of time on the given time field.
    180. *
    181. * @param isoString
    182. * @param field the time field.
    183. * @param up Indicates if rolling up or rolling down the field value.
    184. * @throws ParseException if an unknown field value is given.
    185. */
    186. public static final String roll(String isoString, int field, boolean up) throws
    187. ParseException {
    188. return roll(isoString, DATETIME_PATTERN, field, up);
    189. }
    190. /**
    191. * java.util.Date
    192. *
    193. * @param dateText
    194. * @param format
    195. * @param lenient
    196. * @return
    197. */
    198. public static Date stringToDate(String dateText, String format,
    199. boolean lenient) {
    200. if (dateText == null) {
    201. return null;
    202. }
    203. DateFormat df = null;
    204. try {
    205. if (format == null) {
    206. df = new SimpleDateFormat();
    207. } else {
    208. df = new SimpleDateFormat(format);
    209. }
    210. // setLenient avoids allowing dates like 9/32/2001
    211. // which would otherwise parse to 10/2/2001
    212. df.setLenient(false);
    213. return df.parse(dateText);
    214. } catch (ParseException e) {
    215. return null;
    216. }
    217. }
    218. /**
    219. * @return Timestamp
    220. */
    221. public static java.sql.Timestamp getCurrentTimestamp() {
    222. return new java.sql.Timestamp(new Date().getTime());
    223. }
    224. /**
    225. * java.util.Date
    226. *
    227. * @param dateText
    228. * @param format
    229. * @return
    230. */
    231. public static Date stringToDate(String dateString, String format) {
    232. return stringToDate(dateString, format, LENIENT_DATE);
    233. }
    234. /**
    235. * java.util.Date
    236. *
    237. * @param dateText
    238. */
    239. public static Date stringToDate(String dateString) {
    240. return stringToDate(dateString, ISO_EXPANDED_DATE_FORMAT, LENIENT_DATE);
    241. }
    242. /**
    243. * @param pattern
    244. * @param date
    245. * @return
    246. */
    247. public static String dateToString(Date date, String pattern) {
    248. if (date == null) {
    249. return null;
    250. }
    251. try {
    252. SimpleDateFormat sfDate = new SimpleDateFormat(pattern);
    253. sfDate.setLenient(false);
    254. return sfDate.format(date);
    255. } catch (Exception e) {
    256. return null;
    257. }
    258. }
    259. /**
    260. * yyyy-MM-dd
    261. *
    262. * @param date
    263. * @return
    264. */
    265. public static String dateToString(Date date) {
    266. return dateToString(date, ISO_EXPANDED_DATE_FORMAT);
    267. }
    268. /**
    269. * @return
    270. */
    271. public static Date getCurrentDateTime() {
    272. Calendar calNow = Calendar.getInstance();
    273. Date dtNow = calNow.getTime();
    274. return dtNow;
    275. }
    276. /**
    277. * @param pattern
    278. * @return
    279. */
    280. public static String getCurrentDateString(String pattern) {
    281. return dateToString(getCurrentDateTime(), pattern);
    282. }
    283. /**
    284. * yyyy-MM-dd
    285. *
    286. * @return
    287. */
    288. public static String getCurrentDateString() {
    289. return dateToString(getCurrentDateTime(), ISO_EXPANDED_DATE_FORMAT);
    290. }
    291. /**
    292. * 返回固定格式的当前时间
    293. * yyyy-MM-dd hh:mm:ss
    294. *
    295. * @param date
    296. * @return
    297. */
    298. public static String dateToStringWithTime() {
    299. return dateToString(new Date(), DATETIME_PATTERN);
    300. }
    301. /**
    302. * yyyy-MM-dd hh:mm:ss
    303. *
    304. * @param date
    305. * @return
    306. */
    307. public static String dateToStringWithTime(Date date) {
    308. return dateToString(date, DATETIME_PATTERN);
    309. }
    310. /**
    311. * @param date
    312. * @param days
    313. * @return java.util.Date
    314. */
    315. public static Date dateIncreaseByDay(Date date, int days) {
    316. Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone(
    317. "GMT"));
    318. cal.setTime(date);
    319. cal.add(Calendar.DATE, days);
    320. return cal.getTime();
    321. }
    322. /**
    323. * @param date
    324. * @param days
    325. * @return java.util.Date
    326. */
    327. public static Date dateIncreaseByMonth(Date date, int mnt) {
    328. Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone(
    329. "GMT"));
    330. cal.setTime(date);
    331. cal.add(Calendar.MONTH, mnt);
    332. return cal.getTime();
    333. }
    334. /**
    335. * @param date
    336. * @param mnt
    337. * @return java.util.Date
    338. */
    339. public static Date dateIncreaseByYear(Date date, int mnt) {
    340. Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone(
    341. "GMT"));
    342. cal.setTime(date);
    343. cal.add(Calendar.YEAR, mnt);
    344. return cal.getTime();
    345. }
    346. /**
    347. * @param date yyyy-MM-dd
    348. * @param days
    349. * @return yyyy-MM-dd
    350. */
    351. public static String dateIncreaseByDay(String date, int days) {
    352. return dateIncreaseByDay(date, ISO_DATE_FORMAT, days);
    353. }
    354. /**
    355. * @param date
    356. * @param fmt
    357. * @param days
    358. * @return
    359. */
    360. public static String dateIncreaseByDay(String date, String fmt, int days) {
    361. return dateIncrease(date, fmt, Calendar.DATE, days);
    362. }
    363. /**
    364. * @param src
    365. * @param srcfmt
    366. * @param desfmt
    367. * @return
    368. */
    369. public static String stringToString(String src, String srcfmt,
    370. String desfmt) {
    371. return dateToString(stringToDate(src, srcfmt), desfmt);
    372. }
    373. /**
    374. * @param date
    375. * @return string
    376. */
    377. public static String getYear(Date date) {
    378. SimpleDateFormat formater = new SimpleDateFormat(
    379. "yyyy");
    380. String cur_year = formater.format(date);
    381. return cur_year;
    382. }
    383. /**
    384. * @param date
    385. * @return string
    386. */
    387. public static String getMonth(Date date) {
    388. SimpleDateFormat formater = new SimpleDateFormat(
    389. "MM");
    390. String cur_month = formater.format(date);
    391. return cur_month;
    392. }
    393. /**
    394. * @param date
    395. * @return string
    396. */
    397. public static String getDay(Date date) {
    398. SimpleDateFormat formater = new SimpleDateFormat(
    399. "dd");
    400. String cur_day = formater.format(date);
    401. return cur_day;
    402. }
    403. public static int getDayInt(Date date) {
    404. SimpleDateFormat formater = new SimpleDateFormat(
    405. "dd");
    406. String cur_day = formater.format(date);
    407. return Integer.valueOf(cur_day);
    408. }
    409. /**
    410. * @param date
    411. * @return string
    412. */
    413. public static String getHour(Date date) {
    414. SimpleDateFormat formater = new SimpleDateFormat(
    415. "HH");
    416. String cur_day = formater.format(date);
    417. return cur_day;
    418. }
    419. public static int getMinsFromDate(Date dt) {
    420. GregorianCalendar cal = new GregorianCalendar();
    421. cal.setTime(dt);
    422. int hour = cal.get(Calendar.HOUR_OF_DAY);
    423. int min = cal.get(Calendar.MINUTE);
    424. return ((hour * 60) + min);
    425. }
    426. /**
    427. * Function to convert String to Date Object. If invalid input then current or next day date
    428. * is returned (Added by Ali Naqvi on 2006-5-16).
    429. *
    430. * @param str String input in YYYY-MM-DD HH:MM[:SS] format.
    431. * @param isExpiry boolean if set and input string is invalid then next day date is returned
    432. * @return Date
    433. */
    434. public static Date convertToDate(String str, boolean isExpiry) {
    435. SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    436. Date dt = null;
    437. try {
    438. dt = fmt.parse(str);
    439. } catch (ParseException ex) {
    440. Calendar cal = Calendar.getInstance();
    441. if (isExpiry) {
    442. cal.add(Calendar.DAY_OF_MONTH, 1);
    443. cal.set(Calendar.HOUR_OF_DAY, 23);
    444. cal.set(Calendar.MINUTE, 59);
    445. } else {
    446. cal.set(Calendar.HOUR_OF_DAY, 0);
    447. cal.set(Calendar.MINUTE, 0);
    448. }
    449. dt = cal.getTime();
    450. }
    451. return dt;
    452. }
    453. public static Date convertToDate(String str) {
    454. SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm");
    455. Date dt = null;
    456. try {
    457. dt = fmt.parse(str);
    458. } catch (ParseException ex) {
    459. dt = new Date();
    460. }
    461. return dt;
    462. }
    463. public static String dateFromat(Date date, int minute) {
    464. String dateFormat = null;
    465. int year = Integer.parseInt(getYear(date));
    466. int month = Integer.parseInt(getMonth(date));
    467. int day = Integer.parseInt(getDay(date));
    468. int hour = minute / 60;
    469. int min = minute % 60;
    470. dateFormat = String.valueOf(year)
    471. +
    472. (month > 9 ? String.valueOf(month) :
    473. "0" + String.valueOf(month))
    474. +
    475. (day > 9 ? String.valueOf(day) : "0" + String.valueOf(day))
    476. + " "
    477. +
    478. (hour > 9 ? String.valueOf(hour) : "0" + String.valueOf(hour))
    479. +
    480. (min > 9 ? String.valueOf(min) : "0" + String.valueOf(min))
    481. + "00";
    482. return dateFormat;
    483. }
    484. public static String sDateFormat() {
    485. return new SimpleDateFormat(DATE_PATTERN).format(Calendar.getInstance().getTime());
    486. }
    487. /**
    488. * @return
    489. * @Description: 获得本月的第一天日期
    490. * @author leechenxiang
    491. * @date 2017年5月31日 下午1:37:34
    492. */
    493. public static String getFirstDateOfThisMonth() {
    494. SimpleDateFormat format = new SimpleDateFormat(ISO_EXPANDED_DATE_FORMAT);
    495. Calendar calendarFirst = Calendar.getInstance();
    496. calendarFirst = Calendar.getInstance();
    497. calendarFirst.add(Calendar.MONTH, 0);
    498. calendarFirst.set(Calendar.DAY_OF_MONTH, 1);
    499. String firstDate = format.format(calendarFirst.getTime());
    500. return firstDate;
    501. }
    502. /**
    503. * @return
    504. * @Description: 获得本月的最后一天日期
    505. * @author leechenxiang
    506. * @date 2017年5月31日 下午1:37:50
    507. */
    508. public static String getLastDateOfThisMonth() {
    509. SimpleDateFormat format = new SimpleDateFormat(ISO_EXPANDED_DATE_FORMAT);
    510. Calendar calendarLast = Calendar.getInstance();
    511. calendarLast.setTime(new Date());
    512. calendarLast.getActualMaximum(Calendar.DAY_OF_MONTH);
    513. String lastDate = format.format(calendarLast.getTime());
    514. return lastDate;
    515. }
    516. /**
    517. * @Description: 判断字符串日期是否匹配指定的格式化日期
    518. */
    519. public static boolean isValidDate(String strDate, String formatter) {
    520. SimpleDateFormat sdf = null;
    521. ParsePosition pos = new ParsePosition(0);
    522. if (StringUtils.isBlank(strDate) || StringUtils.isBlank(formatter)) {
    523. return false;
    524. }
    525. try {
    526. sdf = new SimpleDateFormat(formatter);
    527. sdf.setLenient(false);
    528. Date date = sdf.parse(strDate, pos);
    529. if (date == null) {
    530. return false;
    531. } else {
    532. if (pos.getIndex() > sdf.format(date).length()) {
    533. return false;
    534. }
    535. return true;
    536. }
    537. } catch (Exception e) {
    538. e.printStackTrace();
    539. return false;
    540. }
    541. }
    542. public static void main(String[] args) {
    543. // String timeDir=DateUtil.dateToString(new Date(),DateUtil.ISO_EXPANDED_DATE_FORMAT);
    544. // System.out.println(timeDir);
    545. boolean flag = DateUtil.isValidDate("1990-10-32", DateUtil.ISO_EXPANDED_DATE_FORMAT);
    546. System.out.println(flag);
    547. }
    548. }

    在 foodie-dev-common 模块下的 com.imooc.enums 包里创建 Sex 枚举

    package com.imooc.enums;
    
    /**
     * 性别枚举
     * Created by 92578 on 2020/8/16 20:38
     **/
    public enum Sex {
        woman(0, "女"),
        man(1, "男"),
        secret(2, "保密");
    
        public final Integer type;
        public final String value;
    
        Sex(Integer type, String value) {
            this.type = type;
            this.value = value;
        }
    }
    

    前往 github 下载 id 生成框架代码,网址如下:https://github.com/bingoohuang/idworker-client
    下载好后将源码放在 foodie-dev-common 模块的 src\main\java 下面
    image.png
    在 foodie-dev-api 模块中的 Application 类中添加包扫描路径,扫描刚刚添加的生成 id 框架源码,默认 Spring Boot 扫描的是 com.imooc 包下面的文件。

    package com.imooc;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import tk.mybatis.spring.annotation.MapperScan;
    
    /**
     * @author 92578
     * @since 1.0
     */
    @SpringBootApplication
    // 扫描 mybatis 通用 mapper 所在的包
    @MapperScan(basePackages = "com.imooc.mapper")
    // 扫描所有包以及相关组件包
    @ComponentScan(basePackages = {"com.imooc","org.n3r.idworker"})
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    完善 foodie-dev-service 模块下面的 UserServiceImpl 类,实现 createUser 方法

    package com.imooc.service.impl;
    
    import com.imooc.enums.Sex;
    import com.imooc.mapper.UsersMapper;
    import com.imooc.pojo.Users;
    import com.imooc.pojo.bo.UserBO;
    import com.imooc.service.UserService;
    import com.imooc.utils.DateUtil;
    import com.imooc.utils.MD5Utils;
    import org.n3r.idworker.Sid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    import tk.mybatis.mapper.entity.Example;
    
    import java.util.Date;
    
    /**
     * @author 92578
     * @since 1.0
     */
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UsersMapper usersMapper;
    
        @Autowired
        private Sid sid;
    
        private static final String USER_FACE = "http://dp.gtimg.cn/discuzpic/0/discuz_x5_gamebbs_qq_com_forum_201306_19_1256219xc797y90heepdbh.jpg/0";
    
        /**
         * 判断用户名是否存在
         *
         * @param username 用户名
         * @return 是否存在
         */
        @Transactional(propagation = Propagation.SUPPORTS)
        @Override
        public boolean queryUsernameIsExist(String username) {
            Example userExample = new Example(Users.class);
            Example.Criteria userCriteria = userExample.createCriteria();
    
            userCriteria.andEqualTo("username", username);
    
            Users result = usersMapper.selectOneByExample(userExample);
    
            return result == null ? false : true;
        }
    
        /**
         * 创建用户
         *
         * @param userBO
         * @return
         */
        @Transactional(propagation = Propagation.REQUIRED)
        @Override
        public Users createUser(UserBO userBO) {
            String userId = sid.nextShort();
            Users user = new Users();
            user.setId(userId);
            user.setUsername(userBO.getUsername());
            try {
                user.setPassword(MD5Utils.getMD5Str(userBO.getPassword()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 默认用户昵称同用户名
            user.setNickname(userBO.getUsername());
            // 默认头像
            user.setFace(USER_FACE);
            // 默认生日
            user.setBirthday(DateUtil.stringToDate("1900-01-01"));
            // 默认性别为 保密
            user.setSex(Sex.secret.type);
            user.setCreatedTime(new Date());
            user.setUpdatedTime(new Date());
    
            usersMapper.insert(user);
    
            return user;
        }
    }