提交 5af1fa21 编写于 作者: M MaxKey

update

上级 6806c39b
......@@ -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;
}
}
......@@ -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 <T extends JpaBaseEntity> {
public IJpaBaseMapper<T> 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<T>) JpaWebContext.getBean(mapperClassBean);
mapper = (IJpaBaseMapper<T>) MybatisJpaContext.getBean(mapperClassBean);
}
} catch(Exception e) {
_logger.error("getMapper Exception " , e);
......@@ -192,7 +192,7 @@ public class JpaBaseService <T extends JpaBaseEntity> {
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 <T extends JpaBaseEntity> {
public List<T> 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 <T extends JpaBaseEntity> {
@SuppressWarnings("unchecked")
public List<T> 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);
}
......
......@@ -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> T getBean(String name, Class<T> 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 =
......
......@@ -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<String> string2List(String string, String split) {
String[] strs = {};
......
......@@ -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();
......
......@@ -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;
}
......
......@@ -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;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册