diff --git a/docs/spring/cse3ef992a-8c1e-11ee-8a91-acde48001122.java b/docs/spring/cse3ef992a-8c1e-11ee-8a91-acde48001122.java new file mode 100644 index 00000000..bb80dba4 --- /dev/null +++ b/docs/spring/cse3ef992a-8c1e-11ee-8a91-acde48001122.java @@ -0,0 +1,31 @@ +/** + * Copyright 2009-2019 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.submitted.foreach_map; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class MapParam { + private Map map = new LinkedHashMap<>(); + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } +} diff --git a/docs/spring/cse434f1b4-8c1e-11ee-8a91-acde48001122.java b/docs/spring/cse434f1b4-8c1e-11ee-8a91-acde48001122.java new file mode 100644 index 00000000..05e76110 --- /dev/null +++ b/docs/spring/cse434f1b4-8c1e-11ee-8a91-acde48001122.java @@ -0,0 +1,166 @@ +/** + * Copyright 2009-2019 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.transaction.jdbc; + +import org.apache.ibatis.logging.Log; +import org.apache.ibatis.logging.LogFactory; +import org.apache.ibatis.session.TransactionIsolationLevel; +import org.apache.ibatis.transaction.Transaction; +import org.apache.ibatis.transaction.TransactionException; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; + +/** + * + *

+ *

+ *

这部分的内容是数据JDBC操作

+ * {@link Transaction} that makes use of the JDBC commit and rollback facilities directly. + * It relies on the connection retrieved from the dataSource to manage the scope of the transaction. + * Delays connection retrieval until getConnection() is called. + * Ignores commit or rollback requests when autocommit is on. + * + * @author Clinton Begin + * @see JdbcTransactionFactory + */ +public class JdbcTransaction implements Transaction { + + private static final Log log = LogFactory.getLog(JdbcTransaction.class); + + protected Connection connection; + protected DataSource dataSource; + protected TransactionIsolationLevel level; + protected boolean autoCommit; + + public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) { + dataSource = ds; + level = desiredLevel; + autoCommit = desiredAutoCommit; + } + + public JdbcTransaction(Connection connection) { + this.connection = connection; + } + + /** + * 获取连接对象 + * @return + * @throws SQLException + */ + @Override + public Connection getConnection() throws SQLException { + if (connection == null) { + openConnection(); + } + return connection; + } + + /** + * 提交 + * @throws SQLException + */ + @Override + public void commit() throws SQLException { + if (connection != null && !connection.getAutoCommit()) { + if (log.isDebugEnabled()) { + log.debug("Committing JDBC Connection [" + connection + "]"); + } + connection.commit(); + } + } + + /** + * 回滚 + * @throws SQLException + */ + @Override + public void rollback() throws SQLException { + if (connection != null && !connection.getAutoCommit()) { + if (log.isDebugEnabled()) { + log.debug("Rolling back JDBC Connection [" + connection + "]"); + } + connection.rollback(); + } + } + + @Override + public void close() throws SQLException { + if (connection != null) { + resetAutoCommit(); + if (log.isDebugEnabled()) { + log.debug("Closing JDBC Connection [" + connection + "]"); + } + connection.close(); + } + } + + protected void setDesiredAutoCommit(boolean desiredAutoCommit) { + try { + if (connection.getAutoCommit() != desiredAutoCommit) { + if (log.isDebugEnabled()) { + log.debug("Setting autocommit to " + desiredAutoCommit + " on JDBC Connection [" + connection + "]"); + } + connection.setAutoCommit(desiredAutoCommit); + } + } catch (SQLException e) { + // Only a very poorly implemented driver would fail here, + // and there's not much we can do about that. + throw new TransactionException("Error configuring AutoCommit. " + + "Your driver may not support getAutoCommit() or setAutoCommit(). " + + "Requested setting: " + desiredAutoCommit + ". Cause: " + e, e); + } + } + + protected void resetAutoCommit() { + try { + if (!connection.getAutoCommit()) { + // MyBatis does not call commit/rollback on a connection if just selects were performed. + // Some databases start transactions with select statements + // and they mandate a commit/rollback before closing the connection. + // A workaround is setting the autocommit to true before closing the connection. + // Sybase throws an exception here. + if (log.isDebugEnabled()) { + log.debug("Resetting autocommit to true on JDBC Connection [" + connection + "]"); + } + connection.setAutoCommit(true); + } + } catch (SQLException e) { + if (log.isDebugEnabled()) { + log.debug("Error resetting autocommit to true " + + "before closing the connection. Cause: " + e); + } + } + } + + protected void openConnection() throws SQLException { + if (log.isDebugEnabled()) { + log.debug("Opening JDBC Connection"); + } + connection = dataSource.getConnection(); + if (level != null) { + connection.setTransactionIsolation(level.getLevel()); + } + setDesiredAutoCommit(autoCommit); + } + + @Override + public Integer getTimeout() throws SQLException { + return null; + } + +} diff --git a/docs/spring/cse47cec4e-8c1e-11ee-8a91-acde48001122.java b/docs/spring/cse47cec4e-8c1e-11ee-8a91-acde48001122.java new file mode 100644 index 00000000..08f3f3c6 --- /dev/null +++ b/docs/spring/cse47cec4e-8c1e-11ee-8a91-acde48001122.java @@ -0,0 +1,37 @@ +/** + * Copyright 2009-2019 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.reflection; + +import org.junit.jupiter.api.Test; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.UndeclaredThrowableException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ExceptionUtilTest { + + @Test + void shouldUnwrapThrowable() { + Exception exception = new Exception(); + assertEquals(exception, ExceptionUtil.unwrapThrowable(exception)); + assertEquals(exception, ExceptionUtil.unwrapThrowable(new InvocationTargetException(exception, "test"))); + assertEquals(exception, ExceptionUtil.unwrapThrowable(new UndeclaredThrowableException(exception, "test"))); + assertEquals(exception, ExceptionUtil.unwrapThrowable(new InvocationTargetException(new InvocationTargetException(exception, "test"), "test"))); + assertEquals(exception, ExceptionUtil.unwrapThrowable(new InvocationTargetException(new UndeclaredThrowableException(exception, "test"), "test"))); + } + +} diff --git a/docs/spring/cse4c3851e-8c1e-11ee-8a91-acde48001122.java b/docs/spring/cse4c3851e-8c1e-11ee-8a91-acde48001122.java new file mode 100644 index 00000000..d7cbe115 --- /dev/null +++ b/docs/spring/cse4c3851e-8c1e-11ee-8a91-acde48001122.java @@ -0,0 +1,43 @@ +/** + * Copyright 2009-2015 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.domain.blog; + +public class Tag { + + private int id; + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Tag: " + id + " : " + name; + } +} diff --git a/docs/spring/cse5138316-8c1e-11ee-8a91-acde48001122.java b/docs/spring/cse5138316-8c1e-11ee-8a91-acde48001122.java new file mode 100644 index 00000000..0c4ce062 --- /dev/null +++ b/docs/spring/cse5138316-8c1e-11ee-8a91-acde48001122.java @@ -0,0 +1,142 @@ +package com.huifer.data.list.doubleLinkedList; + +/** + *

Title : DoubleLinkedList

+ *

Description : 双向循环链表

+ * + * @author huifer + * @date 2019-04-18 + */ +public class DoubleLinkedList { + + private DoubleNode first; + private DoubleNode last; + private int size = 0; + + public static void main(String[] args) { + DoubleLinkedList d = new DoubleLinkedList(); + d.add(0); + d.add(1); + d.add(2); + + d.add(1, 999); + d.delete(999); + System.out.println(); + } + + public void add(Integer data) { + addLast(data); + } + + + public boolean delete(Integer data) { + if (data == null) { + throw new RuntimeException("参数不能空"); + } else { + for (DoubleNode d = first; d != null; d = d.next) { + if (d.data.equals(data)) { + deleteNode(d); + return true; + } + } + } + return false; + } + + private Integer deleteNode(DoubleNode d) { + DoubleNode next = d.next; + DoubleNode prev = d.prev; + + if (prev == null) { + first = next; + } else { + // 当前结点上一个结点等与当前结点的下一个 + prev.next = next; + d.prev = null; + } + + if (next == null) { + last = prev; + } else { + // 当前结点的下一个结点等与当前结点的上一个结点 + next.prev = prev; + d.next = null; + } + + d.data = null; + size--; + return d.data; + } + + + public DoubleNode get(int index) { + return node(index); + } + + + private void addLast(Integer data) { + // 原来的最后一个结点 + DoubleNode l = this.last; + // 新增节点 ,新增节点的上一个结点是最后一个 没有下一个结点 + DoubleNode newNode = new DoubleNode(data, l, null); + last = newNode; + if (l == null) { + // 如果最后一个结点等于空 那么只有一个结点 第一个结点等于新节点 + first = newNode; + } else { + // 否则l的下一个结点等于新节点 + l.next = newNode; + } + size++; + } + + + public void add(int index, Integer data) { + if (!(index >= 0 && index <= size)) { + throw new IndexOutOfBoundsException(); + } + if (size == index) { + addLast(data); + } else { + addbefor(data, node(index)); + } + + } + + + private void addbefor(Integer data, DoubleNode node) { + // 输入结点的上一个结点 + DoubleNode pred = node.prev; + // 新节点构造 (数据, 上一个结点是输入结点的下一个,下一个结点时输入结点) + DoubleNode newNode = new DoubleNode(data, pred, node); + // 输入结点的下一个结点时新结点 + node.prev = newNode; + if (pred == null) { + first = newNode; + } else { + pred.next = newNode; + } + size++; + } + + + private DoubleNode node(int index) { + // 一半一半的去查询找到这个结点 + if (index < (size >> 1)) { + // 当index这个值小于总量的一半从头查询 反之从尾部开始 + DoubleNode d = first; + for (int i = 0; i < index; i++) { + d = d.next; + } + return d; + } else { + DoubleNode d = this.last; + for (int i = size - 1; i < index; i--) { + d = d.prev; + } + return d; + } + } + + +}