Skip to content

Commit 56319e4

Browse files
committed
Example增加了4个方法
1 parent 4ee8de3 commit 56319e4

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

src/main/java/tk/mybatis/mapper/entity/Example.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.apache.ibatis.reflection.MetaObject;
2828
import org.apache.ibatis.reflection.SystemMetaObject;
29+
import org.apache.ibatis.type.TypeHandler;
2930
import tk.mybatis.mapper.mapperhelper.EntityHelper;
3031

3132
import java.util.*;
@@ -305,6 +306,55 @@ public Criteria andNotLike(String property, String value) {
305306
return (Criteria) this;
306307
}
307308

309+
/**
310+
* 手写条件
311+
*
312+
* @param condition 例如 "length(countryname)<5"
313+
* @return
314+
*/
315+
public Criteria andCondition(String condition) {
316+
addCriterion(condition);
317+
return (Criteria) this;
318+
}
319+
320+
/**
321+
* 手写左边条件,右边用value值
322+
*
323+
* @param condition 例如 "length(countryname)="
324+
* @param value 例如 5
325+
* @return
326+
*/
327+
public Criteria andCondition(String condition, Object value) {
328+
criteria.add(new Criterion(condition, value));
329+
return (Criteria) this;
330+
}
331+
332+
/**
333+
* 手写左边条件,右边用value值
334+
*
335+
* @param condition 例如 "length(countryname)="
336+
* @param value 例如 5
337+
* @param typeHandler 类型处理
338+
* @return
339+
*/
340+
public Criteria andCondition(String condition, Object value, String typeHandler) {
341+
criteria.add(new Criterion(condition, value, typeHandler));
342+
return (Criteria) this;
343+
}
344+
345+
/**
346+
* 手写左边条件,右边用value值
347+
*
348+
* @param condition 例如 "length(countryname)="
349+
* @param value 例如 5
350+
* @param typeHandler 类型处理
351+
* @return
352+
*/
353+
public Criteria andCondition(String condition, Object value, Class<? extends TypeHandler> typeHandler) {
354+
criteria.add(new Criterion(condition, value, typeHandler.getCanonicalName()));
355+
return (Criteria) this;
356+
}
357+
308358
/**
309359
* 将此对象的不为空的字段参数作为相等查询条件
310360
*

src/test/java/tk/mybatis/mapper/test/example/TestSelectByExample.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tk.mybatis.mapper.test.example;
22

33
import org.apache.ibatis.session.SqlSession;
4+
import org.apache.ibatis.type.StringTypeHandler;
45
import org.junit.Assert;
56
import org.junit.Test;
67
import tk.mybatis.mapper.entity.Example;
@@ -33,6 +34,24 @@ public void testSelectByExample() {
3334
}
3435
}
3536

37+
@Test
38+
public void testAndExample() {
39+
SqlSession sqlSession = MybatisHelper.getSqlSession();
40+
try {
41+
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
42+
Example example = new Example(Country.class);
43+
example.createCriteria()
44+
.andCondition("countryname like 'C%' and id < 100")
45+
.andCondition("length(countryname) = ", 5)
46+
.andCondition("countrycode =", "CN", StringTypeHandler.class);
47+
List<Country> countries = mapper.selectByExample(example);
48+
//查询总数
49+
Assert.assertEquals(1, countries.size());
50+
} finally {
51+
sqlSession.close();
52+
}
53+
}
54+
3655
@Test
3756
public void testSelectByExampleInNotIn() {
3857
SqlSession sqlSession = MybatisHelper.getSqlSession();

src/test/resources/mybatis-java.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<property name="url" value="jdbc:hsqldb:mem:basetest" />
3030
<property name="username" value="sa" />
3131
<!--<property name="driver" value="com.mysql.jdbc.Driver" />
32-
<property name="url" value="jdbc:mysql://192.168.1.200:3306/pagehelper" />
32+
<property name="url" value="jdbc:mysql://192.168.16.137:3306/test" />
3333
<property name="username" value="root" />
3434
<property name="password" value="" />-->
3535
</dataSource>

0 commit comments

Comments
 (0)