View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package examples.simple;
17  
18  import static examples.simple.PersonDynamicSqlSupport.id;
19  import static examples.simple.PersonDynamicSqlSupport.occupation;
20  import static org.assertj.core.api.Assertions.assertThat;
21  import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
22  import static org.mybatis.dynamic.sql.SqlBuilder.isNull;
23  import static org.mybatis.dynamic.sql.SqlBuilder.where;
24  
25  import java.io.InputStream;
26  import java.io.InputStreamReader;
27  import java.sql.Connection;
28  import java.sql.DriverManager;
29  import java.util.List;
30  
31  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
32  import org.apache.ibatis.jdbc.ScriptRunner;
33  import org.apache.ibatis.mapping.Environment;
34  import org.apache.ibatis.session.Configuration;
35  import org.apache.ibatis.session.SqlSession;
36  import org.apache.ibatis.session.SqlSessionFactory;
37  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
38  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
39  import org.junit.jupiter.api.BeforeEach;
40  import org.junit.jupiter.api.Test;
41  import org.mybatis.dynamic.sql.where.WhereApplier;
42  
43  class ReusableWhereTest {
44  
45      private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
46      private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
47  
48      private SqlSessionFactory sqlSessionFactory;
49  
50      @BeforeEach
51      void setup() throws Exception {
52          Class.forName(JDBC_DRIVER);
53          InputStream is = getClass().getResourceAsStream("/examples/simple/CreateSimpleDB.sql");
54          assert is != null;
55          try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
56              ScriptRunner sr = new ScriptRunner(connection);
57              sr.setLogWriter(null);
58              sr.runScript(new InputStreamReader(is));
59          }
60  
61          UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
62          Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
63          Configuration config = new Configuration(environment);
64          config.addMapper(PersonMapper.class);
65          config.addMapper(PersonWithAddressMapper.class);
66          sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
67      }
68  
69      @Test
70      void testCount() {
71          try (SqlSession session = sqlSessionFactory.openSession()) {
72              PersonMapper mapper = session.getMapper(PersonMapper.class);
73  
74              long rows = mapper.count(c -> c.applyWhere(commonWhere));
75  
76              assertThat(rows).isEqualTo(3);
77          }
78      }
79  
80      @Test
81      void testDelete() {
82          try (SqlSession session = sqlSessionFactory.openSession()) {
83              PersonMapper mapper = session.getMapper(PersonMapper.class);
84  
85              int rows = mapper.delete(c -> c.applyWhere(commonWhere));
86  
87              assertThat(rows).isEqualTo(3);
88          }
89      }
90  
91      @Test
92      void testSelect() {
93          try (SqlSession session = sqlSessionFactory.openSession()) {
94              PersonMapper mapper = session.getMapper(PersonMapper.class);
95  
96              List<PersonRecord> rows = mapper.select(c ->
97                  c.applyWhere(commonWhere)
98                  .orderBy(id));
99  
100             assertThat(rows).hasSize(3);
101         }
102     }
103 
104     @Test
105     void testUpdate() {
106         try (SqlSession session = sqlSessionFactory.openSession()) {
107             PersonMapper mapper = session.getMapper(PersonMapper.class);
108 
109             int rows = mapper.update(c ->
110                 c.set(occupation).equalToStringConstant("worker")
111                 .applyWhere(commonWhere));
112 
113             assertThat(rows).isEqualTo(3);
114         }
115     }
116 
117     private final WhereApplier commonWhere = where(id, isEqualTo(1)).or(occupation, isNull()).toWhereApplier();
118 }