- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Spring3.1.1+MyBatis3.1.1的增、删、查、改以及分页和事务管理由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1. [代码]Mybatis全局配置文件 。
1
2
3
|
<plugins>
< plugin interceptor =
"com.has.core.page.PaginationInterceptor"
/>
</plugins>
|
2. [文件] PaginationInterceptor.java 。
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
|
@Intercepts
({
@Signature
(type = StatementHandler.
class
, method =
"prepare"
, args = { Connection.
class
}) })
public
class
PaginationInterceptor
implements
Interceptor {
@Override
public
Object intercept(Invocation invocation)
throws
Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
BoundSql boundSql = statementHandler.getBoundSql();
MetaObject metaStatementHandler = MetaObject.forObject(statementHandler);
RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue(
"delegate.rowBounds"
);
if
(rowBounds ==
null
|| rowBounds == RowBounds.DEFAULT) {
return
invocation.proceed();
}
Configuration configuration = (Configuration) metaStatementHandler.getValue(
"delegate.configuration"
);
Dialect.Type databaseType =
null
;
try
{
databaseType = Dialect.Type.valueOf(configuration.getVariables().getProperty(
"dialect"
).toUpperCase());
}
catch
(Exception e) {
}
if
(databaseType ==
null
) {
throw
new
RuntimeException(
"the value of the dialect property in configuration.xml is not defined : "
+ configuration.getVariables().getProperty(
"dialect"
));
}
Dialect dialect =
null
;
switch
(databaseType) {
case
MYSQL:
dialect =
new
MySql5Dialect();
break
;
case
ORACLE:
dialect =
new
OracleDialect();
break
;
}
String originalSql = (String) metaStatementHandler.getValue(
"delegate.boundSql.sql"
);
metaStatementHandler.setValue(
"delegate.boundSql.sql"
,
dialect.getLimitString(originalSql, rowBounds.getOffset(), rowBounds.getLimit()));
metaStatementHandler.setValue(
"delegate.rowBounds.offset"
, RowBounds.NO_ROW_OFFSET);
metaStatementHandler.setValue(
"delegate.rowBounds.limit"
, RowBounds.NO_ROW_LIMIT);
return
invocation.proceed();
}
@Override
public
Object plugin(Object target) {
return
Plugin.wrap(target,
this
);
}
@Override
public
void
setProperties(Properties properties) {
}
}
|
3. [文件] Dialect.java 。
1
2
3
4
5
6
7
8
9
10
|
/**
* 数据库方言定义
*
*/
public
abstract
class
Dialect {
public
static
enum
Type {
MYSQL, ORACLE
}
public
abstract
String getLimitString(String sql,
int
skipResults,
int
maxResults);
}
|
4. [文件] MySql5Dialect.java 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/**
* mysql方言分页类
*/
public
class
MySql5Dialect
extends
Dialect {
protected
static
final
String SQL_END_DELIMITER =
";"
;
public
String getLimitString(String sql,
boolean
hasOffset) {
return
MySql5PageHepler.getLimitString(sql, -
1
, -
1
);
}
public
String getLimitString(String sql,
int
offset,
int
limit) {
return
MySql5PageHepler.getLimitString(sql, offset, limit);
}
public
boolean
supportsLimit() {
return
true
;
}
}
|
5. [文件] OracleDialect.java 。
1
2
3
4
5
6
7
8
9
10
11
|
package
com.chyjr.has.core.page.dialect;
/**
* oracel方言分页
*
*/
public
class
OracleDialect
extends
Dialect {
public
String getLimitString(String sql,
int
offset,
int
limit) {
// TODO 未实现
return
""
;
}
}
|
6. [文件] MySql5PageHepler.java 。
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
|
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
/**
* mysql分页工具类
*/
public
class
MySql5PageHepler {
/**
* 得到查询总数的sql
*/
public
static
String getCountString(String querySelect) {
querySelect = getLineSql(querySelect);
int
orderIndex = getLastOrderInsertPoint(querySelect);
int
formIndex = getAfterFormInsertPoint(querySelect);
String select = querySelect.substring(
0
, formIndex);
// 如果SELECT 中包含 DISTINCT 只能在外层包含COUNT
if
(select.toLowerCase().indexOf(
"select distinct"
) != -
1
|| querySelect.toLowerCase().indexOf(
"group by"
) != -
1
) {
return
new
StringBuffer(querySelect.length()).append(
"select count(1) count from ("
)
.append(querySelect.substring(
0
, orderIndex)).append(
" ) t"
).toString();
}
else
{
return
new
StringBuffer(querySelect.length()).append(
"select count(1) count "
)
.append(querySelect.substring(formIndex, orderIndex)).toString();
}
}
/**
* 得到最后一个Order By的插入点位置
*
* @return 返回最后一个Order By插入点的位置
*/
private
static
int
getLastOrderInsertPoint(String querySelect) {
int
orderIndex = querySelect.toLowerCase().lastIndexOf(
"order by"
);
if
(orderIndex == -
1
|| !isBracketCanPartnership(querySelect.substring(orderIndex, querySelect.length()))) {
throw
new
RuntimeException(
"My SQL 分页必须要有Order by 语句!"
);
}
return
orderIndex;
}
/**
* 得到分页的SQL
*
* @param offset
* 偏移量
* @param limit
* 位置
* @return 分页SQL
*/
public
static
String getLimitString(String querySelect,
int
offset,
int
limit) {
querySelect = getLineSql(querySelect);
// String sql = querySelect.replaceAll("[^\\s,]+\\.", "") + " limit " +
// offset + " ," + limit;
String sql = querySelect +
" limit "
+ offset +
" ,"
+ limit;
return
sql;
}
/**
* 将SQL语句变成一条语句,并且每个单词的间隔都是1个空格
*
* @param sql
* SQL语句
* @return 如果sql是NULL返回空,否则返回转化后的SQL
*/
private
static
String getLineSql(String sql) {
return
sql.replaceAll(
"[\r\n]"
,
" "
).replaceAll(
"\\s{2,}"
,
" "
);
}
/**
* 得到SQL第一个正确的FROM的的插入点
*/
private
static
int
getAfterFormInsertPoint(String querySelect) {
String regex =
"\\s+FROM\\s+"
;
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(querySelect);
while
(matcher.find()) {
int
fromStartIndex = matcher.start(
0
);
String text = querySelect.substring(
0
, fromStartIndex);
if
(isBracketCanPartnership(text)) {
return
fromStartIndex;
}
}
return
0
;
}
/**
* 判断括号"()"是否匹配,并不会判断排列顺序是否正确
*
* @param text
* 要判断的文本
* @return 如果匹配返回TRUE,否则返回FALSE
*/
private
static
boolean
isBracketCanPartnership(String text) {
if
(text ==
null
|| (getIndexOfCount(text,
'('
) != getIndexOfCount(text,
')'
))) {
return
false
;
}
return
true
;
}
/**
* 得到一个字符在另一个字符串中出现的次数
*
* @param text
* 文本
* @param ch
* 字符
*/
private
static
int
getIndexOfCount(String text,
char
ch) {
int
count =
0
;
for
(
int
i =
0
; i < text.length(); i++) {
count = (text.charAt(i) == ch) ? count +
1
: count;
}
return
count;
}
}
|
2. [图片] Mybatis.jpg 。
最后此篇关于Spring3.1.1+MyBatis3.1.1的增、删、查、改以及分页和事务管理的文章就讲到这里了,如果你想了解更多关于Spring3.1.1+MyBatis3.1.1的增、删、查、改以及分页和事务管理的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在使用 PostgREST 将数据库实体暴露给使用这些实体的 Springboot 应用。 我的数据库中有两个实体,分别是 Person 和 City。 我想同时保存 Person 实体和 Cit
1、事务的定义 Redis的事务提供了一种“将多个命令打包, 然后一次性、按顺序地执行”的机制。 redis事务的主要作用就是串联多个命令防止别的命令插队。 但是,事务并不具有传统
SQLite 事务(Transaction) 事务(Transaction)是一个对数据库执行工作单元。事务(Transaction)是以逻辑顺序完成的工作单位或序列,可以是由用户手动操作完成,也可
事务是顺序组操作。 它们作为单个单元运行,并且直到组中的所有操作都成功执行时才终止。 组中的单个故障会导致整个事务失败,并导致对数据库没有影响。 事务符合ACID(原子性,一致性,隔离和耐久性)
我希望将 SqlKata 用于一个项目。但是,项目标准的一部分是查询应该能够作为事务执行。有没有一种方法可以使用 MSSQL 事务执行一个查询或多个查询? 非常感谢。 最佳答案 SQLKata 使用
我只是以多线程方式测试 PetaPoco 事务... 我有一个简单的测试用例: -- 简单的值对象称之为 MediaDevice -- 插入一条记录,更新1000次 void TransactionT
我正在尝试从 Excel VBA 向 SQL 中插入一些数据。 SQL 命令是在 VBA 脚本的过程中构建的,包括使用一些 SQL 变量。 我试图了解事务在 VBA 中是如何工作的,以及它们是否可以处
情况如下: 一个大型生产客户端/服务器系统,其中一个中央数据库表具有某个列,该列的默认值是 NULL,但现在默认值是 0。但是在该更改之前创建的所有行当然仍然具有 null 值,这会在该系统中生成许多
数据库事务是一个熟悉的概念。 try { ... .. updateDB() .. ... commit(); } catch error { rollback(); }
我想了解使用传播支持进行 Spring 交易的用途。 java 文档提到如果具有 @Transactional(propagation = Propagation.SUPPORTS) 的方法从支持该事
我需要获取 hibernate 的事务 ID。对于每笔交易,此 ID 必须是唯一的。我尝试使用 session.getTransaction().hashCode(),但我相信这个值不是唯一的。 最佳
我从 firebase 收到以下消息:runTransactionBlock:启用持久性时检测到的使用情况。请注意,事务不会在应用重新启动后保留。 那么应用程序重新启动后到底会发生什么?由于主数据库的
我需要在 jdbc 中执行选择、更新、插入查询的序列。 这是我的代码: public String editRequest(){ connection = DatabaseUtil.getServi
Java 是否提供了一种智能“聚合”事务的方法?如果我有多个异构数据存储库,我想保持同步(即用于数据的 Postgres、用于图表的 Neo4j 以及用于索引的 Lucene),是否有一个范例仅允许
我对标题中的主题有几个问题。首先,假设我们使用 JDBC,并且有 2 个事务 T1 和 T2。在 T1 中,我们在一个特定的行上执行 select 语句。然后我们对该行执行更新。在事务 T2 中,我们
我有一个 Python CGI 处理支付交易。当用户提交表单时,CGI 被调用。提交后,CGI 需要一段时间才能执行信用卡交易。在此期间,用户可能会按下 ESC 或刷新按钮。这样做不会“杀死”CGI,
我有一个代码,类似这样 def many_objects_saving(list_of_objects): for some_object in list_of_objects:
我有一个包含 100,000 条记录的表。我正在考虑使用事务来更新数据。将有一个查询将一列更新为零,并且大约有 5000 个更新,每个更新将更新一条记录。 这些大型事务对内存有何影响?事务运行时选择数
有没有办法在一个命令中执行 SQL 事务?例如 mysql_query(" START TRANSACTION; INSERT INTO table1 ....etc; INSERT INTO tab
真心希望能帮到你! 我使用以下函数在 PHP/MySql 应用程序中发送消息: public function sendMail($sender_id, $recipient_id, $subject
我是一名优秀的程序员,十分优秀!