From 0118b48b44808a763a12608a220c6fccdd536ef2 Mon Sep 17 00:00:00 2001 From: Zen Huifer Date: Tue, 18 Jul 2023 09:36:12 +0800 Subject: [PATCH] write a line into test.file --- ...s76b90c8a-250b-11ee-9e02-acde48001122.java | 55 ++++++++++++ ...s7729dd48-250b-11ee-9e02-acde48001122.java | 32 +++++++ ...s778962a4-250b-11ee-9e02-acde48001122.java | 57 ++++++++++++ ...s78244026-250b-11ee-9e02-acde48001122.java | 88 +++++++++++++++++++ ...s78d58534-250b-11ee-9e02-acde48001122.java | 85 ++++++++++++++++++ ...s79b33668-250b-11ee-9e02-acde48001122.java | 50 +++++++++++ 6 files changed, 367 insertions(+) create mode 100644 docs/spring/cs76b90c8a-250b-11ee-9e02-acde48001122.java create mode 100644 docs/spring/cs7729dd48-250b-11ee-9e02-acde48001122.java create mode 100644 docs/spring/cs778962a4-250b-11ee-9e02-acde48001122.java create mode 100644 docs/spring/cs78244026-250b-11ee-9e02-acde48001122.java create mode 100644 docs/spring/cs78d58534-250b-11ee-9e02-acde48001122.java create mode 100644 docs/spring/cs79b33668-250b-11ee-9e02-acde48001122.java diff --git a/docs/spring/cs76b90c8a-250b-11ee-9e02-acde48001122.java b/docs/spring/cs76b90c8a-250b-11ee-9e02-acde48001122.java new file mode 100644 index 00000000..eeaab5b3 --- /dev/null +++ b/docs/spring/cs76b90c8a-250b-11ee-9e02-acde48001122.java @@ -0,0 +1,55 @@ +/** + * Copyright 2009-2018 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.primitive_result_type; + +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; + +import java.io.Reader; + +public class IbatisConfig { + + private static SqlSessionFactory sqlSessionFactory; + + private IbatisConfig() { + } + + private static synchronized void init() { + if (sqlSessionFactory == null) + try { + final String resource = "org/apache/ibatis/submitted/primitive_result_type/ibatis.xml"; + Reader reader = Resources.getResourceAsReader(resource); + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static SqlSession getSession() { + if (sqlSessionFactory == null) { + init(); + } + return sqlSessionFactory.openSession(); + } + + public static SqlSessionFactory getSqlSessionFactory() { + init(); + return sqlSessionFactory; + } + +} \ No newline at end of file diff --git a/docs/spring/cs7729dd48-250b-11ee-9e02-acde48001122.java b/docs/spring/cs7729dd48-250b-11ee-9e02-acde48001122.java new file mode 100644 index 00000000..c9f55b62 --- /dev/null +++ b/docs/spring/cs7729dd48-250b-11ee-9e02-acde48001122.java @@ -0,0 +1,32 @@ +/** + * Copyright 2009-2018 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.keycolumn; + +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Options; + +public interface InsertMapper { + + @Insert({ + "insert into mbtest.test_identity", + "(first_name, last_name)", + "values(#{firstName,jdbcType=VARCHAR}, #{lastName,jdbcType=VARCHAR})" + }) + @Options(keyProperty = "id", useGeneratedKeys = true, keyColumn = "name_id") + int insertNameAnnotated(Name name); + + int insertNameMapped(Name name); +} diff --git a/docs/spring/cs778962a4-250b-11ee-9e02-acde48001122.java b/docs/spring/cs778962a4-250b-11ee-9e02-acde48001122.java new file mode 100644 index 00000000..c7b0c9a7 --- /dev/null +++ b/docs/spring/cs778962a4-250b-11ee-9e02-acde48001122.java @@ -0,0 +1,57 @@ +package com.huifer.design.template.dao; + +import com.huifer.design.template.JdbcTemplate; +import com.huifer.design.template.RowMapper; +import com.huifer.design.template.entity.Menber; +import org.springframework.jdbc.datasource.DriverManagerDataSource; + +import javax.sql.DataSource; +import java.sql.ResultSet; +import java.util.List; + +/** + *

Title : MenberDao

+ *

Description :

+ * + * @author huifer + * @date 2019-05-20 + */ +public class MenberDao { + + private static final String driverClassName = "com.mysql.cj.jdbc.Driver"; + private static final String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8"; + private static final String dbUsername = "root"; + private static final String dbPassword = "root"; + private JdbcTemplate jdbcTemplate = new JdbcTemplate(getDatasource()); + + public static void main(String[] args) throws Exception { + + MenberDao menberDao = new MenberDao(); + List query = menberDao.query(); + System.out.println(); + } + + private static DataSource getDatasource() { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(driverClassName); + dataSource.setUrl(url); + dataSource.setUsername(dbUsername); + dataSource.setPassword(dbPassword); + return dataSource; + } + + + public List query() throws Exception { + String sql = "select * from t_menber"; + return jdbcTemplate.executeQuery(sql, new RowMapper() { + @Override + public Menber mapRow(ResultSet rs, int rowNum) throws Exception { + Menber menber = new Menber(); + menber.setName(rs.getString("name")); + menber.setPwd(rs.getString("pwd")); + return menber; + } + }, new Object[]{}); + } + +} diff --git a/docs/spring/cs78244026-250b-11ee-9e02-acde48001122.java b/docs/spring/cs78244026-250b-11ee-9e02-acde48001122.java new file mode 100644 index 00000000..c4540bde --- /dev/null +++ b/docs/spring/cs78244026-250b-11ee-9e02-acde48001122.java @@ -0,0 +1,88 @@ +package com.huifer.design.template; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + *

Title : JdbcTemplate

+ *

Description :

+ * + * @author huifer + * @date 2019-05-20 + */ +public class JdbcTemplate { + + private DataSource dataSource; + + public JdbcTemplate(DataSource dataSource) { + this.dataSource = dataSource; + } + + private List parseResultSet(ResultSet rs, RowMapper rowMapper) throws Exception { + List result = new ArrayList<>(); + int rowNum = 1; + while (rs.next()) { + result.add(rowMapper.mapRow(rs, rowNum++)); + } + return result; + + } + + public List executeQuery(String sql, RowMapper rowMapper, Object[] value) { + try { + + // 1.获取连接 + Connection conn = this.getConn(); + // 2.创建sql + PreparedStatement pstm = this.createpstm(sql, conn); + // 3.执行查询 获得结果 + ResultSet rst = this.executeQuery(pstm, value); + // 4.解析结果 + List resList = this.parseResultSet(rst, rowMapper); + + // 5.关闭 + resultsetClose(rst); + pstmClose(pstm); + connClose(conn); + return resList; + } catch (Exception e) { + e.printStackTrace(); + + } + return null; + } + + private void connClose(Connection conn) throws SQLException { + conn.close(); + } + + private void pstmClose(PreparedStatement pstm) throws SQLException { + pstm.close(); + } + + private void resultsetClose(ResultSet rst) throws SQLException { + rst.close(); + } + + private ResultSet executeQuery(PreparedStatement pstm, Object[] value) throws SQLException { + for (int i = 0; i < value.length; i++) { + pstm.setObject(i, value[i]); + } + return pstm.executeQuery(); + } + + private PreparedStatement createpstm(String sql, Connection conn) throws SQLException { + return conn.prepareStatement(sql); + } + + private Connection getConn() throws SQLException { + return dataSource.getConnection(); + } + + +} diff --git a/docs/spring/cs78d58534-250b-11ee-9e02-acde48001122.java b/docs/spring/cs78d58534-250b-11ee-9e02-acde48001122.java new file mode 100644 index 00000000..d71289bf --- /dev/null +++ b/docs/spring/cs78d58534-250b-11ee-9e02-acde48001122.java @@ -0,0 +1,85 @@ +/** + * 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.builder.xml; + +import org.apache.ibatis.io.Resources; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Locale; + +/** + * Offline entity resolver for the MyBatis DTDs. + * 离线加载dtd文件 + * @author Clinton Begin + * @author Eduardo Macarron + */ +public class XMLMapperEntityResolver implements EntityResolver { + + private static final String IBATIS_CONFIG_SYSTEM = "ibatis-3-config.dtd"; + private static final String IBATIS_MAPPER_SYSTEM = "ibatis-3-mapper.dtd"; + private static final String MYBATIS_CONFIG_SYSTEM = "mybatis-3-config.dtd"; + private static final String MYBATIS_MAPPER_SYSTEM = "mybatis-3-mapper.dtd"; + + private static final String MYBATIS_CONFIG_DTD = "org/apache/ibatis/builder/xml/mybatis-3-config.dtd"; + private static final String MYBATIS_MAPPER_DTD = "org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd"; + + /** + * Converts a public DTD into a local one. + * + * @param publicId The public id that is what comes after "PUBLIC" + * @param systemId The system id that is what comes after the public id. + * @return The InputSource for the DTD + * + * @throws org.xml.sax.SAXException If anything goes wrong + * 加载 jar 中的文件判断是否出现异常 (是否符合xml解析规则) + */ + @Override + public InputSource resolveEntity(String publicId, String systemId) throws SAXException { + try { + if (systemId != null) { + String lowerCaseSystemId = systemId.toLowerCase(Locale.ENGLISH); + if (lowerCaseSystemId.contains(MYBATIS_CONFIG_SYSTEM) || lowerCaseSystemId.contains(IBATIS_CONFIG_SYSTEM)) { + return getInputSource(MYBATIS_CONFIG_DTD, publicId, systemId); + } else if (lowerCaseSystemId.contains(MYBATIS_MAPPER_SYSTEM) || lowerCaseSystemId.contains(IBATIS_MAPPER_SYSTEM)) { + return getInputSource(MYBATIS_MAPPER_DTD, publicId, systemId); + } + } + return null; + } catch (Exception e) { + throw new SAXException(e.toString()); + } + } + + private InputSource getInputSource(String path, String publicId, String systemId) { + InputSource source = null; + if (path != null) { + try { + InputStream in = Resources.getResourceAsStream(path); + source = new InputSource(in); + source.setPublicId(publicId); + source.setSystemId(systemId); + } catch (IOException e) { + // ignore, null is ok + } + } + return source; + } + +} diff --git a/docs/spring/cs79b33668-250b-11ee-9e02-acde48001122.java b/docs/spring/cs79b33668-250b-11ee-9e02-acde48001122.java new file mode 100644 index 00000000..c25a4dea --- /dev/null +++ b/docs/spring/cs79b33668-250b-11ee-9e02-acde48001122.java @@ -0,0 +1,50 @@ +package com.huifer.jdk.random; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.TreeMap; + +/** + * 权重随机 + */ +public class WeightRandom { + + + + public static void main(String[] args) { + int[] weightArray = new int[]{1, 2, 3, 4, 5}; + TreeMap treeMap = new TreeMap(); + + int key = 0; + for (int weight : weightArray) { + treeMap.put(key, weight); + key += weight; + } + + Random r = new Random(); + int num; + int store; + System.out.println("简单展示一下效果:"); + for (int i = 0; i < 10; i++) { + num = r.nextInt(key); + store = treeMap.floorEntry(num).getValue(); + System.out.printf(" num: %d, result: %d \n", num, store); + } + + System.out.println("\n统计概率:"); + int[] storeStat = new int[weightArray.length + 1]; + for (int i = 0; i < 10000; i++) { + num = r.nextInt(key); + store = treeMap.floorEntry(num).getValue(); + storeStat[store]++; + } + + for (int i = 1; i < storeStat.length; i++) { + double result = storeStat[i] / 10000.0; + System.out.printf(" store %d: %f\n", i, result); + } + + } + +}