diff --git a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/PageResultsSqlCache.java b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/PageResultsSqlCache.java index 16185490eef1136b231d082e46a3dc38bd043cd8..6319a43a9355247b05ca89d5bace120d0c806b9f 100644 --- a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/PageResultsSqlCache.java +++ b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/PageResultsSqlCache.java @@ -20,7 +20,9 @@ package org.apache.mybatis.jpa; import org.apache.ibatis.mapping.BoundSql; public class PageResultsSqlCache { + String sql; + BoundSql boundSql; @@ -29,18 +31,21 @@ public class PageResultsSqlCache { this.sql = sql; this.boundSql = boundSql; } - + public String getSql() { return sql; } + public void setSql(String sql) { this.sql = sql; } + public BoundSql getBoundSql() { return boundSql; } + public void setBoundSql(BoundSql boundSql) { this.boundSql = boundSql; } - + } diff --git a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/JpaBaseService.java b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/JpaBaseService.java index fd6cbe03e604020d0f72b952fbbfcaebe6c2644c..3b8b95e998e328baffe176ccb82d6853a17c5222 100644 --- a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/JpaBaseService.java +++ b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/persistence/JpaBaseService.java @@ -29,7 +29,7 @@ import org.apache.mybatis.jpa.query.Query; import org.apache.mybatis.jpa.util.BeanUtil; import org.apache.mybatis.jpa.util.InstanceUtil; import org.apache.mybatis.jpa.util.StringUtils; -import org.apache.mybatis.jpa.util.JpaWebContext; +import org.apache.mybatis.jpa.util.MybatisJpaContext; import com.fasterxml.jackson.annotation.JsonIgnore; import com.github.benmanes.caffeine.cache.Cache; @@ -112,9 +112,9 @@ public class JpaBaseService { public IJpaBaseMapper getMapper() { try { if(mapper == null) { - String mapperClassBean = mapperClass.toLowerCase().charAt(0) + mapperClass.substring(1); + String mapperClassBean = StringUtils.firstToLowerCase(mapperClass); _logger.info("mapperClass Bean is {}" , mapperClassBean); - mapper = (IJpaBaseMapper) JpaWebContext.getBean(mapperClassBean); + mapper = (IJpaBaseMapper) MybatisJpaContext.getBean(mapperClassBean); } } catch(Exception e) { _logger.error("getMapper Exception " , e); @@ -192,7 +192,7 @@ public class JpaBaseService { Integer count = 0; try { if(entity == null) { - entity = (T) entityClass.newInstance(); + entity = (T) entityClass.getDeclaredConstructor().newInstance(); } count = getMapper().queryPageResultsCount(entity); _logger.debug("queryCount count : {}" , count); @@ -211,7 +211,7 @@ public class JpaBaseService { public List query(T entity) { try { if(entity == null) { - entity = (T) entityClass.newInstance(); + entity = (T) entityClass.getDeclaredConstructor().newInstance(); } return getMapper().query(entity); } catch(Exception e) { @@ -228,7 +228,7 @@ public class JpaBaseService { @SuppressWarnings("unchecked") public List query(Query query) { try { - return getMapper().filterByQuery((T)entityClass.newInstance(),query); + return getMapper().filterByQuery((T)entityClass.getDeclaredConstructor().newInstance(),query); } catch(Exception e) { _logger.error("query Exception " , e); } diff --git a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/util/JpaWebContext.java b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/util/MybatisJpaContext.java similarity index 78% rename from mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/util/JpaWebContext.java rename to mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/util/MybatisJpaContext.java index ccac093de79372ee2e177af3540f3621567ba360..857d9d144ec98d03278cef259af11232d4b098ce 100644 --- a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/util/JpaWebContext.java +++ b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/util/MybatisJpaContext.java @@ -19,6 +19,7 @@ package org.apache.mybatis.jpa.util; import org.apache.commons.lang.SystemUtils; import org.joda.time.DateTime; +import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.core.env.StandardEnvironment; import org.springframework.web.context.request.RequestContextHolder; @@ -36,13 +37,13 @@ import jakarta.servlet.http.HttpSession; * @author Crystal.Sea * @since 1.6 */ -public final class JpaWebContext { +public final class MybatisJpaContext { private static String VERSION = null; public static StandardEnvironment properties; - public static ApplicationContext applicationContext=null; + public static ApplicationContext applicationContext = null; /** * get ApplicationContext from web ServletContext configuration @@ -58,13 +59,20 @@ public final class JpaWebContext { * @return Object */ public static Object getBean(String id){ - if(applicationContext==null) { + if(applicationContext == null) { return getApplicationContext().getBean(id); }else { return applicationContext.getBean(id); } } + public static T getBean(String name, Class requiredType) throws BeansException{ + if(applicationContext == null) { + return getApplicationContext().getBean(name,requiredType); + }else { + return applicationContext.getBean(name,requiredType); + } + }; //below method is common HttpServlet method /** @@ -75,7 +83,6 @@ public final class JpaWebContext { return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); } - /** * get current Session * @return HttpSession @@ -83,51 +90,7 @@ public final class JpaWebContext { public static HttpSession getSession(){ return getRequest().getSession(); } - - /** - * get current Session,if no session ,new Session created - * @return HttpSession - */ - public static HttpSession getSession(boolean create) { - return getRequest().getSession(create); - } - - /** - * set Attribute to session ,Attribute name is name,value is value - * @param name - * @param value - */ - public static void setAttribute(String name,Object value){ - getSession().setAttribute(name, value); - } - - /** - * get Attribute from session by name - * @param name - * @return - */ - public static Object getAttribute(String name){ - return getSession().getAttribute(name); - } - - /** - * remove Attribute from session by name - * @param name - */ - public static void removeAttribute(String name){ - getSession().removeAttribute(name); - } - - /** - * get Request Parameter by name - * @param name - * @return String - */ - public static String getParameter(String name){ - return getRequest().getParameter(name); - } - public static String version() { if(VERSION == null) { StringBuffer version = diff --git a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/util/StringUtils.java b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/util/StringUtils.java index 252e2ba08388c2ad35b373f50a9ec624cd05516a..c861164c678a214adbe21fc8bf368fc9cd83b9b1 100644 --- a/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/util/StringUtils.java +++ b/mybatis-jpa-extra-core/src/main/java/org/apache/mybatis/jpa/util/StringUtils.java @@ -54,6 +54,10 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils { } return num; } + + public static String firstToLowerCase(String str) { + return str.toLowerCase().charAt(0) + str.substring(1); + } public static List string2List(String string, String split) { String[] strs = {}; diff --git a/mybatis-jpa-extra-spring-boot-starter-test/src/test/java/org/apache/mybatis/jpa/test/MybatisJpaApplication.java b/mybatis-jpa-extra-spring-boot-starter-test/src/test/java/org/apache/mybatis/jpa/test/MybatisJpaApplication.java index 0f5e4f1a52e0e861a081b9e1efa881b43ac06f0d..6e89294d7bda2aebbc3a64dfb0f9aa7969acbd23 100644 --- a/mybatis-jpa-extra-spring-boot-starter-test/src/test/java/org/apache/mybatis/jpa/test/MybatisJpaApplication.java +++ b/mybatis-jpa-extra-spring-boot-starter-test/src/test/java/org/apache/mybatis/jpa/test/MybatisJpaApplication.java @@ -21,7 +21,7 @@ import org.apache.mybatis.jpa.id.SerialGenerator; import org.apache.mybatis.jpa.persistence.JpaPageResults; import org.apache.mybatis.jpa.test.dao.service.StudentsService; import org.apache.mybatis.jpa.test.entity.Students; -import org.apache.mybatis.jpa.util.JpaWebContext; +import org.apache.mybatis.jpa.util.MybatisJpaContext; import org.mybatis.spring.annotation.MapperScan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,7 +59,7 @@ public class MybatisJpaApplication implements ApplicationRunner{ @Override public void run(ApplicationArguments args) throws Exception { - JpaWebContext.applicationContext=applicationContext; + MybatisJpaContext.applicationContext=applicationContext; _logger.info("queryPageResults by mapperId..."); Students student=new Students(); diff --git a/mybatis-jpa-extra-spring-boot-starter-test/src/test/java/org/apache/mybatis/jpa/test/MybatisJpaApplicationTest.java b/mybatis-jpa-extra-spring-boot-starter-test/src/test/java/org/apache/mybatis/jpa/test/MybatisJpaApplicationTest.java index 5051ba0983ec68c0d261a652d86dc6aa10510794..5ddd996080ab0e6e7130426c616dd982b10a63b5 100644 --- a/mybatis-jpa-extra-spring-boot-starter-test/src/test/java/org/apache/mybatis/jpa/test/MybatisJpaApplicationTest.java +++ b/mybatis-jpa-extra-spring-boot-starter-test/src/test/java/org/apache/mybatis/jpa/test/MybatisJpaApplicationTest.java @@ -21,7 +21,7 @@ import java.util.ArrayList; import java.util.List; import org.apache.mybatis.jpa.test.dao.service.StudentsService; import org.apache.mybatis.jpa.test.entity.Students; -import org.apache.mybatis.jpa.util.JpaWebContext; +import org.apache.mybatis.jpa.util.MybatisJpaContext; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -47,7 +47,7 @@ public class MybatisJpaApplicationTest{ @BeforeEach public void before() { _logger.info("---------------- before"); - JpaWebContext.applicationContext = applicationContext; + MybatisJpaContext.applicationContext = applicationContext; } diff --git a/mybatis-jpa-extra-test/src/test/java/org/apache/mybatis/jpa/test/InitContext.java b/mybatis-jpa-extra-test/src/test/java/org/apache/mybatis/jpa/test/InitContext.java index 3847b0474fb692a3b036527850761ed654ec50b6..8e1be6637e2c863b8c6b776d025981bcc9e36bb3 100644 --- a/mybatis-jpa-extra-test/src/test/java/org/apache/mybatis/jpa/test/InitContext.java +++ b/mybatis-jpa-extra-test/src/test/java/org/apache/mybatis/jpa/test/InitContext.java @@ -4,7 +4,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import org.apache.mybatis.jpa.test.dao.service.StudentsService; -import org.apache.mybatis.jpa.util.JpaWebContext; +import org.apache.mybatis.jpa.util.MybatisJpaContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; @@ -23,8 +23,8 @@ public class InitContext { _logger.info("Application dir "+System.getProperty("user.dir")); context = new ClassPathXmlApplicationContext(new String[] {"spring/applicationContext.xml"}); - JpaWebContext.applicationContext=context; - StudentsService service =(StudentsService)JpaWebContext.getBean("studentsService"); + MybatisJpaContext.applicationContext=context; + StudentsService service =(StudentsService)MybatisJpaContext.getBean("studentsService"); return service; } }