1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.BeforeEach;
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 SqlSessionFactory sqlSessionFactory;
55
56 @BeforeEach
57 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
123 @Test
124 void testIsLikeEscape() {
125 try (SqlSession session = sqlSessionFactory.openSession()) {
126 CommonSelectMapper mapper = session.getMapper(CommonSelectMapper.class);
127
128 SelectStatementProvider selectStatement = select(id, description)
129 .from(items)
130 .where(description, IsLikeEscape.isLike("Item 1%", '#').map(s -> s))
131 .orderBy(id)
132 .build()
133 .render(RenderingStrategies.MYBATIS3);
134
135 assertThat(selectStatement.getSelectStatement())
136 .isEqualTo("select id, description from items where description like #{parameters.p1,jdbcType=VARCHAR} ESCAPE '#' order by id");
137
138 List<Map<String, Object>> rows = mapper.selectManyMappedRows(selectStatement);
139 assertThat(rows).hasSize(11);
140 assertThat(rows.get(2)).containsOnly(entry("id", 11), entry("description", "Item 11"));
141 }
142 }
143 }