Skip to content

Commit

Permalink
write a line into test.file
Browse files Browse the repository at this point in the history
  • Loading branch information
huifer committed Jul 18, 2023
1 parent 5230f32 commit 0118b48
Show file tree
Hide file tree
Showing 6 changed files with 367 additions and 0 deletions.
55 changes: 55 additions & 0 deletions docs/spring/cs76b90c8a-250b-11ee-9e02-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright 2009-2018 the original author or authors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
}

}
32 changes: 32 additions & 0 deletions docs/spring/cs7729dd48-250b-11ee-9e02-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright 2009-2018 the original author or authors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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);
}
57 changes: 57 additions & 0 deletions docs/spring/cs778962a4-250b-11ee-9e02-acde48001122.java
Original file line number Diff line number Diff line change
@@ -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;

/**
* <p>Title : MenberDao </p>
* <p>Description : </p>
*
* @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<Object> 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<Object> query() throws Exception {
String sql = "select * from t_menber";
return jdbcTemplate.executeQuery(sql, new RowMapper<Menber>() {
@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[]{});
}

}
88 changes: 88 additions & 0 deletions docs/spring/cs78244026-250b-11ee-9e02-acde48001122.java
Original file line number Diff line number Diff line change
@@ -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;

/**
* <p>Title : JdbcTemplate </p>
* <p>Description : </p>
*
* @author huifer
* @date 2019-05-20
*/
public class JdbcTemplate {

private DataSource dataSource;

public JdbcTemplate(DataSource dataSource) {
this.dataSource = dataSource;
}

private List<Object> parseResultSet(ResultSet rs, RowMapper<?> rowMapper) throws Exception {
List<Object> result = new ArrayList<>();
int rowNum = 1;
while (rs.next()) {
result.add(rowMapper.mapRow(rs, rowNum++));
}
return result;

}

public List<Object> 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<Object> 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();
}


}
85 changes: 85 additions & 0 deletions docs/spring/cs78d58534-250b-11ee-9e02-acde48001122.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright 2009-2019 the original author or authors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
}

}
50 changes: 50 additions & 0 deletions docs/spring/cs79b33668-250b-11ee-9e02-acde48001122.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> treeMap = new TreeMap<Integer, Integer>();

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);
}

}

}

0 comments on commit 0118b48

Please sign in to comment.