微信开发准备(三)--框架以及工具的基本使用

转自:http://www.cuiyongzhi.com/post/35.html

在前面两篇中我们从基本的项目建立到框架搭建,将项目已经搭建成功,并将基本的配置项也都已经配置完成,那么这里我们就进入到对框架的熟悉和一个工具generator的使用!

(一)项目部分配置文件的初始化

我们在前面框架中层在web.xml文件中配置了一个启动Servlet初始化文件,这里做的就是在项目中需要用到某些配置文件的时候,我们在这个时候对配置文件中的值初始化到公共Properties中,以方便后面的调用,基本代码实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.cuiyongzhi.web.start;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
 
/**
 
 * ClassName: InterfaceUrlIntiServlet
 
 * @Description: 項目文件初始化
 * @author dapengniao
 * @date 2015/10/13
 */
public class InterfaceUrlIntiServlet extends HttpServlet {
 
    private static final long serialVersionUID = 1L;
 
    @Override
    public void init(ServletConfig config) throws ServletException {
        InterfaceUrlInti.init();
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.cuiyongzhi.web.start;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
import com.cuiyongzhi.web.util.GlobalConstants;
 
/**
 
 * ClassName: InterfaceUrlInti
 * @Description: 項目啓動配置文件初始化
 * @author dapengniao
 * @date 2015/10/13
 */
public class InterfaceUrlInti {
 
    public synchronized static void init(){
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        Properties props = new Properties();
        if(GlobalConstants.interfaceUrlProperties==null){
            GlobalConstants.interfaceUrlProperties = new Properties();
        }
        InputStream in = null;
        try {
            in = cl.getResourceAsStream("interface_url.properties");
            props.load(in);
            for(Object key : props.keySet()){
                GlobalConstants.interfaceUrlProperties.put(key, props.get(key));
            }
             
            props = new Properties();
            in = cl.getResourceAsStream("wechat.properties");
            props.load(in);
            for(Object key : props.keySet()){
                GlobalConstants.interfaceUrlProperties.put(key, props.get(key));
            }
             
        catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(in!=null){
                try {
                    in.close();
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return;
    }
 
}

在这里我初始化化了两个文件,一个是用来配置在微信开发中经常用的到appid、AppSecret的参数(wechat.properties),另外一个用来初始化我们经常用到的http请求的url地址interface_url.properties!

初始化成功之后我们只需要通过下面的方法即可在项目中任何想用的地方去使用:GlobalConstants.getInterfaceUrl(key),如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.cuiyongzhi.web.util;
 
import java.util.Properties;
 
public class GlobalConstants {
 
    public static Properties interfaceUrlProperties;
 
/**
 
 * @Description: TODO
 * @param @param key
 * @param @return   
 * @author dapengniao
 * @date 2015年10月13日 下午4:59:14
 */
    public static String getInterfaceUrl(String key) {
        return (String) interfaceUrlProperties.get(key);
    }
     
         
     
}

(二)对日志文件的配置说明

在我搭建的开发环境中采用的是log4j日志记录的方式,这种方式对普通项目是没有问题的,后续将有可能升级为logback,首先我们在resources下新建文件log4j.properties,简单配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
log4j.rootLogger=DEBUG,Console,File
#ERROR,WARN,INFO,DEBUG   日志输出等级依次降低,可以根据自己的需求自己调整输出等级
 
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Threshold=DEBUG
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH:mm:ss,SSS}][%c]%m%n
 
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.File.File=${catalina.base}/wechatlogs/wechat.log
log4j.appender.File.Threshold=INFO
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.Append=true
log4j.appender.File.ImmediateFlush=true
log4j.appender.File.DatePattern=yyyy-MM-dd'.log'
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH:mm:ss,SSS}][%c]%m%n

有了上面的配置文件之后我们在web.xml中加入如下的启动配置即可:

1
2
3
4
5
6
7
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

简单的使用如下图所示:

1.png

(三)Mybatis工具Generator

在这里我要推荐一款工具Generator,在项目开发中他给我节省了很多,他的作用是让我们能很方便生成我们需要的表对应的pojo、mapping、dao的代码,而且使用起来非常简单,由于这篇文章篇幅不短了,所以我开了一篇新的来详细讲解它的使用,地址:http://www.cuiyongzhi.com/?id=36 

原文地址:https://www.cnblogs.com/sharpest/p/10227038.html