标签:rom com user url 删除 select avatar sql package
package com.fei.provider;
import org.apache.ibatis.jdbc.SQL;
import com.fei.domain.User;
public class UserProvider {
/**
* 新增用户
*
* @return
*/
public String addUser() {
return new SQL() {
{
INSERT_INTO("USER");
VALUES("account_id, name, token, gmt_create, gmt_modified, avatar_url",
"#{accountId}, #{name}, #{token}, #{gmtCreate}, #{gmtModified}, #{avatarUrl}");
}
}.toString();
}
/**
* 根据id删除用户
*
* @return
*/
public String delUserById() {
return new SQL() {
{
DELETE_FROM("USER");
WHERE("ID = #{id}");
}
}.toString();
}
/**
* 更新用户信息
*
* @param user
* @return
*/
public String updateUser(final User user) {
return new SQL() {
{
UPDATE("USER");
// 条件写法
if (user.getAccountId() != null) {
SET("account_id = #{accountId}");
}
if (user.getName() != null) {
SET("name = #{name}");
}
if (user.getToken() != null) {
SET("token = #{token}");
}
if (user.getGmtCreate() != null) {
SET("gmt_create = #{gmtCreate}");
}
if (user.getGmtModified() != null) {
SET("gmt_modified = #{gmtModified}");
}
if (user.getAvatarUrl() != null) {
SET("avatar_url = #{avatarUrl}");
}
WHERE("ID = #{id}");
}
}.toString();
}
/**
* 根据id或姓名查询用户
*
* @param id
* @param name
* @return
*/
public String selectUserLike(final String id, final String name) {
return new SQL() {
{
SELECT("u.id, u.account_id, u.name, u.token, u.gmt_create, u.gmt_modified");
FROM("USER u");
if (id != null) {
WHERE("u.id like #{id}");
}
if (name != null) {
WHERE("u.name like #{name}");
}
ORDER_BY("u.id");
}
}.toString();
}
// Builder / Fluent style
/**
* 保存用户的第二种写法
*
* @return
*/
public String insertUserSql() {
return new SQL().INSERT_INTO("USER").VALUES("account_id, name, token, gmt_create, gmt_modified",
"#{accountId}, #{name}, #{token}, #{gmtCreate}, #{gmtModified}").toString();
}
}
标签:rom com user url 删除 select avatar sql package
原文地址:https://www.cnblogs.com/zxfei/p/11735495.html