|
| 1 | +#3.3.0版本新增内容使用文档 |
| 2 | + |
| 3 | +##增加对动态表名的支持 |
| 4 | + |
| 5 | +###新增`IDynamicTableName`接口: |
| 6 | + |
| 7 | +```java |
| 8 | +/** |
| 9 | + * 实现动态表名时,实体类实现该接口 |
| 10 | + * |
| 11 | + * @author liuzh |
| 12 | + * @since 2015-10-28 22:20 |
| 13 | + */ |
| 14 | +public interface IDynamicTableName { |
| 15 | + |
| 16 | + /** |
| 17 | + * 获取动态表名 - 这个方法是关键,只要有返回值,不是null和'',就会用返回值作为表名 |
| 18 | + * |
| 19 | + * @return |
| 20 | + */ |
| 21 | + String getDynamicTableName(); |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +###一个动态表名的例子 |
| 26 | + |
| 27 | +####首先继承`IDynamicTableName`接口: |
| 28 | +```java |
| 29 | +public class Country implements IDynamicTableName { |
| 30 | + |
| 31 | + @Id |
| 32 | + @OrderBy("desc") |
| 33 | + private Integer id; |
| 34 | + |
| 35 | + @Column |
| 36 | + private String countryname; |
| 37 | + private String countrycode; |
| 38 | + |
| 39 | + @Transient//非表字段,字段名称无所谓 |
| 40 | + private String dynamicTableName123; |
| 41 | + |
| 42 | + //省略getter和setter |
| 43 | + |
| 44 | + @Override//当该方法返回值不为空时,就会使用返回值作为表名 |
| 45 | + public String getDynamicTableName() { |
| 46 | + return dynamicTableName123; |
| 47 | + } |
| 48 | + |
| 49 | + public void setDynamicTableName(String dynamicTableName) { |
| 50 | + this.dynamicTableName123 = dynamicTableName; |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +####测试代码: |
| 56 | + |
| 57 | +```java |
| 58 | +CountryMapper mapper = sqlSession.getMapper(CountryMapper.class); |
| 59 | +Country country = new Country(); |
| 60 | +country.setDynamicTableName("country_123"); |
| 61 | +List<country> countryList = mapper.select(country); |
| 62 | +``` |
| 63 | + |
| 64 | +####输出SQL: |
| 65 | + |
| 66 | +`SELECT id,countryname,countrycode FROM country_123 ORDER BY id` |
| 67 | + |
| 68 | +##`Example`增加了4个方法 |
| 69 | + |
| 70 | +```java |
| 71 | +/** |
| 72 | + * 手写条件 |
| 73 | + * |
| 74 | + * @param condition 例如 "length(countryname)<5" |
| 75 | + * @return |
| 76 | + */ |
| 77 | +public Criteria andCondition(String condition) { |
| 78 | + addCriterion(condition); |
| 79 | + return (Criteria) this; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * 手写左边条件,右边用value值 |
| 84 | + * |
| 85 | + * @param condition 例如 "length(countryname)=" |
| 86 | + * @param value 例如 5 |
| 87 | + * @return |
| 88 | + */ |
| 89 | +public Criteria andCondition(String condition, Object value) { |
| 90 | + criteria.add(new Criterion(condition, value)); |
| 91 | + return (Criteria) this; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * 手写左边条件,右边用value值 |
| 96 | + * |
| 97 | + * @param condition 例如 "length(countryname)=" |
| 98 | + * @param value 例如 5 |
| 99 | + * @param typeHandler 类型处理 |
| 100 | + * @return |
| 101 | + */ |
| 102 | +public Criteria andCondition(String condition, Object value, String typeHandler) { |
| 103 | + criteria.add(new Criterion(condition, value, typeHandler)); |
| 104 | + return (Criteria) this; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * 手写左边条件,右边用value值 |
| 109 | + * |
| 110 | + * @param condition 例如 "length(countryname)=" |
| 111 | + * @param value 例如 5 |
| 112 | + * @param typeHandler 类型处理 |
| 113 | + * @return |
| 114 | + */ |
| 115 | +public Criteria andCondition(String condition, Object value, Class<? extends TypeHandler> typeHandler) { |
| 116 | + criteria.add(new Criterion(condition, value, typeHandler.getCanonicalName())); |
| 117 | + return (Criteria) this; |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +###`Example`新方法用法举例 |
| 122 | +```java |
| 123 | +Example example = new Example(Country.class); |
| 124 | +example.createCriteria() |
| 125 | + .andCondition("countryname like 'C%' and id < 100") |
| 126 | + .andCondition("length(countryname) = ", 5) |
| 127 | + .andCondition("countrycode =", "CN", StringTypeHandler.class); |
| 128 | +List<Country> countries = mapper.selectByExample(example); |
| 129 | +``` |
| 130 | + |
| 131 | +###输出SQL |
| 132 | +```sql |
| 133 | +SELECT id,countryname,countrycode |
| 134 | +FROM Country |
| 135 | +WHERE ( |
| 136 | +countryname like 'C%' |
| 137 | +and id < 100 |
| 138 | +and length(countryname) = ? |
| 139 | +and countrycode = ? |
| 140 | +) |
| 141 | +ORDER BY id desc |
| 142 | +``` |
| 143 | + |
| 144 | +##`@GeneratedValue(strategy = GenerationType.IDENTITY)`的一种重要用法说明: |
| 145 | + |
| 146 | +`IDENTITY`除了[集成文档](http://git.oschina.net/free/Mapper/blob/master/wiki/mapper3/3.Use.md#2-<code>-generatedvalue-strategy-=-generationtype-identity-<-code>)中的这些选项外,还可以是任意可以执行的SQL,例如MySql的`select uuid()`,SqlServer的`select newid()`等等,这种情况下需要保证主键的类型和SQL的返回值一致。 |
| 147 | + |
| 148 | +利用这一个特点,我们就可以使用可以回写的UUID值,如果想获得更特殊的主键值,可以自己写函数调用。 |
0 commit comments