Top > SAStrutsあれこれ > 住所と給料からの従業員検索(動的SQL)

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<html>
<head>
  <title>Tutorial: Employee Search2</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
 
<h2>Tutorial: Employee Search2</h2>
 
<s:form>
  住所キーワードを入力下さい=><html:text property="keyword" size="25" /><br>
  給料はいくら以上ですか=><html:text property="salary" size="25" />
  <input type="submit" name="search" value="検索" />
</s:form>
 
</body>
</html>

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<html>
<head>
<title>Tutorial: Employee List</title>
</head>
<body>
<h1>Tutorial: Employee List</h1>
<table border="1">
<tr style="background-color:pink">
<th>Id</th><th>Name</th><th>JobType</th><th>Salary</th><th>Department</th><th>Address</th>
</tr>
<c:forEach var="e" varStatus="s" items="${employees}">
<tr style="background-color:${s.index %2 == 0 ? 'white' : 'yellow'}">
<td align="right">${f:h(e.id)}</td>
<td>${f:h(e.name)}</td>
<td>${f:h(e.jobType)}</td>
<td align="right"><fmt:formatNumber value="${e.salary}" pattern="#,##0"/></td>
<td>${f:h(e.department.name)}</td>
<td>${f:h(e.address.name)}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Everything is expanded.Everything is shortened.
  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
 
 
 
 
 
 
-
|
|
|
|
|
|
!
 
 
 
 
 
 
 
-
|
!
 
 
 
 
 
 
 
-
|
!
    /**
     * 住所テーブルのname列と従業員テーブルのsalary列から従業員を検索します。
     * @param keyword 住所キーワード
     * @param salary 給料下限
     * @return 結果リスト
     */
    public List<Employee> search2(String keyword, String salary) {
        return select()
        .innerJoin(address(), contains(address().name(), empty2null(keyword)))
        .innerJoin(department())
        .where( ge(salary(), str2int(salary)) )
        .orderBy(asc(id()))
        .getResultList();
    }
    
    /**
     * 渡された文字列が空文字列(長さ=0)ならば、nullを返します。
     * そうでなければ、渡された文字列をそのまま返します。
     * @param value
     * @return
     */
    private static String empty2null(String value) {
        return (value.length() != 0) ? value : null;
    }
    
    /**
     * 渡された文字列が空文字列(長さ=0)ならば、nullを返します。
     * そうでなければ、渡された文字列をIntegerに変換して返します。
     * @param value
     * @return
     */
    private static Integer str2int(String value) {
        return (value.length() != 0) ? Integer.valueOf(value) : null;
    }
Everything is expanded.Everything is shortened.
  1
 
.innerJoin(address(), contains(address().name(), "STREET 4"))
Everything is expanded.Everything is shortened.
  1
  2
 
 
    INNER JOIN ADDRESS T2_
        ON T1_.ADDRESS_ID = T2_.ID AND T2_.NAME LIKE '%STREET 4%'
Everything is expanded.Everything is shortened.
  1
 
.innerJoin(address(), contains(address().name(), null))
Everything is expanded.Everything is shortened.
  1
  2
 
 
    INNER JOIN ADDRESS T2_
        ON T1_.ADDRESS_ID = T2_.ID AND T2_.NAME
Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 
 
 
 
 
 
-
|
|
|
|
|
|
|
|
|
|
-
|
!
!
package tutorial.form;
 
import java.util.List;
import org.seasar.struts.annotation.IntegerType;
import tutorial.entity.Department;
import tutorial.entity.Employee;
 
public class Search2Form {
    
    public String keyword;
    
    @IntegerType
    public String salary;
    
    public List<Employee> employees;
    
    public List<Department> departments;
    
    public void initialize() {
        keyword = "STREET 1";
    }
}
Everything is expanded.Everything is shortened.
  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
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
!
|
|
-
|
|
|
!
!
package tutorial.action;
 
import javax.annotation.Resource;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import tutorial.form.Search2Form;
import tutorial.service.DepartmentService;
import tutorial.service.EmployeeService;
 
public class Search2Action {
  
    @ActionForm
    @Resource
    protected Search2Form search2Form;
    
    @Resource
    protected EmployeeService employeeService;
    
    @Resource
    protected DepartmentService departmentService;
    
    @Execute(validator = false)
    public String index() {
        search2Form.initialize();
        return "index.jsp";
    }
    
    @Execute(validator = false)
    public String search() {
        search2Form.employees = employeeService.search2(search2Form.keyword, search2Form.salary);
        search2Form.departments = departmentService.findAll();
        return "result.jsp";
    }
}

添付ファイル: filet_safe_search2_2.jpg 314件 [詳細] filet_safe_search2_1.jpg 299件 [詳細]

リロード   新規 編集 凍結解除 差分 添付 複製 名前変更   ホーム 一覧 単語検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: 2012-08-08 (水) 23:26:16 (2928d)