转载自崔用志个人博客: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
>
|
简单的使用如下图所示:
(三)Mybatis工具Generator
在这里我要推荐一款工具Generator,在项目开发中它帮我节省了很多时间,让我们能很方便生成我们需要的表对应的pojo、mapping、dao的代码,而且使用起来非常简单。
MyBatis Generator(以下简称为MBG),可以逆向生成持久层的基本代码,而且mybatis的实现方案比较好,可以自由组合完成比较复杂的查询,当然更复杂的就需要手动写了,下面整理下基本使用!
①MBG的下载
我这里分享的是我经常使用的版本,百度云下载地址:http://pan.baidu.com/s/1dEvCThv 下载成功后的大致目录结构为:
②打开目录中的generator.xml文件,其中部分需要修改的地方我都有备注说明,我这里给出的示例如下:
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
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<
generatorConfiguration
>
<!-- 数据库驱动包位置 -->
<
classPathEntry
location
=
"F:\generator\mysql.jar"
/>
<!--<classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
<
context
id
=
"DB2Tables"
targetRuntime
=
"MyBatis3"
>
<
commentGenerator
>
<
property
name
=
"suppressAllComments"
value
=
"true"
/>
</
commentGenerator
>
<!-- mysql数据库链接URL、用户名、密码 -->
<
jdbcConnection
driverClass
=
"com.mysql.jdbc.Driver"
connectionURL
=
"jdbc:mysql://localhost:3306/wechat"
userId
=
"root"
password
=
"123456789"
>
<!--Oracle数据库使用 -->
<!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
</
jdbcConnection
>
<
javaTypeResolver
>
<
property
name
=
"forceBigDecimals"
value
=
"false"
/>
</
javaTypeResolver
>
<!-- 生成模型的包名和位置 -->
<
javaModelGenerator
targetPackage
=
"com.cuiyongzhi.web.pojo"
targetProject
=
"F:\generator\src"
>
<
property
name
=
"enableSubPackages"
value
=
"true"
/>
<
property
name
=
"trimStrings"
value
=
"true"
/>
</
javaModelGenerator
>
<!-- 生成的映射文件包名和位置 -->
<
sqlMapGenerator
targetPackage
=
"com.cuiyongzhi.web.mapping"
targetProject
=
"F:\generator\src"
>
<
property
name
=
"enableSubPackages"
value
=
"true"
/>
</
sqlMapGenerator
>
<!-- 生成DAO的包名和位置 -->
<
javaClientGenerator
type
=
"XMLMAPPER"
targetPackage
=
"com.cuiyongzhi.web.dao"
targetProject
=
"F:\generator\src"
>
<
property
name
=
"enableSubPackages"
value
=
"true"
/>
</
javaClientGenerator
>
<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
<
table
tableName
=
"t_wechatconfig"
domainObjectName
=
"WechatConfig"
enableCountByExample
=
"false"
enableUpdateByExample
=
"false"
enableDeleteByExample
=
"false"
enableSelectByExample
=
"false"
selectByExampleQueryId
=
"false"
/>
</
context
>
</
generatorConfiguration
>
|
这里的mysql的依赖包需要定位到自己的jar包目录,数据库账号密码、数据库选择、导出的位置、数据库表等设置成自己的即可!
③生成自己需要的文件的操作只需要到文件目录空白处按住【shift】然后邮件鼠标右击选择【在此处打开命令窗口】,然后复制【生成语句.txt】中的命令道cmd命令行,【回车】执行即可,然后我们就可以在【src】里面找到我们设置的目录和文件(下图)!
MBG的基本使用和注意事项也就是这样了,最后感谢你的翻阅,如有疑问可以留言讨论!