|
| 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.mysql; |
| 17 | + |
| 18 | +import static examples.mysql.MemberOfCondition.memberOf; |
| 19 | +import static examples.mysql.MemberOfFunction.memberOf; |
| 20 | +import static examples.mariadb.ItemsDynamicSQLSupport.*; |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.assertj.core.api.Assertions.entry; |
| 23 | +import static org.mybatis.dynamic.sql.SqlBuilder.*; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import config.TestContainersConfiguration; |
| 29 | +import org.apache.ibatis.datasource.unpooled.UnpooledDataSource; |
| 30 | +import org.apache.ibatis.mapping.Environment; |
| 31 | +import org.apache.ibatis.session.Configuration; |
| 32 | +import org.apache.ibatis.session.SqlSession; |
| 33 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 34 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 35 | +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; |
| 36 | +import org.junit.jupiter.api.BeforeAll; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import org.mybatis.dynamic.sql.render.RenderingStrategies; |
| 39 | +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; |
| 40 | +import org.mybatis.dynamic.sql.util.mybatis3.CommonSelectMapper; |
| 41 | +import org.testcontainers.containers.MySQLContainer; |
| 42 | +import org.testcontainers.junit.jupiter.Container; |
| 43 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 44 | + |
| 45 | +@Testcontainers |
| 46 | +class MySQLTest { |
| 47 | + |
| 48 | + @SuppressWarnings("resource") |
| 49 | + @Container |
| 50 | + private static final MySQLContainer<?> mysql = |
| 51 | + new MySQLContainer<>(TestContainersConfiguration.MYSQL_LATEST) |
| 52 | + .withInitScript("examples/mariadb/CreateDB.sql"); |
| 53 | + |
| 54 | + private static SqlSessionFactory sqlSessionFactory; |
| 55 | + |
| 56 | + @BeforeAll |
| 57 | + static void setup() { |
| 58 | + UnpooledDataSource ds = new UnpooledDataSource(mysql.getDriverClassName(), mysql.getJdbcUrl(), |
| 59 | + mysql.getUsername(), mysql.getPassword()); |
| 60 | + Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); |
| 61 | + Configuration config = new Configuration(environment); |
| 62 | + config.addMapper(CommonSelectMapper.class); |
| 63 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void smokeTest() { |
| 68 | + try (SqlSession session = sqlSessionFactory.openSession()) { |
| 69 | + CommonSelectMapper mapper = session.getMapper(CommonSelectMapper.class); |
| 70 | + |
| 71 | + SelectStatementProvider selectStatement = select(id, description) |
| 72 | + .from(items) |
| 73 | + .orderBy(id) |
| 74 | + .build() |
| 75 | + .render(RenderingStrategies.MYBATIS3); |
| 76 | + List<Map<String, Object>> rows = mapper.selectManyMappedRows(selectStatement); |
| 77 | + assertThat(rows).hasSize(20); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testMemberOfAsCondition() { |
| 83 | + try (SqlSession session = sqlSessionFactory.openSession()) { |
| 84 | + CommonSelectMapper mapper = session.getMapper(CommonSelectMapper.class); |
| 85 | + |
| 86 | + SelectStatementProvider selectStatement = select(id, memberOf(id, "'[1, 2, 3]'").as("inList")) |
| 87 | + .from(items) |
| 88 | + .where(id, memberOf("'[1, 2, 3]'")) |
| 89 | + .orderBy(id) |
| 90 | + .build() |
| 91 | + .render(RenderingStrategies.MYBATIS3); |
| 92 | + |
| 93 | + assertThat(selectStatement.getSelectStatement()) |
| 94 | + .isEqualTo("select id, id member of('[1, 2, 3]') as inList from items where id member of('[1, 2, 3]') order by id"); |
| 95 | + |
| 96 | + List<Map<String, Object>> rows = mapper.selectManyMappedRows(selectStatement); |
| 97 | + assertThat(rows).hasSize(3); |
| 98 | + assertThat(rows.get(2)).containsOnly(entry("id", 3), entry("inList", 1L)); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void testMemberOfAsFunction() { |
| 104 | + try (SqlSession session = sqlSessionFactory.openSession()) { |
| 105 | + CommonSelectMapper mapper = session.getMapper(CommonSelectMapper.class); |
| 106 | + |
| 107 | + SelectStatementProvider selectStatement = select(id, memberOf(id, "'[1, 2, 3]'").as("inList")) |
| 108 | + .from(items) |
| 109 | + .where(memberOf(id,"'[1, 2, 3]'"), isEqualTo(1L)) |
| 110 | + .orderBy(id) |
| 111 | + .build() |
| 112 | + .render(RenderingStrategies.MYBATIS3); |
| 113 | + |
| 114 | + assertThat(selectStatement.getSelectStatement()) |
| 115 | + .isEqualTo("select id, id member of('[1, 2, 3]') as inList from items where id member of('[1, 2, 3]') = #{parameters.p1} order by id"); |
| 116 | + |
| 117 | + List<Map<String, Object>> rows = mapper.selectManyMappedRows(selectStatement); |
| 118 | + assertThat(rows).hasSize(3); |
| 119 | + assertThat(rows.get(2)).containsOnly(entry("id", 3), entry("inList", 1L)); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments