-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathupdate.html
170 lines (157 loc) · 9.26 KB
/
update.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 2.0.0-M18 from src/site/markdown/docs/update.md at 03 Jun 2024
| Rendered using Apache Maven Fluido Skin 2.0.0-M9
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="generator" content="Apache Maven Doxia Site Renderer 2.0.0-M18" />
<title>MyBatis Dynamic SQL</title>
<link rel="stylesheet" href="../css/apache-maven-fluido-2.0.0-M9.min.css" />
<link rel="stylesheet" href="../css/site.css" />
<link rel="stylesheet" href="../css/print.css" media="print" />
<script src="../js/apache-maven-fluido-2.0.0-M9.min.js"></script>
</head>
<body>
<div class="container-fluid container-fluid-top">
<header>
<div id="banner">
<div class="pull-left"><div id="bannerLeft"><h1><a href="../docs/introduction.html">MyBatis Dynamic SQL</a></h1></div></div>
<div class="pull-right"><div id="bannerRight"><h1><a href="http://www.mybatis.org/" class="externalLink"><img class="imageLink" src="https://mybatis.org/images/mybatis-logo.png" alt="MyBatis logo" /> MyBatis</a></h1></div></div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 03 Jun 2024<span class="divider">|</span>
</li>
<li id="projectVersion">Version: 1.5.2</li>
</ul>
</div>
</header>
<div class="row-fluid">
<header id="leftColumn" class="span2">
<nav class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">User's Guide</li>
<li><a href="../docs/introduction.html">Introduction</a></li>
<li><a href="../docs/CHANGELOG.html">Change Log</a></li>
<li><a href="../docs/quickStart.html">Quick Start</a></li>
<li><a href="../docs/exceptions.html">Exceptions thrown by the Library</a></li>
<li><a href="../docs/configuration.html">Configuration of the Library</a></li>
<li><a href="../docs/databaseObjects.html">Modeling Database Objects</a></li>
<li><a href="../docs/whereClauses.html"><span class="icon-chevron-down"></span>WHERE Clause Support</a>
<ul class="nav nav-list">
<li><a href="../docs/conditions.html">WHERE Conditions</a></li>
</ul></li>
<li><a href="../docs/select.html"><span class="icon-chevron-down"></span>SELECT Statements</a>
<ul class="nav nav-list">
<li><a href="../docs/caseExpressions.html">Case Expressions</a></li>
<li><a href="../docs/complexQueries.html">Complex Queries</a></li>
</ul></li>
<li><a href="../docs/delete.html">DELETE Statements</a></li>
<li><a href="../docs/insert.html">INSERT Statements</a></li>
<li class="active"><a>UPDATE Statements</a></li>
<li><a href="../docs/subQueries.html">SubQuery Support</a></li>
<li><a href="../docs/functions.html">Database Functions</a></li>
<li><a href="../docs/mybatis3.html">MyBatis3 Support</a></li>
<li><a href="../docs/spring.html">Spring Support</a></li>
<li><a href="../docs/springBatch.html">Spring Batch Support</a></li>
<li><a href="../docs/kotlinOverview.html"><span class="icon-chevron-right"></span>Kotlin Support</a></li>
<li><a href="../docs/howItWorks.html">How it Works</a></li>
<li><a href="../docs/extending.html">Extending the Library</a></li>
<li><a href="../docs/codingStandards.html">Coding Standards</a></li>
<li><a href="../docs/motivation.html">Motivation</a></li>
<li class="nav-header">Project Documentation</li>
<li><a href="../project-info.html"><span class="icon-chevron-right"></span>Project Information</a></li>
<li><a href="../project-reports.html"><span class="icon-chevron-right"></span>Project Reports</a></li>
</ul>
</nav>
<div class="well sidebar-nav">
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<a href="https://maven.apache.org/" class="builtBy" target="_blank"><img class="builtBy" alt="Built by Maven" src="../images/logos/maven-feather.png" /></a>
</div>
</div>
</header>
<main id="bodyColumn" class="span10">
<section><a id="Update_Statements"></a>
<h1>Update Statements</h1>
<p>Update statements are composed by specifying the table and columns to update, and an optional where clause. For example:</p>
<pre><code class="language-java"> UpdateStatementProvider updateStatement = update(animalData)
.set(bodyWeight).equalTo(row.getBodyWeight())
.set(animalName).equalToNull()
.where(id, isIn(1, 5, 7))
.or(id, isIn(2, 6, 8), and(animalName, isLike("%bat")))
.or(id, isGreaterThan(60))
.and(bodyWeight, isBetween(1.0).and(3.0))
.build()
.render(RenderingStrategies.MYBATIS3);
int rows = mapper.update(updateStatement);
</code></pre>
<p>Notice the <code>set</code> method. It is used to set the value of a database column. There are several options for setting a value:</p>
<ol style="list-style-type: decimal;">
<li><code>set(column).equalToNull()</code> will set a null into a column</li>
<li><code>set(column).equalToConstant(String constant)</code> will set a constant into a column. The constant_value will be written into the generated update statement exactly as entered</li>
<li><code>set(column).equalToStringConstant(String constant)</code> will set a constant into a column. The constant_value will be written into the generated update statement surrounded by single quote marks (as an SQL String)</li>
<li><code>set(column).equalTo(T value)</code> will set a value into a column. The value will be bound to the SQL statement as a prepared statement parameter</li>
<li><code>set(column).equalTo(Supplier<T> valueSupplier)</code> will set a value into a column. The value will be bound to the SQL statement as a prepared statement parameter</li>
<li><code>set(column).equalToWhenPresent(T value)</code> will set a value into a column if the value is non-null. The value of the property will be bound to the SQL statement as a prepared statement parameter. This is used to generate a “selective” update as defined in MyBatis Generator.</li>
<li><code>set(column).equalToWhenPresent(Supplier<T> valueSupplier)</code> will set a value into a column if the value is non-null. The value of the property will be bound to the SQL statement as a prepared statement parameter. This is used to generate a “selective” update as defined in MyBatis Generator.</li>
<li><code>set(column).equalTo(Buildable<SelectModel> selectModelBuilder)</code> will set the result of a sub-query into a column. The query should only have one column and the type of the returned column must be able to be converted by the database if it is not the same type. These constraints are NOT validated by the library.</li>
<li><code>set(column).equalTo(BasicColumn rightColumn)</code> will set the value of a column the be the value of another column. This is also useful for specifying a function such as add, subtract, etc.</li>
</ol>
<p>You can also build an update statement without a where clause. This will update every row in a table.
For example:</p>
<pre><code class="language-java"> UpdateStatementProvider updateStatement = update(animalData)
.set(bodyWeight).equalTo(row.getBodyWeight())
.set(animalName).equalToNull()
.build()
.render(RenderingStrategies.MYBATIS3);
</code></pre><section><a id="Annotated_Mapper_for_Update_Statements"></a>
<h2>Annotated Mapper for Update Statements</h2>
<p>The UpdateStatementProvider object can be used as a parameter to a MyBatis mapper method directly. If you
are using an annotated mapper, the update method should look like this:</p>
<pre><code class="language-java">import org.apache.ibatis.annotations.UpdateProvider;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
...
@UpdateProvider(type=SqlProviderAdapter.class, method="update")
int update(UpdateStatementProvider updateStatement);
...
</code></pre></section><section><a id="XML_Mapper_for_Update_Statements"></a>
<h2>XML Mapper for Update Statements</h2>
<p>We do not recommend using an XML mapper for update statements, but if you want to do so the UpdateStatementProvider object can be used as a parameter to a MyBatis mapper method directly.</p>
<p>If you are using an XML mapper, the update method should look like this in the Java interface:</p>
<pre><code class="language-java">import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
...
int update(UpdateStatementProvider updateStatement);
...
</code></pre>
<p>The XML element should look like this:</p>
<pre><code class="language-xml"> <update id="update">
${updateStatement}
</update>
</code></pre></section></section>
</main>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p>© 2016–2024
<a href="https://www.mybatis.org/">MyBatis.org</a>
</p>
</div>
</div>
</footer>
<script>
if(anchors) {
anchors.add();
}
</script>
</body>
</html>