我的博客bgm默认是关不掉的。。想关的手动静音或者改代码吧。。
这个BGM的原曲是初音未来的dear miku-dear
这个钢琴版是触手猴弹奏的。触手猴-dear
初音就不多说了,一直以来一直追的本命。
第一次看到触手猴[twitter]的弹二小姐的时候我是震惊的[U.N. Owen was her? – 2分30秒开始高能]世界上怎么会有弹钢琴这么屌的人。后来越看越震惊。。自然也就成了触手猴的粉丝。
推荐几个触手猴的演奏视频吧【触手猴】初音ミクの消失【触手猴】《only my railgun》钢琴演奏【某科学的超电磁炮OP】【触手猴】Night Of Knights
有鬼畜向的也有正常向的,慢慢欣赏钢琴的艺术吧。
这个就是可柔可刚的触手猴

million 10^ 6
billion 10^ 9
trillion 10^ 12
quadrillion 10^ 15
quintillion 10^ 18
sextillion 10^ 21
septillion 10^ 24
octillion 10^ 27
nonillion 10^ 30
decillion 10^ 33
undecillion 10^ 36
duodecillion 10^ 39
tredecillion 10^ 42
quattuordecillion 10^ 45
quindecillion 10^ 48
sexdecillion 10^ 51
septendecillion 10^ 54
octodecillion 10^ 57
novemdecillion 10^ 60
vigintillion 10^ 63
unvigintillion 10^ 66
duovigintillion 10^ 69
trevigintillion 10^ 72
quattuorvigintillion 10^ 75
quinvigintillion 10^ 78
sexvigintillion 10^ 81
septenvigintillion 10^ 84
octovigintillion 10^ 87
novemvigintillion 10^ 90
trigintillion 10^ 93
untrigintillion 10^ 96
duotrigintillion 10^ 99
tretrigintillion 10^102
quattuortrigintillion 10^105
quintrigintillion 10^108
sextrigintillion 10^111
septtrigintillion 10^114
octotrigintillion 10^117
novemtrigintillion 10^120
quardragintillion 10^123
unquardragintillion 10^126
duoquardragintillion 10^129
trequardragintillion 10^132
quattuorquardragintillion 10^135
quinquardragintillion 10^138
sexquardragintillion 10^141
septquardragintillion 10^144
octoquardragintillion 10^147
novemquardragintillion 10^150
quinquagintillion 10^153
unquinquagintillion 10^156
duoquinquagintillion 10^159
trequinquagintillion 10^162
quattuorquinquagintillion 10^165
quinquinquagintillion 10^168
sexquinquagintillion 10^171
septquinquagintillion 10^174
octoquinquagintillion 10^177
novemquinquagintillion 10^180
sexagintillion 10^183
unsexagintillion 10^186
duosexagintillion 10^189
tresexagintillion 10^192
quattuorsexagintillion 10^195
quinsexagintillion 10^198
sexsexagintillion 10^201
septsexagintillion 10^204
octosexagintillion 10^207
novemsexagintillion 10^210
septuagintillion 10^213
unseptuagintillion 10^216
duoseptuagintillion 10^219
treseptuagintillion 10^222
quattuorseptuagintillion 10^225
quinseptuagintillion 10^228
sexseptuagintillion 10^231
septseptuagintillion 10^234
octoseptuagintillion 10^237
novemseptuagintillion 10^240
octogintillion 10^243
unoctogintillion 10^246
duooctogintillion 10^249
treoctogintillion 10^252
quattuoroctogintillion 10^255
quinoctogintillion 10^258
sexoctogintillion 10^261
septoctogintillion 10^264
octooctogintillion 10^267
novemoctogintillion 10^270
nonagintillion 10^273
unnonagintillion 10^276
duononagintillion 10^279
trenonagintillion 10^282
quattuornonagintillion 10^285
quinnonagintillion 10^288
sexnonagintillion 10^291
septnonagintillion 10^294
octononagintillion 10^297
novemnonagintillion 10^300
centillion 10^303

QQ20160519-0@2x

最后。。。大家千万不要去玩 adventure capitalist

最近好奇做了一个测试,使用spring + mybatis 对mysql插入百条数据。
第一种,通过调用service一条一条插入,出错没有回滚。
结果耗时非常长,这也是很正常的。
第二种,在100w条sql跑在同一个事物中,好处就是这一百万条数据是原子性的。统一成功或者失败。
数据生成代码

                List<Log> logs = new ArrayList<Log>(100_0000);
		for (int i = 0; i < 100_0000; i++) {
			Log log = new Log();
			log.setAddress(Math.random()+"");
			log.setJingdu(Math.random()*100);
			log.setWeidu(Math.random()*100);
			log.setTokenId(UUID.randomUUID().toString().split("-")[0]);
			log.setSignDate(new Date());
			logs.add(log);
		}

批量插入代码

        public void insert(Iterable<Log> logs) {
		for (Log l : logs) {
			baseDao.insert(l);
		}
		
	}

结果:百万次,十次测试[679, 703, 620, 564, 698, 700, 678, 686, 711, 619]单位秒
第三种 拼接sql. insert into … values (…),(…),(….)
这种方式直接卡死。
第四种 batch update

@Override
	public void addBatch(final List<Log> item) {
		String sql = "insert into log (id, token_id, sign_date,jingdu, weidu, address)values (null,?,?,?,?, ?)";
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {  
            // 为prepared statement设置参数。这个方法将在整个过程中被调用的次数  
            public void setValues(PreparedStatement pst, int i)  
                    throws SQLException {  
            	Log log = item.get(i);
                pst.setString(1, log.getTokenId());  
                pst.setDate(2,new java.sql.Date(log.getSignDate().getTime()) );
                pst.setDouble(3, log.getJingdu());
                pst.setDouble(4, log.getWeidu());
                pst.setString(5, log.getAddress());  
            }  
  
            // 返回更新的结果集条数  
            public int getBatchSize() {  
                return item.size();  
            }  	
        });  
	}

结果[211, 216, 243, 234, 235, 238, 237, 224, 222, 226]秒
目前来看还是batchUpdate的效率最高。

今天测试了下插入性能发现用注解配置的事物不回滚,仔细找了找原因。
首先确认了下配置是对的。

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"
		proxy-target-class="true" />

然后检查注解

@Service
@Transactional(propagation=Propagation.REQUIRED) 
public interface LogService extends BaseService<Log>{
	Log dataToLog(Data data);
	
	Log selectLostContact(Date date);
	
	void insert(Iterable<Log> logs);
	
	void addBatch(List<Log> item);
}

发现都没啥问题。然后把propagation改成了只读,发现还能提交。说明事物根本没配置成功。然后把注解放到实现类上试了下。发现可以了,事物正常了,回滚也正常了。md….原来接口上的注解不继承。。。
spring版本4.0.1

最近公司有需求,希望生成pdf文档并打印。最近研究了一下itext生成pdf方面的东西。

首先我是用的maven,配置好依赖,我准备使用JFREECHART生成图表,ITEXT生成pdf

生成的pdf:

程序生成了几个矩形作为几个区域,用jfreechart生成了图表,最后讲每个区域拼合形成整个pdf。
首先生成一个pdf文档

Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();

由于某些服务器没有中文字体,需要自行安装中文字体。
我是这样解决的。

	
	public final String CHINESE_FONT;
	public final BaseFont BASE_FONT;
	public final Font fontBigTitle;
	public final Font fontSmallTitle;
	public final Font fontText;
	public final Font fontTextUnderLine;
	public final Font fontTextBold;
	public final java.text.DecimalFormat decimalFormat = new java.text.DecimalFormat("0.00");
	public final SimpleDateFormat yyyyMMDDFormat = new SimpleDateFormat("yyyy-MM-dd");
	public final SimpleDateFormat yyyyMMDDHHmmssFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public final SimpleDateFormat HHmmssFormat = new SimpleDateFormat("HH:mm:ss");


	public PDFUtils(String font) throws DocumentException, IOException {
		CHINESE_FONT = font;
		BASE_FONT = BaseFont.createFont(CHINESE_FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		fontBigTitle = new Font(BASE_FONT, 22, Font.BOLD);
		fontSmallTitle = new Font(BASE_FONT, 10, Font.BOLD);
		fontText = new Font(BASE_FONT, 8, Font.NORMAL);
		fontTextUnderLine = new Font(BASE_FONT, 8, Font.UNDERLINE | Font.NORMAL);
		fontTextBold = new Font(BASE_FONT, 8, Font.BOLD);
	}

调用的时候

String font = servletRailPath + "fonts/NotoSansCJKsc-Regular.otf";
new PDFUtils(font)

设置好标题和字体并增加到pdf

	Paragraph t = new Paragraph("标题", 
       new com.itextpdf.text.Font.Font(BASE_FONT, 22, Font.BOLD););
t.setAlignment(Element.ALIGN_CENTER);
document.add(t);

计算位置并画出一个方块,这部分代码可以封装成几个方法自动画出每一块。

	
Rectangle rectangle = drawAndSetRetangle(700, 760, writer);

public Rectangle drawAndSetRetangle(int startY, int endY, PdfWriter writer) {
		drawRectangle(startY, endY, writer);
		return setRectangle(startY, endY, writer);
}

将文字限定在刚画的矩形内

	
ColumnText ct = new ColumnText(writer.getDirectContent());
ct.setSimpleColumn(rectangle);

之后就可以新建段落,并添加文字,增加到矩形内了

	
Paragraph paragraph = new Paragraph();
paragraph.setLeading(12f);
paragraph.setFont(fontSmallTitle);
paragraph.add("xxxx \n");
paragraph.add("xxxx");
ct.addElement(paragraph);
ct.go();

按照如上步骤增加几个区域,之后pdf就生成好了。
最后不要忘了

document.close();

然后就可以去查看pdf的效果了。
以下是我封装的几个常用方法

        /**
	 * @param paragraph
	 */
	public void addDefaultSpace(Paragraph paragraph) {
		addSpace(paragraph, 5);
	}

	public void addTextAndSpaceWithUnderLine(Paragraph paragraph, String text, int length) {
		int textLength = 0;
		try {
			for (int i = 0; i < text.length(); i++) {
				char temp = text.charAt(i);
				if ((temp >= '0' && temp <= '9') || (temp >= 'a' && temp <= 'z') || (temp >= 'A' && temp <= 'Z')) {
					textLength += 2;
				} else {
					textLength += 4;
				}
			}
			length -= textLength;
			addUnderLineText(paragraph, text);
			addSpaceWithUnderLine(paragraph, length);
		} catch (Exception e) {
			addSpaceWithUnderLine(paragraph, length);
		}

	}

	public void addBoldText(Paragraph paragraph, String text) {
		paragraph.setFont(fontTextBold);
		paragraph.add(text);
		paragraph.setFont(fontText);
	}

	public void addNormalText(Paragraph paragraph, String text) {
		paragraph.setFont(fontText);
		paragraph.add(text);
		paragraph.setFont(fontText);
	}

	public void addUnderLineText(Paragraph paragraph, String text) {
		paragraph.setFont(fontTextUnderLine);
		paragraph.add(text);
		paragraph.setFont(fontText);
	}

	public void addSpaceWithUnderLine(Paragraph paragraph, int amonut) {
		paragraph.setFont(fontTextUnderLine);
		for (int i = 0; i < amonut; i++) {
			paragraph.add("\u00a0");
		}
		paragraph.setFont(fontText);
	}

	public void addSpace(Paragraph paragraph, int amonut) {
		paragraph.setFont(fontText);
		for (int i = 0; i < amonut; i++) {
			paragraph.add("\u00a0");
		}
		paragraph.setFont(fontText);
	}

	public void drawRectangle(int startY, int endY, PdfWriter writer) {
		PdfContentByte canvas = writer.getDirectContent();
		canvas.setColorFill(BaseColor.BLACK);
		canvas.moveTo(20, startY);
		canvas.lineTo(20, endY);
		canvas.lineTo(575, endY);
		canvas.lineTo(575, startY);
		canvas.lineTo(20, startY);
		canvas.closePathStroke();
	}

	public Rectangle setRectangle(int startY, int endY, PdfWriter writer) {
		Rectangle r = new Rectangle(25, startY, 575, endY);
		PdfContentByte canvas = writer.getDirectContent();
		canvas.setColorFill(BaseColor.BLACK);
		canvas.rectangle(r);
		canvas.stroke();
		return r;
	}

	public Rectangle drawAndSetRetangle(int startY, int endY, PdfWriter writer) {
		drawRectangle(startY, endY, writer);
		return setRectangle(startY, endY, writer);
	}

maven配置如下:

  
        
            com.itextpdf
            itextpdf
            5.5.8
        

        
            com.itextpdf
            itext-pdfa
            5.5.8
        

        
            com.itextpdf
            itext-xtra
            5.5.8
        

        
            com.itextpdf.tool
            xmlworker
            5.5.8
        

        
            com.itextpdf
            itext-asian
            5.2.0
        
        
            org.jfree
            jfreechart
            1.0.19