-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
docs/spring/csb95ca364-6414-11ef-ba69-acde48001122.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* Copyright 2009-2019 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.jdbc; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.apache.ibatis.jdbc.SelectBuilder.SQL; | ||
import static org.apache.ibatis.jdbc.SelectBuilder.*; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class SelectBuilderTest { | ||
|
||
private static String example1() { | ||
SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME"); | ||
SELECT("P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON"); | ||
FROM("PERSON P"); | ||
FROM("ACCOUNT A"); | ||
INNER_JOIN("DEPARTMENT D on D.ID = P.DEPARTMENT_ID"); | ||
INNER_JOIN("COMPANY C on D.COMPANY_ID = C.ID"); | ||
WHERE("P.ID = A.ID"); | ||
WHERE("P.FIRST_NAME like ?"); | ||
OR(); | ||
WHERE("P.LAST_NAME like ?"); | ||
GROUP_BY("P.ID"); | ||
HAVING("P.LAST_NAME like ?"); | ||
OR(); | ||
HAVING("P.FIRST_NAME like ?"); | ||
ORDER_BY("P.ID"); | ||
ORDER_BY("P.FULL_NAME"); | ||
return SQL(); | ||
} | ||
|
||
private static String example2(String id, String firstName, String lastName) { | ||
SELECT("P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME"); | ||
FROM("PERSON P"); | ||
if (id != null) { | ||
WHERE("P.ID like #id#"); | ||
} | ||
if (firstName != null) { | ||
WHERE("P.FIRST_NAME like #firstName#"); | ||
} | ||
if (lastName != null) { | ||
WHERE("P.LAST_NAME like #lastName#"); | ||
} | ||
ORDER_BY("P.LAST_NAME"); | ||
return SQL(); | ||
} | ||
|
||
@Test | ||
void shouldProduceExpectedSimpleSelectStatement() { | ||
String expected = | ||
"SELECT P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME\n" + | ||
"FROM PERSON P\n" + | ||
"WHERE (P.ID like #id# AND P.FIRST_NAME like #firstName# AND P.LAST_NAME like #lastName#)\n" + | ||
"ORDER BY P.LAST_NAME"; | ||
assertEquals(expected, example2("a", "b", "c")); | ||
} | ||
|
||
@Test | ||
void shouldProduceExpectedSimpleSelectStatementMissingFirstParam() { | ||
String expected = | ||
"SELECT P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME\n" + | ||
"FROM PERSON P\n" + | ||
"WHERE (P.FIRST_NAME like #firstName# AND P.LAST_NAME like #lastName#)\n" + | ||
"ORDER BY P.LAST_NAME"; | ||
assertEquals(expected, example2(null, "b", "c")); | ||
} | ||
|
||
@Test | ||
void shouldProduceExpectedSimpleSelectStatementMissingFirstTwoParams() { | ||
String expected = | ||
"SELECT P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME\n" + | ||
"FROM PERSON P\n" + | ||
"WHERE (P.LAST_NAME like #lastName#)\n" + | ||
"ORDER BY P.LAST_NAME"; | ||
assertEquals(expected, example2(null, null, "c")); | ||
} | ||
|
||
@Test | ||
void shouldProduceExpectedSimpleSelectStatementMissingAllParams() { | ||
String expected = | ||
"SELECT P.ID, P.USERNAME, P.PASSWORD, P.FIRST_NAME, P.LAST_NAME\n" + | ||
"FROM PERSON P\n" + | ||
"ORDER BY P.LAST_NAME"; | ||
assertEquals(expected, example2(null, null, null)); | ||
} | ||
|
||
@Test | ||
void shouldProduceExpectedComplexSelectStatement() { | ||
String expected = | ||
"SELECT P.ID, P.USERNAME, P.PASSWORD, P.FULL_NAME, P.LAST_NAME, P.CREATED_ON, P.UPDATED_ON\n" + | ||
"FROM PERSON P, ACCOUNT A\n" + | ||
"INNER JOIN DEPARTMENT D on D.ID = P.DEPARTMENT_ID\n" + | ||
"INNER JOIN COMPANY C on D.COMPANY_ID = C.ID\n" + | ||
"WHERE (P.ID = A.ID AND P.FIRST_NAME like ?) \n" + | ||
"OR (P.LAST_NAME like ?)\n" + | ||
"GROUP BY P.ID\n" + | ||
"HAVING (P.LAST_NAME like ?) \n" + | ||
"OR (P.FIRST_NAME like ?)\n" + | ||
"ORDER BY P.ID, P.FULL_NAME"; | ||
assertEquals(expected, example1()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.github.huifer.full.shiro.service.impl; | ||
|
||
import com.github.huifer.full.shiro.dao.ShiroPostDao; | ||
import com.github.huifer.full.shiro.entity.ShiroPost; | ||
import com.github.huifer.full.shiro.ex.ServerEx; | ||
import com.github.huifer.full.shiro.model.req.post.PostCreateParam; | ||
import com.github.huifer.full.shiro.service.PostService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.StringUtils; | ||
|
||
@Service | ||
public class PostServiceImpl implements PostService { | ||
|
||
@Autowired | ||
private ShiroPostDao shiroPostDao; | ||
|
||
@Override | ||
public boolean create(PostCreateParam param) { | ||
String name = param.getName(); | ||
if (!StringUtils.hasText(name)) { | ||
throw new ServerEx(""); | ||
} | ||
ShiroPost shiroPost = shiroPostDao.findByName(param.getName()); | ||
if (shiroPost != null) { | ||
throw new ServerEx(""); | ||
} | ||
ShiroPost ins = new ShiroPost(); | ||
ins.setName(param.getName()); | ||
return shiroPostDao.insert(ins) > 0; | ||
} | ||
|
||
@Override | ||
public boolean update(PostCreateParam param, int id) { | ||
ShiroPost up = this.shiroPostDao.selectById(id); | ||
|
||
String name = param.getName(); | ||
if (!StringUtils.hasText(name)) { | ||
throw new ServerEx(""); | ||
} | ||
if (!up.getName().equals(name)) { | ||
|
||
ShiroPost shiroPost = shiroPostDao.findByName(param.getName()); | ||
if (shiroPost != null) { | ||
throw new ServerEx(""); | ||
} | ||
up.setName(param.getName()); | ||
} | ||
|
||
return this.shiroPostDao.updateById(up) > 0; | ||
} | ||
|
||
@Override | ||
public boolean delete(int id) { | ||
return this.shiroPostDao.deleteById(id) > 0; | ||
} | ||
|
||
@Override | ||
public ShiroPost byId(int id) { | ||
return this.shiroPostDao.selectById(id); | ||
} | ||
} |