Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd04abd

Browse files
committed
增加SqlServer专用的两个insert方法。
1 parent 48f278f commit fd04abd

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 [email protected]
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.github.abel533.mapper;
26+
27+
28+
import com.github.abel533.mapper.sqlserver.InsertMapper;
29+
import com.github.abel533.mapper.sqlserver.InsertSelectiveMapper;
30+
31+
/**
32+
* 通用Mapper接口,SqlServerMapper独有的通用方法
33+
*
34+
* @param <T> 不能为空
35+
* @author liuzh
36+
*/
37+
public interface SqlServerMapper<T> extends
38+
InsertMapper<T>,
39+
InsertSelectiveMapper<T> {
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 [email protected]
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.github.abel533.mapper.sqlserver;
26+
27+
import com.github.abel533.provider.SqlServerProvider;
28+
import org.apache.ibatis.annotations.InsertProvider;
29+
import org.apache.ibatis.annotations.Options;
30+
31+
/**
32+
* 通用Mapper接口,插入
33+
*
34+
* @param <T> 不能为空
35+
* @author liuzh
36+
*/
37+
public interface InsertMapper<T> {
38+
39+
@Options(useGeneratedKeys = true, keyProperty = "id")
40+
@InsertProvider(type = SqlServerProvider.class, method = "dynamicSQL")
41+
int insert(T record);
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 [email protected]
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.github.abel533.mapper.sqlserver;
26+
27+
import com.github.abel533.provider.SqlServerProvider;
28+
import org.apache.ibatis.annotations.InsertProvider;
29+
import org.apache.ibatis.annotations.Options;
30+
31+
/**
32+
* 通用Mapper接口,插入
33+
*
34+
* @param <T> 不能为空
35+
* @author liuzh
36+
*/
37+
public interface InsertSelectiveMapper<T> {
38+
39+
@Options(useGeneratedKeys = true, keyProperty = "id")
40+
@InsertProvider(type = SqlServerProvider.class, method = "dynamicSQL")
41+
int insertSelective(T record);
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 [email protected]
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.github.abel533.provider;
26+
27+
import com.github.abel533.mapperhelper.EntityHelper;
28+
import com.github.abel533.mapperhelper.MapperHelper;
29+
import com.github.abel533.mapperhelper.MapperTemplate;
30+
import org.apache.ibatis.mapping.MappedStatement;
31+
import org.apache.ibatis.scripting.xmltags.*;
32+
33+
import java.util.LinkedList;
34+
import java.util.LinkedList;
35+
import java.util.List;
36+
import java.util.Set;
37+
38+
/**
39+
* Mappper实现类,特殊方法实现类
40+
*
41+
* @author liuzh
42+
*/
43+
public class SqlServerProvider extends MapperTemplate {
44+
45+
public SqlServerProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
46+
super(mapperClass, mapperHelper);
47+
}
48+
49+
/**
50+
* 插入
51+
*
52+
* @param ms
53+
*/
54+
public String insert(MappedStatement ms) {
55+
final Class<?> entityClass = getSelectReturnType(ms);
56+
EntityHelper.EntityTable table = EntityHelper.getEntityTable(entityClass);
57+
//开始拼sql
58+
StringBuilder sql = new StringBuilder();
59+
sql.append("insert into ");
60+
sql.append(table.getName());
61+
sql.append("(");
62+
boolean first = true;
63+
for (EntityHelper.EntityColumn column : table.getEntityClassColumns()) {
64+
if (column.isId()) {
65+
continue;
66+
}
67+
if(!first) {
68+
sql.append(",");
69+
}
70+
sql.append(column.getColumn());
71+
first = false;
72+
}
73+
sql.append(") values(");
74+
first = true;
75+
for (EntityHelper.EntityColumn column : table.getEntityClassColumns()) {
76+
if (column.isId()) {
77+
continue;
78+
}
79+
if(!first) {
80+
sql.append(",");
81+
}
82+
sql.append("#{").append(column.getProperty()).append("}");
83+
first = false;
84+
}
85+
sql.append(")");
86+
return sql.toString();
87+
}
88+
89+
/**
90+
* 插入不为null的字段
91+
*
92+
* @param ms
93+
* @return
94+
*/
95+
public SqlNode insertSelective(MappedStatement ms) {
96+
Class<?> entityClass = getSelectReturnType(ms);
97+
List<SqlNode> sqlNodes = new LinkedList<SqlNode>();
98+
//insert into table
99+
sqlNodes.add(new StaticTextSqlNode("INSERT INTO " + tableName(entityClass)));
100+
//获取全部列
101+
Set<EntityHelper.EntityColumn> columnList = EntityHelper.getColumns(entityClass);
102+
List<SqlNode> ifNodes = new LinkedList<SqlNode>();
103+
//Identity列只能有一个
104+
Boolean hasIdentityKey = false;
105+
//当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
106+
for (EntityHelper.EntityColumn column : columnList) {
107+
//当使用序列时
108+
if (!column.isId()) {
109+
ifNodes.add(getIfNotNull(column, new StaticTextSqlNode(column.getColumn() + ",")));
110+
}
111+
}
112+
//将动态的列加入sqlNodes
113+
sqlNodes.add(new TrimSqlNode(ms.getConfiguration(), new MixedSqlNode(ifNodes), "(", null, ")", ","));
114+
115+
ifNodes = new LinkedList<SqlNode>();
116+
//处理values(#{property},#{property}...)
117+
for (EntityHelper.EntityColumn column : columnList) {
118+
//当参数中的属性值不为空的时候,使用传入的值
119+
if (!column.isId()) {
120+
ifNodes.add(new IfSqlNode(new StaticTextSqlNode("#{" + column.getProperty() + "},"), column.getProperty() + " != null "));
121+
}
122+
}
123+
//values(#{property},#{property}...)
124+
sqlNodes.add(new TrimSqlNode(ms.getConfiguration(), new MixedSqlNode(ifNodes), "VALUES (", null, ")", ","));
125+
return new MixedSqlNode(sqlNodes);
126+
}
127+
}

0 commit comments

Comments
 (0)