mybatisUtils加入使用默认参数的工厂函数。
This commit is contained in:
		@@ -21,13 +21,14 @@ public class MybatisUtils
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    private static HashMap<String, SqlSession> SessionMap = new HashMap<>();
 | 
					    private static HashMap<String, SqlSession> SessionMap = new HashMap<>();
 | 
				
			||||||
    private static String DEFAULT_CONFIGFILE = "mybatis/mybatis-config.xml";
 | 
					    private static String DEFAULT_CONFIGFILE = "mybatis/mybatis-config.xml";
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * 
 | 
					     * 
 | 
				
			||||||
     * @param configFile
 | 
					     * @param configFile
 | 
				
			||||||
     * @return
 | 
					     * @return
 | 
				
			||||||
     * @throws IOException
 | 
					     * @throws IOException
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static SqlSession getSqlSession(String configFile = "mybatis/mybatis-config.xml" )
 | 
					    public static SqlSession getSqlSession(String configFile )
 | 
				
			||||||
    throws IOException
 | 
					    throws IOException
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // 根据配置文件的路径,查找是否已经创建了对应的session
 | 
					        // 根据配置文件的路径,查找是否已经创建了对应的session
 | 
				
			||||||
@@ -48,5 +49,28 @@ public class MybatisUtils
 | 
				
			|||||||
        SessionMap.put(configFile, session);
 | 
					        SessionMap.put(configFile, session);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return session;
 | 
					        return session;
 | 
				
			||||||
    }    
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    public static SqlSession getSqlSession()
 | 
				
			||||||
 | 
					    throws IOException
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        // 根据配置文件的路径,查找是否已经创建了对应的session
 | 
				
			||||||
 | 
					        SqlSession session = SessionMap.get(DEFAULT_CONFIGFILE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // 找到就直接返回
 | 
				
			||||||
 | 
					        if ( session != null )
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return session;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // 没找到就创建一个
 | 
				
			||||||
 | 
					        InputStream configIS = Resources.getResourceAsStream(DEFAULT_CONFIGFILE);
 | 
				
			||||||
 | 
					        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(configIS);
 | 
				
			||||||
 | 
					        session = factory.openSession();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // 保存
 | 
				
			||||||
 | 
					        SessionMap.put(DEFAULT_CONFIGFILE, session);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return session;
 | 
				
			||||||
 | 
					    } 
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -22,9 +22,6 @@ import com.fasterxml.jackson.annotation.JsonProperty;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
public class DepartmentArchievement
 | 
					public class DepartmentArchievement
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    // mybatis配置文件路径
 | 
					 | 
				
			||||||
    private static String mybatisConfigFile = "mybatis/mybatis-config.xml";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // log4j2
 | 
					    // log4j2
 | 
				
			||||||
    private static Logger logger = LoggerFactory.getLogger(DepartmentArchievement.class);
 | 
					    private static Logger logger = LoggerFactory.getLogger(DepartmentArchievement.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -71,7 +68,7 @@ public class DepartmentArchievement
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        try
 | 
					        try
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            SqlSession session = MybatisUtils.getSqlSession( mybatisConfigFile );
 | 
					            SqlSession session = MybatisUtils.getSqlSession();
 | 
				
			||||||
            ArchievementMapper mapper = session.getMapper(ArchievementMapper.class);
 | 
					            ArchievementMapper mapper = session.getMapper(ArchievementMapper.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // 参数集合
 | 
					            // 参数集合
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user