摘要:
閱讀全文
posted @
2017-09-04 16:33 David1228 閱讀(305) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2017-08-02 11:41 David1228 閱讀(320) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2017-07-24 19:10 David1228 閱讀(11391) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2017-07-19 18:10 David1228 閱讀(3433) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2017-06-06 23:10 David1228 閱讀(292) |
評(píng)論 (0) |
編輯 收藏
posted @
2017-06-06 23:09 David1228 閱讀(244) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2017-05-30 19:26 David1228 閱讀(3283) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2017-05-30 19:25 David1228 閱讀(1254) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2016-12-22 23:01 David1228 閱讀(4616) |
評(píng)論 (2) |
編輯 收藏
摘要:
閱讀全文
posted @
2016-11-04 23:44 David1228 閱讀(2297) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2016-07-28 17:57 David1228 閱讀(401) |
評(píng)論 (0) |
編輯 收藏
摘要: JVM調(diào)優(yōu)總結(jié) + jstat 分析jstat -gccause pid 1 每格1毫秒輸出結(jié)果jstat -gccause pid 2000 每格2秒輸出結(jié)果不斷的在屏幕打印出結(jié)果 S0 S1 E &n...
閱讀全文
posted @
2016-01-03 23:26 David1228 閱讀(2997) |
評(píng)論 (2) |
編輯 收藏
摘要:
閱讀全文
posted @
2015-12-27 16:49 David1228 閱讀(1089) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2015-10-31 12:37 David1228 閱讀(669) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2015-10-30 20:20 David1228 閱讀(2879) |
評(píng)論 (0) |
編輯 收藏
轉(zhuǎn)載文章:
http://www.qixing318.com/article/by-lua-adhesive-nginx-ecological-environment.html
posted @
2015-09-08 13:22 David1228 閱讀(512) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2015-09-08 09:34 David1228|
編輯 收藏
摘要:
閱讀全文
posted @
2015-07-29 13:48 David1228 閱讀(5555) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2014-12-03 23:56 David1228 閱讀(28338) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2013-11-19 11:48 David1228 閱讀(23742) |
評(píng)論 (1) |
編輯 收藏
摘要:
閱讀全文
posted @
2013-11-18 18:01 David1228 閱讀(2901) |
評(píng)論 (4) |
編輯 收藏
摘要:
閱讀全文
posted @
2013-10-31 17:32 David1228 閱讀(705) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2013-10-14 09:50 David1228 閱讀(21753) |
評(píng)論 (6) |
編輯 收藏
Hibernate不支持left join帶on的條件查詢(xún)。
解決辦法:使用原生SQL或者使用HQL方式需要修改hbm文件(如果項(xiàng)目中大部分不適用級(jí)聯(lián)配置情況下)
需求是查詢(xún)網(wǎng)絡(luò)信息,網(wǎng)絡(luò)信息中關(guān)聯(lián)了分區(qū)、網(wǎng)絡(luò)IP(1個(gè)網(wǎng)絡(luò)--N個(gè)網(wǎng)絡(luò)IP),網(wǎng)橋信息.
HQL:
public void queryVnets(Page page) {
StringBuffer hql = new StringBuffer();
hql.append("select new Map(vn.id as id, vn.vnName as vnName, b.name as vnType, ");
hql.append("vn.vnIptype as vnIptype, vn.vnIp as vnIp,vn.vnNat as vnNat, vn.vnFlag as vnFlag, ");
hql.append("vn.vnetFlag as vnetFlag, (case when sum(vmvn.state)='0' then 0 else 1 end) as vnState, ");
hql.append("vn.useType as useType, vn.createTime as createTime, ");
hql .append("zone.oneName as oneName, zone.oneHypervisor as oneHypervisor, zone.oneSeq as oneSeq) ");
hql.append("from VnetTable vn, ZoneTable zone, BridgeTable b ");
hql.append("left join fetch vn.VnTables vmvn where zone.id = vn.zoneId and vn.vnType = b.id ");
... ...
hql.append(" group by vn.id");
if (vnState != null && !"".equals(vnState)) {
if ("0".equals(vnState)) {
hql.append(" having sum(vmvn.state)=0");
} else if ("1".equals(vnState)) {
hql.append(" having sum(vmvn.state)!=0");
}
}
}
hbm配置文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.base.mapping.VnetTable" table="vnet_table" >
<id name="id" type="java.lang.String">
<column name="ID" length="32" />
<generator class="uuid.hex" />
</id>
<property name="vnId" type="java.lang.Long">
<column name="VN_ID">
<comment>創(chuàng)建虛擬網(wǎng)絡(luò)時(shí)得到的ID</comment>
</column>
</property>
...
<property name="zoneId" type="java.lang.String">
<column name="ZONE_ID" length="32">
<comment>分區(qū)ID</comment>
</column>
</property>
<property name="vlanId" type="java.lang.Integer">
<column name="VLAN_ID">
<comment>VLAN_ID</comment>
</column>
</property>
<set name="vnTables" lazy="false" order-by="id asc" inverse="false" fetch="join" cascade="all">
<key column="VN_NID"/>
<one-to-many class="com.base.mapping.VnTable"/>
</set>
</class>
</hibernate-mapping>
理解inverse和cascade,可以參考:http://blog.csdn.net/wkcgy/article/details/6271321
總結(jié): <one-to-many>中,建議inverse=”true”,由“many”方來(lái)進(jìn)行關(guān)聯(lián)關(guān)系的維護(hù) <many-to-many>中,只設(shè)置其中一方inverse=”false”,或雙方都不設(shè)置 Cascade,通常情況下都不會(huì)使用。特別是刪除,一定要慎重。操作建議
一般對(duì)many-to-one和many-to-many不設(shè)置級(jí)聯(lián),這要看業(yè)務(wù)邏輯的需要;對(duì)one-to-one和one-to-many設(shè)置級(jí)聯(lián)。 many-to-many關(guān)聯(lián)關(guān)系中,一端設(shè)置inverse=”false”,另一端設(shè)置為inverse=”true”。在one-to-many關(guān)聯(lián)關(guān)系中,設(shè)置inverse=”true”,由多端來(lái)維護(hù)關(guān)系表
posted @
2013-09-12 11:57 David1228 閱讀(16680) |
評(píng)論 (2) |
編輯 收藏
-- 以下兩個(gè)查詢(xún)效率相當(dāng),查詢(xún)時(shí)間均為0.0xx秒,~ 但是Hibernate HQL不支持from后面接子查詢(xún),可以直接使用SQL方式解決,ibatis當(dāng)然可以用了。
SELECT count(*) from (
SELECT sum(t2.STATE) as vnSum, t1.ID FROM
vnetinfo_table t1, vm_vn_table t2
where t1.ID = t2.VN_NID and t1.ZONE_ID = '4028a49c3facdf26013fae12531b286e' GROUP BY t1.ID) t where t.vnSum = 0;
SELECT count(*) FROM vnetinfo_table v ,(
SELECT sum(t2.STATE) as vnSum, t1.ID as idd FROM
vnetinfo_table t1, vm_vn_table t2
where t1.ID = t2.VN_NID and t1.ZONE_ID = '4028a49c3facdf26013fae12531b286e' GROUP BY t1.ID) t
where v.ID = t.idd and t.vnSum = 0;
-- 查詢(xún)效率太低了,查詢(xún)時(shí)間均為0.5xx秒左右。要比以上兩個(gè)SQL耗時(shí)很多倍。~ Hibernate支持在where后用子查詢(xún)作為查詢(xún)條件。
SELECT count(*) FROM vnetinfo_table v WHERE v.id in
(SELECT CASE WHEN SUM(t2.STATE)=0 THEN t1.ID ELSE '' END
FROM vnetinfo_table t1, vm_vn_table t2
where t1.ID = t2.VN_NID and t1.ZONE_ID = '4028a49c3facdf26013fae12531b286e' GROUP BY t1.ID);
-- 使用join方式替換in 查詢(xún)時(shí)間均為0.09x秒左右。明顯提高了效率,但是Hibernate HQL任然不支持在這種聯(lián)合子查詢(xún)方式。
SELECT count(*) FROM vnetinfo_table v INNER JOIN (SELECT CASE WHEN SUM(t2.STATE)=0 THEN t1.ID ELSE '' END as idd
FROM vnetinfo_table t1, vm_vn_table t2
WHERE t1.ID = t2.VN_NID and t1.ZONE_ID = '4028a49c3facdf26013fae12531b286e' GROUP BY t1.ID) t ON v.ID = t.idd and t.idd IS not NULL;
附:Mysql中關(guān)于Exists用法的介紹請(qǐng)參見(jiàn):http://www.nowamagic.net/librarys/veda/detail/639
posted @
2013-08-21 13:47 David1228 閱讀(3757) |
評(píng)論 (2) |
編輯 收藏
md5sum命令詳解:linux下的shell命令 ,制作md5碼 也用于軟件的md5校驗(yàn)
MD5算法常常被用來(lái)驗(yàn)證網(wǎng)絡(luò)文件傳輸?shù)耐暾裕乐刮募蝗舜鄹摹D5 全稱(chēng)是報(bào)文摘要算法(Message-Digest Algorithm 5),此算法對(duì)任意長(zhǎng)度的信息逐位進(jìn)行計(jì)算,產(chǎn)生一個(gè)二進(jìn)制長(zhǎng)度為128位(十六進(jìn)制長(zhǎng)度就是32位)的“指紋”(或稱(chēng)“報(bào)文摘要”),不同的文件產(chǎn)生相同的報(bào)文摘要的可能性是非常非常之小的。
在linux或Unix上,md5sum是用來(lái)計(jì)算和校驗(yàn)文件報(bào)文摘要的工具程序。一般來(lái)說(shuō),安裝了Linux后,就會(huì)有md5sum這個(gè)工具,直接在命令行終端直接運(yùn)行。
1、使用md5sum來(lái)產(chǎn)生指紋(報(bào)文摘要)命令如下:
md5sum filename > filename.md5 或者
md5sum filename >>filename.md5
也可以把多個(gè)文件的報(bào)文摘要輸出到一個(gè)md5文件中,這要使用通配符*,比如某目錄下有幾個(gè)iso文件,要把這幾個(gè)iso文件的摘要輸出到iso.md5文件中,命令如下:
md5sum *.iso > iso.md5
2、使用md5報(bào)文摘要驗(yàn)證文件,方法有二:
把下載的文件file和該文件的file.md5報(bào)文摘要文件放在同一個(gè)目錄下,然后用如下命令進(jìn)行驗(yàn)證:
md5sum -c file.md5
然后如果驗(yàn)證成功,則會(huì)輸出:正確結(jié)果。實(shí)戰(zhàn)案例:
需求:虛擬機(jī)存在多份備份,當(dāng)進(jìn)行備份恢復(fù)時(shí),要求檢查源備份壓縮文件的完整性。那么可以通過(guò)md5這種方式來(lái)驗(yàn)證。壓縮備份文件并生成md5碼,部分shell腳本如下:#----------------------------------
log "Compressing and moving $SRC_PATH"
exec_and_log "$SSH $SRC_HOST mkdir -p ${SRC_PATH}_compress"
for file in `$SSH $SRC_HOST ls $SRC_PATH | grep -v $SN_SIGN`
do
exec_and_log "$SSH $SRC_HOST cd $SRC_PATH;sudo chmod -R +r $SRC_PATH/*;tar -Sczvf ${SRC_PATH}_compress/${file}.tgz ${file}"
done
#----------------------------------
#--------------md5 begin-----------------
log "Create backup.md5"
exec_and_log "$SSH $SRC_HOST $MD5SUM ${SRC_PATH}_compress/*.tgz > ${SRC_PATH}_compress/backup.md5"
#--------------md5 end-----------------
#其中 exec_and_log是另一個(gè)公共腳本中定義的。shell中可以通過(guò)點(diǎn)(.)的方式將腳本引入。該函數(shù)內(nèi)如下:
# Executes a command, if it fails returns error message and exits
# If a second parameter is present it is used as the error message when
# the command fails
function exec_and_log
{
message=$2
EXEC_LOG_ERR=`$1 2>&1 1>/dev/null`
EXEC_LOG_RC=$?
if [ $EXEC_LOG_RC -ne 0 ]; then
log_error "Command \"$1\" failed: $EXEC_LOG_ERR"
if [ -n "$2" ]; then
error_message "$2"
else
error_message "Error executing $1: $EXEC_LOG_ERR"
fi
exit $EXEC_LOG_RC
fi
}
# Logs a message, alias to log_info
function log
{
log_info "$1"
}
# Log function that knows how to deal with severities and adds the
# script name
function log_function
{
echo "$1: $SCRIPT_NAME: $2" 1>&2
}
# Logs an info message
function log_info
{
log_function "INFO" "$1"
}
# Logs an error message
function log_error
{
log_function "ERROR" "$1"
}
# This function is used to pass error message to the mad
function error_message
{
(
echo "ERROR MESSAGE --8<------"
echo "$1"
echo "ERROR MESSAGE ------>8--"
) 1>&2
}
生成的backup.md5內(nèi)容:$ md5sum *.tgz > backup.md5
2c07174d451a687bc0e401b1dc7d84b8 checkpoint.1376892493.tgz
57a18d7f6eed36565b9012e519985130 checkpoint.1376903188.tgz
b652990806e58a81b41dc1b23db9adda checkpoint.1376904023.tgz
f4e7adb46a8fde2643e082875cc60e21 checkpoint.1376904289.tgz
ad2a59902652e2896b35507417b7016f checkpoint.tgz
4bd43eac64f238bb35c7194816da7d23 deployment.0.tgz
ee3d9266cf92f7540cc75a94343bc20a deployment.7.tgz
fcd5aae37354b5cb66f4b90544eaa5c3 disk.0.tgz
c1ef6d911b441660ec2d1e44b5a56f66 disk.1.iso.tgz
25eae70a31a7c7cff3baade4cd65fe82 disk.1.tgz
檢查備份文件是否完整:exec_and_log "$SSH $SRC_HOST $MD5SUM -c ${SRC_PATH}/backup.md5"
檢測(cè)結(jié)果:$ md5sum -c backup.md5
checkpoint.1376892493.tgz: OK
checkpoint.1376903188.tgz: OK
checkpoint.1376904023.tgz: OK
checkpoint.1376904289.tgz: OK
checkpoint.tgz: OK
deployment.0.tgz: OK
deployment.7.tgz: OK
disk.0.tgz: OK
disk.1.iso.tgz: OK
disk.1.tgz: OK
posted @
2013-08-20 11:34 David1228 閱讀(2440) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2013-08-20 09:46 David1228 閱讀(392) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2013-08-20 09:44 David1228 閱讀(393) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2013-07-24 16:11 David1228 閱讀(33410) |
評(píng)論 (0) |
編輯 收藏
摘要: 根據(jù)官方的說(shuō)法,在ibatis3,也就是Mybatis3問(wèn)世之前,Spring3的開(kāi)發(fā)工作就已經(jīng)完成了,所以Spring3中還是沒(méi)有對(duì)Mybatis3的支持。因此由Mybatis社區(qū)自己開(kāi)發(fā)了一個(gè)Mybatis-Spring用來(lái)滿(mǎn)足Mybatis用戶(hù)整合Spring的需求。下面就將通過(guò)Mybatis-Spring來(lái)整合Mybatis跟Spring的用法做一個(gè)簡(jiǎn)單的介紹。MapperFactoryB...
閱讀全文
posted @
2013-07-10 18:07 David1228 閱讀(21427) |
評(píng)論 (2) |
編輯 收藏
不得不承認(rèn)的是,在目前的web應(yīng)用開(kāi)發(fā)中,Spring的應(yīng)用越來(lái)越廣泛,其地位變得相當(dāng)重要。而在最近推出的Spring 3.2版本中,又引入了一些新的值得關(guān)注的特性。本文中將介紹其中一些值得關(guān)注的新特性。
一 不再需要web.xml
在新的Spring 3.2中,可以不再需要web.xml這個(gè)文件了,這得益于Servlet 3.0的新特性。
其中,可以使用@WebServlet在需要調(diào)用的servlet中,使用注解去設(shè)置servlet中的映射關(guān)系。這樣的話(huà),就可以不再象以往那樣要通過(guò)在web.xml中對(duì)servlet進(jìn)行配置,十分方便。
此外,Servlet 3.0提供了既能在容器中動(dòng)態(tài)注冊(cè)servlet的方法,也提供了通過(guò)實(shí)現(xiàn)
ServletContainerInitializer接口的方法實(shí)現(xiàn)在容器啟動(dòng)階段為容器動(dòng)態(tài)注冊(cè)Servlet、Filter和listeners。容器會(huì)在應(yīng)用的啟動(dòng)階段,調(diào)用所有實(shí)現(xiàn)ServletContainerInitializer接口類(lèi)中的onStartup()方法。而Spring 3.2中,則進(jìn)一步簡(jiǎn)化了這點(diǎn),只需要實(shí)現(xiàn)WebApplicationInitializer接口就可以了,其中提供了一個(gè)相關(guān)的實(shí)現(xiàn)類(lèi)--AbstractContextLoaderInitializer,它可以動(dòng)態(tài)注冊(cè)DispatcherServlet。這意味著,只要spring-webmvc.jar放置在web應(yīng)用的web-inf/lib中,就可以調(diào)用Dispatcher servlet了。可以參考如下的例子(來(lái)自Spring文檔):
- public class MyWebApplicationInitializer implements WebApplicationInitializer {
-
- @Override
- public void onStartup(ServletContext container) {
- ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet());
- registration.setLoadOnStartup(1);
- registration.addMapping("/example/*");
- }
-
- }
二 支持Java編程式配置在Spring mvc 3.2中,一個(gè)不錯(cuò)的特性就是在之前版本中支持使用Java代碼配置各種依賴(lài)注入的基礎(chǔ)上,進(jìn)一步簡(jiǎn)化了配置。關(guān)于使用Java編程式配置的好處,可以通過(guò)下面這個(gè)鏈接文章的介紹去學(xué)習(xí)
(http://blog.frankel.ch/consider-replacing-spring-xml-configuration-with-javaconfig)。
在Spring mvc 3.2中,提供了AbstractContextLoaderInitialize的一個(gè)子類(lèi)AbstractAnnotationConfigDispatcherServletInitializer
去實(shí)現(xiàn)零XML配置的效果,只需要?jiǎng)?chuàng)建的類(lèi)繼承AbstractAnnotationConfigDispatcherServletInitializer
就可以了,如下代碼所示:
- public
-
- class
-
- SugarSpringWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
-
- @Override
- protected Class<?>[] getRootConfigClasses() {
- return new Class[] { JavaConfig.class };
- }
-
- @Override
- protected Class<?>[] getServletConfigClasses() {
- return new Class[] { WebConfig.class };
- }
-
- @Override
- protected String[] getServletMappings() {
- return new String[] { "/" };
- }
-
- }
三功能更強(qiáng)大的
Spring Test
框架
現(xiàn)在,單元測(cè)試顯得越來(lái)越重要了,每一個(gè)類(lèi)都建議做好對(duì)應(yīng)的單元測(cè)試。在
Spring 3.2
版本之前,如果要對(duì)
Spring MVC
進(jìn)行單元測(cè)試的話(huà),必須顯式去調(diào)用某個(gè)控制器類(lèi)中的某個(gè)方法,而不能直接單元測(cè)試相關(guān)的
mapping
映射。而在
Spring mvc 3.2
中,重新整合和增強(qiáng)了測(cè)試框架的功能,支持直接用
/*
這樣的映射方式去測(cè)試某個(gè)控制器中的類(lèi)。同時(shí),之前的開(kāi)源項(xiàng)目(
https://github.com/SpringSource/spring-test-mvc)也被收錄到Spring mvc 3.2中去了
。此外,還新增加了對(duì)返回
(return)
,重定向
(redirect)
和
model
等的測(cè)試改進(jìn),下面是一個(gè)例子:
- public class SayHelloControllerIT extends AbstractTestNGSpringContextTests {
-
- private MockMvc mockMvc;
-
- @BeforeMethod
- public void setUp() {
-
- mockMvc = webAppContextSetup((WebApplicationContext) applicationContext).build();
- }
-
- @Test(dataProvider = "pathParameterAndExpectedModelValue")
- public void accessingSayhelloWithSubpathShouldForwardToSayHelloJspWithModelFilled(String path, String value) throws Exception {
-
- mockMvc.perform(get("/sayHello/Jo")).andExpect(view().name("sayHello")).andExpect(model().attribute("name", "Jo"));
- }
- }
本文的示例代碼可以在:
http://blog.frankel.ch/wp-content/resources/spring-3-2-sweetness/spring-sweet-1.0.0.zip中下載。
原文鏈接:http://java.dzone.com/articles/spring-32-sweetness
posted @
2013-07-10 11:48 David1228 閱讀(2345) |
評(píng)論 (0) |
編輯 收藏
摘要: 本文轉(zhuǎn)載自:http://www.cnblogs.com/java-my-life/archive/2012/03/28/2418836.html場(chǎng)景問(wèn)題 舉個(gè)生活中常見(jiàn)的例子——組裝電腦,我們?cè)诮M裝電腦的時(shí)候,通常需要選擇一系列的配件,比如CPU、硬盤(pán)、內(nèi)存、主板、電源、機(jī)箱等。為討論使用簡(jiǎn)單點(diǎn),只考慮選擇CPU和主板的問(wèn)題。 事實(shí)上,在選擇CPU的時(shí)候,面臨一系列的...
閱讀全文
posted @
2013-07-04 17:07 David1228 閱讀(400) |
評(píng)論 (0) |
編輯 收藏