S2dao simple Demo)

Source: Internet
Author: User
S2dao Is An orm framework based on seasar2. It is simple to use and has powerful functions. The official website is http://s2dao.seasar.org/. both Chinese and English documents are available.
S2dao is a good choice if you are afraid of hibernate and just looking for a tool to replace jdbc. To some extent, s2dao may be more like ibatis (I have never used ibatis ).

The specific code is basically an interface (equivalent to DAO) corresponding to a javabean. The javabean here is not a pojo and does not have durability. It is just a container used as the return value of the dao method, so it is convenient to implement complex multi-Table search.
The specific method body of the interface can be automatically filled and implemented through the system interceptor.

The following is a detailed introduction to usage. The s2dao version used in this article is 1.0.49.
There are three tables in the DB:

1. Single Table

Example. java is the container class of the Example table (s2dao is often called DTO, although this name is not very appropriate)
1 package com. hg. s2dao. dto;
2
3 import org. seasar. dao. annotation. tiger. Bean;
4
5 @ Bean (table = "EXAMPLE ")
6 public class Example {
7
8 private String id;
9 private String name;
10
11 public String getId (){
12 return id;
13}
14
15 public void setId (String id ){
16 this. id = id;
17}
18
19 public String getName (){
20 return name;
21}
22
23 public void setName (String name ){
24 this. name = name;
25}
26
27 public String toString (){
28 return "id:" + id + "name:" + name;
29}
30
31} use the @ Bean annotation to indicate the DB table name corresponding to the DTO, and the rest are the fields, getter, and setter.
※Note: When performing multi-table search, @ Bean specifies a table.

The next step is DAO, which is easy to understand.
1 package com. hg. s2dao. dao;
2
3 import java. util. List;
4
5 import org. seasar. dao. annotation. tiger. Arguments;
6 import org. seasar. dao. annotation. tiger. Query;
7 import org. seasar. dao. annotation. tiger. S2Dao;
8 import org. seasar. dao. annotation. tiger. SQL;
9
10 import com. hg. s2dao. dto. Example;
11
12 @ S2Dao (bean = Example. class)
13 public interface ExampleDao {
14
15 // C ////////////////////////////////////
16/** insert multi */
17 int insertExamples (java. util. List <Example> exps );
18
19/** insert single */
20 int insertExample (Example exp );
21
22 // R ////////////////////////////////////
23/** search all */
24 List <Example> findAll ();
25
26/** SQL command */
27 @ SQL ("SELECT MAX (E. ID) FROM EXAMPLE E ")
28 String findIDMax ();
29
30/** contains condition */
31 @ Query ("ID =? And name =? ")
32 Example findAllByIdAndName (String id, String name );
33
34/** when multi use or self-define order */
35 @ Arguments ({"id1", "id2 "})
36 @ Query ("ID =/* id2 */or id =/* id1 */")
37 List <Example> findAllById (String id1, String id2 );
38
39/** list param */
40 @ Arguments ({"ids "})
41 @ Query ("id in/* ids */('')")
42 List <Example> findAllByIdList (List <String> ids );
43
44/** param condition */
45 @ Arguments ({"ids "})
46 @ Query ("1! = 0 "//
47 + "/* IF ids! = Null & ids. size ()> 0 */"//
48 + "and id in/* ids */('')"//
49 + "/* END */")
50 List <Example> findAllByIdListAdv (List <String> ids );
51
52 // U ////////////////////////////////////
53/** update */
54 int updateExample (Example exp );
55
56/** object param */
57 @ SQL ("UPDATE EXAMPLE SET NAME =/* exp. name */")
58 int updateExampleName (Example exp );
59
60 // D ////////////////////////////////////
61/** delete */
62 int deleteExample (Example exp );
63
64/** delete by param */
65 @ Query ("ID =? ")
66 int deleteExampleById (String id );
67} @ S2Dao indicates the data undertaking class for retrieval.
Several retrieval functions show that s2dao can basically achieve all types of retrieval.
@ Query is a condition annotation (such as findAllByIdAndName), which is equivalent to the one after where.
When the parameters of the search condition need to be used repeatedly or in reverse order (such as findAllById), add @ Arguments annotations.
@ SQL is a comment (such as findIDMax) on the original and pollution-free SQL code ).
You can set conditions for input parameters (such as findAllByIdListAdv ).

Test procedure:
1 private static void testBase (){
2
3 ExampleDao dao = getComponent (ExampleDao. class );
4
5 // C ///////////////////////////////
6 Example exp = new Example ();
7 exp. setId ("1 ");
8 exp. setName ("");
9 System. out. println (dao. insertExample (exp ));
10 exp = new Example ();
11 exp. setId ("2 ");
12 exp. setName ("B ");
13 System. out. println (dao. insertExample (exp ));
14
15 // R ///////////////////////////////
16 System. out. println (dao. findAll ());
17 System. out. println (dao. findAllByIdAndName ("1", ""));
18 System. out. println (dao. findAllById ("1", "2 "));
19 System. out. println (dao. findIDMax ());
20
21 List <String> ids = new ArrayList <String> ();
22 ids. add ("1 ");
23 ids. add ("2 ");
24 System. out. println (dao. findAllByIdList (ids ));
25 System. out. println (dao. findAllByIdListAdv (null ));
26 System. out. println (dao. findAllByIdListAdv (ids ));
27
28 // U ///////////////////////////////
29 exp = new Example ();
30 exp. setId ("1 ");
31 exp. setName ("c ");
32 System. out. println (dao. updateExample (exp ));
33 exp = new Example ();
34 exp. setName ("haha ");
35 System. out. println (dao. updateExampleName (exp ));
36
37 // D ///////////////////////////////
38 exp = new Example ();
39 exp. setId ("1 ");
40 exp. setName ("");
41 System. out. println (dao. deleteExample (exp ));
42 exp = new Example ();
43 exp. setId ("2 ");
44 System. out. println (dao. deleteExample (exp ));
45}
46
47 public static <T> T getComponent (final Class <T> clazz ){
48 final S2Container container = SingletonS2ContainerFactory. getContainer ();
49 final Object o = container. getComponent (clazz );
50 final T bean = clazz. cast (o );
51 return bean;
52} ※note: before performing the test, you need to initialize the framework, which is actually a line:
SingletonS2ContainerFactory. init ();

Of course, the execution result is OK, And the console LOG will not be pasted. paste the generated DB LOG, which corresponds to the sequence in which the dao function is called in the test code:
1 insert into example (id, name) VALUES ('1', 'A ')
2 insert into example (id, name) VALUES ('2', 'B ')
3 select example. id, EXAMPLE. name FROM EXAMPLE
4 select example. id, EXAMPLE. name from example where id = '1' and name = 'A'
5 select example. id, EXAMPLE. name from example where id = '2' or id = '1'
6 select max (E. ID) FROM EXAMPLE E
7 select example. id, EXAMPLE. name from example where id in ('1', '2 ')
8 select example. id, EXAMPLE. name from example where 1! = 0
9 select example. id, EXAMPLE. name from example where 1! = 0 and id in ('1', '2 ')
10 update example set name = 'C' WHERE id = '1'
11 update example set name = 'hahaha'
12 delete from example where id = '1'
13 delete from example where id = '2'
2. Multiple tables

1 package com. hg. s2dao. dto;
2
3 import org. seasar. dao. annotation. tiger. Bean;
4
5 @ Bean (table = "FATHER ")
6 public class Father {
7
8 private String id;
9 private String name;
10
11 public String getId (){
12 return id;
13}
14
15 public void setId (String id ){
16 this. id = id;
17}
18
19 public String getName (){
20 return name;
21}
22
23 public void setName (String name ){
24 this. name = name;
25}
26
27}
1 package com. hg. s2dao. dto;
2
3 import org. seasar. dao. annotation. tiger. Bean;
4 import org. seasar. dao. annotation. tiger. Relation;
5
6 @ Bean (table = "CHILD ")
7 public class Child {
8
9 private String id;
10 private String name;
11 private Father father;
12/* s2dao do not support cascade CUD but only R, so need field 'fid '*/
13 private String fId;
14
15 public String getId (){
16 return id;
17}
18
19 public void setId (String id ){
20 this. id = id;
21}
22
23 public String getName (){
24 return name;
25}
26
27 public void setName (String name ){
28 this. name = name;
29}
30
31 public String getFId (){
32 return fId;
33}
34
35 public void setFId (String id ){
36 fId = id;
37}
38
39 @ Relation (relationNo = 0, relationKey = "F_ID: ID ")
40 public Father getFather (){
41 return father;
42}
43
44/* cannot del this method */
45 public void setFather (Father father ){
46 this. father = father;
47}
48
49} There is a one-to-many/one association between Father and Child. Child maintains the relationship with Father.
You may be wondering why the fId field exists? Isn't there a Father class? Unfortunately, s2dao currently does not support cascading insertion, update, and deletion (CUD), and only supports cascading retrieval. FId exists for CUD.
The getFather method in Child. java implements cascading search, and @ Relation specifies its relationship. RelationNo is the sequence number associated with the specified foreign key. The relationKey is declared as the primary foreign key name.

1 package com. hg. s2dao. dao;
2
3 import org. seasar. dao. annotation. tiger. S2Dao;
4
5 import com. hg. s2dao. dto. Father;
6
7 @ S2Dao (bean = Father. class)
8 public interface FatherDao {
9
10/** insert single */
11 int insert (Father f );
12
13/** delete */
14 int delete (Father c );
15}
1 package com. hg. s2dao. dao;
2
3 import org. seasar. dao. annotation. tiger. Query;
4 import org. seasar. dao. annotation. tiger. S2Dao;
5
6 import com. hg. s2dao. dto. Child;
7
8 @ S2Dao (bean = Child. class)
9 public interface ChildDao {
10
11/** insert single */
12 int insert (Child c );
13
14/** contains condition */
15 // cannnot only write: @ Query ("ID =? ")
16 @ Query ("CHILD. ID =? ")
17 Child findAllById (String id );
18
19/** update */
20 int update (Child c );
21
22/** delete */
23 int delete (Child c );
24
25}
Test procedure:
1 private static void testRelation (){
2
3 FatherDao daoF = getComponent (FatherDao. class );
4 ChildDao daoC = getComponent (ChildDao. class );
5
6 // C ///////////////////////////////
7 Father popeye = new Father ();
8 popeye. setId ("1 ");
9 popeye. setName ("Popeye ");
10 System. out. println (daoF. insert (popeye ));
11
12 Child pipeye = new Child ();
13 pipeye. setId ("1 ");
14 pipeye. setName ("Pipeye ");
15 pipeye. setFId (popeye. getId ());
16 System. out. println (daoC. insert (pipeye ));
17
18 Child pupeye = new Child ();
19 pupeye. setId ("2 ");
20 pupeye. setName ("Pupeye ");
21 pupeye. setFId (popeye. getId ());
22 System. out. println (daoC. insert (pupeye ));
23
24 // R ///////////////////////////////
25 // pipeye. getFather () is null, so need search again
26 Child pipeyeNew = daoC. findAllById (pipeye. getId ());
27 System. out. println (pipeyeNew. getFather ());
28
29 // U ///////////////////////////////
30 pipeyeNew. setName ("pipeyeNew ");
31 System. out. println (daoC. update (pipeyeNew ));
32
33 // D ///////////////////////////////
34 System. out. println (daoC. delete (pipeye ));
35 System. out. println (daoC. delete (pupeye ));
36 System. out. println (daoF. delete (popeye ));
37}
Execution result db log:
1 insert into father (id, name) VALUES ('1', 'popeye ')
2 insert into child (id, f_id, name) VALUES ('1', '1', 'pipey ')
3 insert into child (id, f_id, name) VALUES ('2', '1', 'pupeye ')
4 select child. id, CHILD. f_id, CHILD. name, father. id AS id_0, father. name AS name_0 from child, FATHER father where child. f_ID = father. ID (+) and child. ID = '1'
5 update child set f_id = '1', name = 'pipeline' WHERE id = '1'
6 delete from child where id = '1'

7 delete from child where id = '2'

8 delete from father where id = '1'

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.