diff --git a/docs/spring/cs0df1635c-6a5e-11ef-8f2f-acde48001122.java b/docs/spring/cs0df1635c-6a5e-11ef-8f2f-acde48001122.java new file mode 100644 index 00000000..3f45c1f6 --- /dev/null +++ b/docs/spring/cs0df1635c-6a5e-11ef-8f2f-acde48001122.java @@ -0,0 +1,79 @@ +/** + * 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.result_handler_type; + +import org.apache.ibatis.BaseDataTest; +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 org.junit.jupiter.api.Test; + +import java.io.Reader; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class DefaultResultHandlerTypeTest { + + @Test + void testSelectList() throws Exception { + String xmlConfig = "org/apache/ibatis/submitted/result_handler_type/MapperConfig.xml"; + SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig); + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + List list = sqlSession + .selectList("org.apache.ibatis.submitted.result_handler_type.PersonMapper.doSelect"); + assertEquals(list.size(), 2); + assertEquals("java.util.LinkedList", list.getClass().getCanonicalName()); + } + } + + @Test + void testSelectMap() throws Exception { + String xmlConfig = "org/apache/ibatis/submitted/result_handler_type/MapperConfig.xml"; + SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig); + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Map map = sqlSession.selectMap( + "org.apache.ibatis.submitted.result_handler_type.PersonMapper.doSelect", "id"); + assertEquals(map.size(), 2); + assertEquals("java.util.LinkedHashMap", map.getClass().getCanonicalName()); + } + } + + @Test + void testSelectMapAnnotation() throws Exception { + String xmlConfig = "org/apache/ibatis/submitted/result_handler_type/MapperConfig.xml"; + SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryXmlConfig(xmlConfig); + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + PersonMapper mapper = sqlSession.getMapper(PersonMapper.class); + Map map = mapper.selectAsMap(); + assertEquals(map.size(), 2); + assertEquals("java.util.LinkedHashMap", map.getClass().getCanonicalName()); + } + } + + private SqlSessionFactory getSqlSessionFactoryXmlConfig(String resource) throws Exception { + try (Reader configReader = Resources.getResourceAsReader(resource)) { + SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader); + BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), + "org/apache/ibatis/submitted/result_handler_type/CreateDB.sql"); + + return sqlSessionFactory; + } + } + +} diff --git a/docs/spring/cs0e6cb700-6a5e-11ef-8f2f-acde48001122.java b/docs/spring/cs0e6cb700-6a5e-11ef-8f2f-acde48001122.java new file mode 100644 index 00000000..4cec21d3 --- /dev/null +++ b/docs/spring/cs0e6cb700-6a5e-11ef-8f2f-acde48001122.java @@ -0,0 +1,41 @@ +package com.huifer.fzjh.service; + +import com.huifer.fzjh.bean.RequestEntity; +import com.huifer.fzjh.bean.ServerWeight; +import com.huifer.fzjh.exception.LoadBalanceException; +import lombok.extern.slf4j.Slf4j; + +import java.util.List; +import java.util.Random; + +/** + * 负载均衡算法实现: 按照随机 + * 直接随机一个机器 + */ +@Slf4j +public class RandomLoadBalance extends AbstractLoadBalance { + private int count = -1; + private RequestEntity requestEntity; + private List serverWeights; + + public RandomLoadBalance(RequestEntity requestEntity, List serverWeights) { + super(requestEntity, serverWeights); + this.count = serverWeights.size(); + this.requestEntity = requestEntity; + this.serverWeights = serverWeights; + } + + + @Override + public String loadBalance() { + if (count < 0) { + throw new LoadBalanceException("机器数量不能小于0"); + } + + Random random = new Random(); + int machineId = random.nextInt(count); + ServerWeight serverWeight = serverWeights.get(machineId); + log.info("当前请求信息={},负载均衡计算后的机器ip={},端口={}", requestEntity, serverWeight.getIp(), serverWeight.getPort()); + return serverWeight.getIp() + ":" + serverWeight.getPort(); + } +} diff --git a/docs/spring/cs0ee04dd2-6a5e-11ef-8f2f-acde48001122.java b/docs/spring/cs0ee04dd2-6a5e-11ef-8f2f-acde48001122.java new file mode 100644 index 00000000..898d3c8d --- /dev/null +++ b/docs/spring/cs0ee04dd2-6a5e-11ef-8f2f-acde48001122.java @@ -0,0 +1,35 @@ +package org.huifer.rbac.service; + +import org.huifer.rbac.entity.db.Demo; +import org.huifer.rbac.mapper.DemoMapper; + +import org.springframework.stereotype.Service; + +@Service +public class DemoService { + + final + DemoMapper demoMapper; + + public DemoService(DemoMapper demoMapper) { + this.demoMapper = demoMapper; + } + + + public void lgs() { + Demo demo = new Demo(); + demo.setName("zs"); + demoMapper.insert(demo); + } + + public void update() { + Demo demo = demoMapper.selectById(2L); + demo.setName("aklsfj"); + demoMapper.updateById(demo); + + } + + public void delete() { + demoMapper.deleteById(2L); + } +} diff --git a/docs/spring/cs0f879830-6a5e-11ef-8f2f-acde48001122.java b/docs/spring/cs0f879830-6a5e-11ef-8f2f-acde48001122.java new file mode 100644 index 00000000..e6739a7f --- /dev/null +++ b/docs/spring/cs0f879830-6a5e-11ef-8f2f-acde48001122.java @@ -0,0 +1,78 @@ +package com.github.huifer.full.shiro.service.impl; + +import com.github.huifer.full.shiro.dao.ShiroDeptDao; +import com.github.huifer.full.shiro.entity.ShiroDept; +import com.github.huifer.full.shiro.ex.ServerEx; +import com.github.huifer.full.shiro.model.req.dept.DeptCreateParam; +import com.github.huifer.full.shiro.service.DeptService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +@Service +public class DeptServiceImpl implements DeptService { + + @Autowired + private ShiroDeptDao shiroDeptDao; + + @Override + public boolean create(DeptCreateParam param) { + String name = param.getName(); + if (!StringUtils.hasText(name)) { + throw new ServerEx(""); + } + Integer companyId = param.getCompanyId(); + if (companyId == null) { + throw new ServerEx(""); + } + ShiroDept byName = this.shiroDeptDao.findByName(name); + if (byName != null) { + throw new ServerEx(""); + } + ShiroDept ins = new ShiroDept(); + ins.setCompanyId(param.getCompanyId()); + ins.setName(param.getName()); + ins.setMainUser(param.getMainUser()); + ins.setPid(param.getPid()); + + return this.shiroDeptDao.insert(ins) > 0; + } + + @Override + public boolean update(DeptCreateParam param, int id) { + ShiroDept shiroDept = this.shiroDeptDao.selectById(id); + if (shiroDept != null) { + + String name = param.getName(); + if (!StringUtils.hasText(name)) { + throw new ServerEx(""); + } + Integer companyId = param.getCompanyId(); + if (companyId == null) { + throw new ServerEx(""); + } + if (!shiroDept.getName().equals(name)) { + + ShiroDept byName = this.shiroDeptDao.findByName(name); + if (byName != null) { + throw new ServerEx(""); + } + shiroDept.setName(param.getName()); + } + shiroDept.setMainUser(param.getMainUser()); + shiroDept.setPid(param.getPid()); + return shiroDeptDao.updateById(shiroDept) > 0; + } + return false; + } + + @Override + public boolean delete(int id) { + return this.shiroDeptDao.deleteById(id) > 0; + } + + @Override + public ShiroDept byId(int id) { + return this.shiroDeptDao.selectById(id); + } +} diff --git a/docs/spring/cs1061cece-6a5e-11ef-8f2f-acde48001122.java b/docs/spring/cs1061cece-6a5e-11ef-8f2f-acde48001122.java new file mode 100644 index 00000000..e48ba508 --- /dev/null +++ b/docs/spring/cs1061cece-6a5e-11ef-8f2f-acde48001122.java @@ -0,0 +1,116 @@ +/** + * 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.named_constructor_args; + +import org.apache.ibatis.BaseDataTest; +import org.apache.ibatis.annotations.Arg; +import org.apache.ibatis.annotations.ConstructorArgs; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.builder.BuilderException; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.Reader; + +import static com.googlecode.catchexception.apis.BDDCatchException.caughtException; +import static com.googlecode.catchexception.apis.BDDCatchException.when; +import static org.assertj.core.api.BDDAssertions.then; + +class InvalidNamedConstructorArgsTest { + + private static SqlSessionFactory sqlSessionFactory; + + @BeforeAll + static void setUp() throws Exception { + // create an SqlSessionFactory + try (Reader reader = Resources.getResourceAsReader( + "org/apache/ibatis/submitted/named_constructor_args/mybatis-config.xml")) { + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); + } + + // populate in-memory database + BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), + "org/apache/ibatis/submitted/named_constructor_args/CreateDB.sql"); + } + + @Test + void noMatchingConstructorArgName() { + Configuration configuration = sqlSessionFactory.getConfiguration(); + when(() -> configuration.addMapper(NoMatchingConstructorMapper.class)); + + then(caughtException()).isInstanceOf(BuilderException.class) + .hasMessageContaining( + "'org.apache.ibatis.submitted.named_constructor_args.InvalidNamedConstructorArgsTest$NoMatchingConstructorMapper.select-void'") + .hasMessageContaining("'org.apache.ibatis.submitted.named_constructor_args.User'") + .hasMessageContaining("[noSuchConstructorArg]"); + } + + @Test + void wrongJavaType() { + Configuration configuration = sqlSessionFactory.getConfiguration(); + when(() -> configuration.addMapper(ConstructorWithWrongJavaType.class)); + then(caughtException()).isInstanceOf(BuilderException.class) + .hasMessageContaining( + "'org.apache.ibatis.submitted.named_constructor_args.InvalidNamedConstructorArgsTest$ConstructorWithWrongJavaType.select-void'") + .hasMessageContaining("'org.apache.ibatis.submitted.named_constructor_args.User'") + .hasMessageContaining("[id]"); + } + + @Test + void missingRequiredJavaType() { + Configuration configuration = sqlSessionFactory.getConfiguration(); + when(() -> configuration.addMapper(ConstructorMissingRequiresJavaType.class)); + then(caughtException()).isInstanceOf(BuilderException.class) + .hasMessageContaining( + "'org.apache.ibatis.submitted.named_constructor_args.InvalidNamedConstructorArgsTest$ConstructorMissingRequiresJavaType.select-void'") + .hasMessageContaining("'org.apache.ibatis.submitted.named_constructor_args.User'") + .hasMessageContaining("[id]"); + } + + interface NoMatchingConstructorMapper { + @ConstructorArgs({ + @Arg(column = "id", name = "noSuchConstructorArg"), + }) + @Select("select * from users ") + User select(); + } + + interface ConstructorWithWrongJavaType { + // There is a constructor with arg name 'id', but + // its type is different from the specified javaType. + @ConstructorArgs({ + @Arg(column = "id", name = "id", javaType = Integer.class), + }) + @Select("select * from users ") + User select(); + } + + interface ConstructorMissingRequiresJavaType { + // There is a constructor with arg name 'id', but its type + // is different from the type of a property with the same name. + // javaType is required in this case. + // Debug log shows the detail of the matching error. + @ConstructorArgs({ + @Arg(column = "id", name = "id"), + }) + @Select("select * from users ") + User select(); + } +} diff --git a/docs/spring/cs1134f8a8-6a5e-11ef-8f2f-acde48001122.java b/docs/spring/cs1134f8a8-6a5e-11ef-8f2f-acde48001122.java new file mode 100644 index 00000000..72ddd356 --- /dev/null +++ b/docs/spring/cs1134f8a8-6a5e-11ef-8f2f-acde48001122.java @@ -0,0 +1,141 @@ +/** + * 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.logging; + +import java.lang.reflect.Constructor; + +/** + *

日志工厂,实现内容:

+ *
    + *
  1. org.slf4j.Logger 日志框架 slf4j
  2. + *
  3. org.apache.commons.logging.Log 日志框架 apache
  4. + *
  5. org.apache.logging.log4j.Logger 日志框架 log4j2
  6. + *
  7. org.apache.log4j.Logger 日志框架 log4j
  8. + *
  9. java.util.logging.Logger 日志框架,JDK的logger
  10. + * + *
+ * @author Clinton Begin + * @author Eduardo Macarron + */ +public final class LogFactory { + + /** + * Marker to be used by logging implementations that support markers. + */ + public static final String MARKER = "MYBATIS"; + + private static Constructor logConstructor; + + /** + * 日志的实现类的具体选择 + */ + static { + // slf4j 日志 + tryImplementation(LogFactory::useSlf4jLogging); + // apache 日志 + tryImplementation(LogFactory::useCommonsLogging); + // log4j2 日志 + tryImplementation(LogFactory::useLog4J2Logging); + // log4 日志 + tryImplementation(LogFactory::useLog4JLogging); + // JDK 日志 + tryImplementation(LogFactory::useJdkLogging); + // 空 日志 + tryImplementation(LogFactory::useNoLogging); + } + + /** + * 私有化构造方法,这是一个单例 + */ + private LogFactory() { + // disable construction + } + + public static Log getLog(Class aClass) { + return getLog(aClass.getName()); + } + + public static Log getLog(String logger) { + try { + return logConstructor.newInstance(logger); + } catch (Throwable t) { + throw new LogException("Error creating logger for logger " + logger + ". Cause: " + t, t); + } + } + + public static synchronized void useCustomLogging(Class clazz) { + setImplementation(clazz); + } + + public static synchronized void useSlf4jLogging() { + setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class); + } + + public static synchronized void useCommonsLogging() { + setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class); + } + + public static synchronized void useLog4JLogging() { + setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class); + } + + public static synchronized void useLog4J2Logging() { + setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class); + } + + public static synchronized void useJdkLogging() { + setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class); + } + + public static synchronized void useStdOutLogging() { + setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class); + } + + public static synchronized void useNoLogging() { + setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class); + } + + /** + * 选择具体的日志实现 + */ + private static void tryImplementation(Runnable runnable) { + if (logConstructor == null) { + try { + // run()? 似乎违背了代码的语义, 看静态方法.静态方法多行同类型的操作我认为是一个多线程 + runnable.run(); + } catch (Throwable t) { + // ignore + } + } + } + + /** + * 选择具体的日志实现 + */ + private static void setImplementation(Class implClass) { + try { + Constructor candidate = implClass.getConstructor(String.class); + Log log = candidate.newInstance(LogFactory.class.getName()); + if (log.isDebugEnabled()) { + log.debug("Logging initialized using '" + implClass + "' adapter."); + } + logConstructor = candidate; + } catch (Throwable t) { + throw new LogException("Error setting Log implementation. Cause: " + t, t); + } + } + +} diff --git a/docs/spring/cs1278cad2-6a5e-11ef-8f2f-acde48001122.java b/docs/spring/cs1278cad2-6a5e-11ef-8f2f-acde48001122.java new file mode 100644 index 00000000..a1361b56 --- /dev/null +++ b/docs/spring/cs1278cad2-6a5e-11ef-8f2f-acde48001122.java @@ -0,0 +1,46 @@ +/** + * 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.submitted.flush_statement_npe; + +public class Person { + private Integer id; + private String firstName; + private String lastName; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } +} diff --git a/docs/spring/cs13c3275c-6a5e-11ef-8f2f-acde48001122.java b/docs/spring/cs13c3275c-6a5e-11ef-8f2f-acde48001122.java new file mode 100644 index 00000000..70ce0175 --- /dev/null +++ b/docs/spring/cs13c3275c-6a5e-11ef-8f2f-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.submitted.collectionparameters; + +import org.apache.ibatis.BaseDataTest; +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 org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.Reader; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +class CollectionParametersTest { + + private static SqlSessionFactory sqlSessionFactory; + + @BeforeAll + static void setUp() throws Exception { + // create an SqlSessionFactory + try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/collectionparameters/mybatis-config.xml")) { + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); + } + + // populate in-memory database + BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), + "org/apache/ibatis/submitted/collectionparameters/CreateDB.sql"); + } + + @Test + void shouldGetTwoUsersPassingAList() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Mapper mapper = sqlSession.getMapper(Mapper.class); + ArrayList list = new ArrayList<>(); + list.add(1); + list.add(2); + List users = mapper.getUsersFromList(list); + Assertions.assertEquals(2, users.size()); + } + } + + @Test + void shouldGetTwoUsersPassingAnArray() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Mapper mapper = sqlSession.getMapper(Mapper.class); + Integer[] list = new Integer[2]; + list[0] = 1; + list[1] = 2; + List users = mapper.getUsersFromArray(list); + Assertions.assertEquals(2, users.size()); + } + } + + @Test + void shouldGetTwoUsersPassingACollection() { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Mapper mapper = sqlSession.getMapper(Mapper.class); + Set list = new HashSet<>(); + list.add(1); + list.add(2); + List users = mapper.getUsersFromCollection(list); + Assertions.assertEquals(2, users.size()); + } + } + +}