@zhanjindong
2015-02-12T15:36:11.000000Z
字数 15190
阅读 2843
iFlytek
ossp-framework-common
ossp-framework-usdl
ossp-framework-dt
ossp-framework-common
<dependency>
<groupId>com.iflytek</groupId>
<artifactId>ossp-framework-common</artifactId>
<version>1.0.1</version>
</dependency>
ossp-framework-usdl
<dependency>
<groupId>com.iflytek</groupId>
<artifactId>ossp-framework-usdl</artifactId>
<version>1.0.1</version>
</dependency>
ossp-framework-dt
<dependency>
<groupId>com.iflytek</groupId>
<artifactId>ossp-framework-dt</artifactId>
<version>1.0.1</version>
</dependency>
点击查看
使用示例,请查看Example Packages
的内容。
@牛志刚
)会发出。详细的修改日志,请点击查看。
引入ossp-framework-example
可以查看示例代码:
<dependency>
<groupId>com.iflytek</groupId>
<artifactId>ossp-framework-example</artifactId>
<version>1.0.2-SNAPSHOT</version>
</dependency>
package com.iflytek.ossp.framework.example.usdl.mysql;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Random;
import com.iflytek.ossp.framework.usdl.mysql.ConnectionCallback;
import com.iflytek.ossp.framework.usdl.mysql.MySqlClient;
import com.iflytek.ossp.framework.usdl.mysql.MySqlConnectionManagement;
/**
*
* {@link MySqlClient} API使用示例。
*
* @author jdzhan,2014-9-22
*
*/
public class MyqlClientExample {
static Random random = new Random();
public static void main(String[] args) throws PropertyVetoException, IOException, URISyntaxException, SQLException,
IllegalAccessException {
MySqlConnectionManagement.load();
System.out.println("执行存储过程,只能处理第一个结果集:");
execProcedureExample();
System.out.println("执行存储过程,可以处理多结果集:");
execMutilResultProcedureExample();
System.out.println("执行存储过程,也可以不指定回调接口,返回null:");
execProcedureWithNullCallback();
MySqlConnectionManagement.destroy();
}
// 执行存储过程,只返回第一个结果集。
private static void execProcedureExample() throws PropertyVetoException, IOException, SQLException,
URISyntaxException, IllegalAccessException {
MySqlClient<Object> client = MySqlConnectionManagement.sharedClient("example-2");
int i = random.nextInt(100);
client.execProc("test_query_proc4", new ConnectionCallback<String>() {
@Override
public String callback(ResultSet resultSet) throws SQLException {
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
return "";
}
}, "framework-" + i, i);
}
// 执行多结果集的存储过程。
// begin
// insert into test(name,age,description,height)
// values('zdd',11,'dede',122);
// select 1;
// select 2;
// select 3;
// select 4;
// end;
private static void execMutilResultProcedureExample() throws PropertyVetoException, IOException,
URISyntaxException, IllegalAccessException, SQLException {
MySqlClient<Object> client = MySqlConnectionManagement.sharedClient("example-2");
int i = random.nextInt(100);
client.execMutilProc("test_query_proc4",
new com.iflytek.ossp.framework.usdl.mysql.ConnectionMutilCallback<String>() {
@Override
public String callback(Iterable<ResultSet> resultSets) throws SQLException {
Iterator<ResultSet> iterator = resultSets.iterator();
for (ResultSet resultSet : resultSets) {
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
}
return "";
}
}, "framework-" + i, i);
}
/** 可以不知道回调接口,返回结果为null */
private static void execProcedureWithNullCallback() throws PropertyVetoException, IOException, URISyntaxException,
IllegalAccessException, SQLException {
MySqlClient<Object> client = MySqlConnectionManagement.sharedClient("example-2");
int i = random.nextInt(100);
Object result = client.execMutilProc("test_query_proc4", null, "framework-" + i, i);
System.out.println("result:" + result);
result = client.execProc("test_query_proc4", null, "framework-" + i, i);
System.out.println("result:" + result);
}
}
package com.iflytek.ossp.framework.example.usdl.mysql;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.iflytek.ossp.framework.usdl.mysql.ConnectionCallback;
import com.iflytek.ossp.framework.usdl.mysql.MySqlClient;
import com.iflytek.ossp.framework.usdl.mysql.MySqlConnectionManagement;
/**
* {@link MySqlConnectionManagement}使用示例。
*
*/
public class MysqlConnectionManagementExample {
public static void main(String[] args) throws PropertyVetoException, IOException, URISyntaxException, SQLException,
IllegalAccessException {
// 在调用之前需要先调用load方法,默认无参的方法会初始化classpath:config/dbaccess/mysql路径下的所有配置。
// 默认使用c3p0连接池,如果需要使用druid或dbcp,请在配置文件中指定dataSource的值为c3p0.
MySqlConnectionManagement.load();
// 重复调用无效
MySqlConnectionManagement.load("/error/path");
MySqlClient<Object> client = MySqlConnectionManagement.sharedClient("example");
int rst = (Integer) client.query("select count(*) as count from pet", new TestQueryCallback());
System.out.println(rst);
// insert和update操作
rst = client.update("insert into pet(name,psssword)values('zjd3','121313')");
System.out.println(rst);
rst = client.update("update pet set name = 'zs' where name = 'zjd3'");
System.out.println(rst);
// 程序关闭的时候建议调用静态方法来销毁资源。
MySqlConnectionManagement.destroy();
// 如果配置文件不是在默认的classpath路径下,可以调用重载的load方法指定父级路径,
// 但注意配置文件所在的上级目录必须为mysql,如下面的参数则配置文件在classpath:config/mysql路径下
MySqlConnectionManagement.load("classpath:config");
client = MySqlConnectionManagement.sharedClient("example2");
rst = (Integer) client.query("select count(*) as count from pet", new TestQueryCallback());
System.out.println(rst);
// insert和update操作
rst = client.update("insert into pet(name,psssword)values('zjd3','121313')");
System.out.println(rst);
rst = client.update("update pet set name = 'zs' where name = 'zjd3'");
System.out.println(rst);
MySqlConnectionManagement.destroy();
// dbcp example
// 注意dbcp除了基本的配置项 其余的和c3p0以及druid差别较大。
MySqlConnectionManagement.load();
// 重复调用无效
client = MySqlConnectionManagement.sharedClient("example-dbcp");
rst = (Integer) client.query("select count(*) as count from pet", new TestQueryCallback());
System.out.println(rst);
// insert和update操作
rst = client.update("insert into pet(name,psssword)values('zjd3','121313')");
System.out.println(rst);
rst = client.update("update pet set name = 'zs' where name = 'zjd3'");
System.out.println(rst);
// 程序关闭的时候建议调用静态方法来销毁资源。
MySqlConnectionManagement.destroy();
// 父级路径也可以是绝对路径,如:配置文件在D:\example\mysql下。
MySqlConnectionManagement.load("file:D:\\example");
// 或
// MySqlConnectionManagement.load("D:\\example");
// 或
// MySqlConnectionManagement.load("D:/example");
// reload:相当于先destroy再load
MySqlConnectionManagement.reload("file:D:\\example");
MySqlConnectionManagement.destroy();
}
public static class TestQueryCallback implements ConnectionCallback<Integer> {
@Override
public Integer callback(ResultSet resultSet) throws SQLException {
while (resultSet.next()) {
return resultSet.getInt("count");
}
return -1;
}
}
}
package com.iflytek.ossp.framework.example.common.serializable;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.iflytek.ossp.framework.common.serializable.JSONRoot;
import com.iflytek.ossp.framework.common.serializable.SerializationInstance;
import com.iflytek.ossp.framework.common.serializable.SerializationTools;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
/**
*
* {@link SerializationTools#fastFromJson(Class, String)}<br />
* {@link SerializationTools#fastToJson(Object)}<br />
*
* @author jdzhan,2014-8-21
*
*/
public class FastjsonExample {
static SerializationInstance tools = SerializationInstance.sharedInstance();
public static void main(String[] args) throws IllegalStateException, Exception {
// annotationBenchmark();
fastJsonExample();
}
public static void fastJsonExample() throws SecurityException, IllegalArgumentException, NoSuchFieldException,
IllegalAccessException {
A a = new A();
a.setStr1("str1");
a.setStr2("str2");
List<String> list = new ArrayList<String>();
list.add("item1");
list.add("item1");
a.setLists(list);
List<B> barr = new ArrayList<FastjsonExample.B>();
for (int i = 0; i < 2; i++) {
B b = new B();
b.setBstr1("b str1");
b.setBstr2("b str2");
barr.add(b);
}
a.setBarr(barr);
// fastjson
String json = tools.fastToJson(a, true);
System.out.println("fastjson result:\r\n" + json);
A newA = tools.fastFromJson(A.class, json);
System.out.println(newA);
// xstream
json = tools.toJson(a);
System.out.println("xstream result:\r\n" + json);
newA = tools.fromJson(A.class, json);
System.out.println(newA);
}
@XStreamAlias("abc")
@JSONRoot(name = "abc")
public static class A {
/** 忽略 */
@JSONField(serialize = false)
// fastjson
@XStreamOmitField
// xstream
public String str1 = "str1";
/** 指定别名 */
@JSONField(name = "name")
// xtream
@XStreamAlias("name")
// fastjson
public String str2;
/** 未指定默认为0 */
@JSONField(serialzeFeatures = { SerializerFeature.WriteNullNumberAsZero })
// fastjson
public int ivalue;
/** 未指定默认为空字符串 */
@JSONField(serialzeFeatures = { SerializerFeature.WriteNullStringAsEmpty })
// fastjson
public String str3;
@JSONField(name = "items")
@XStreamImplicit(itemFieldName = "key")
public List<String> lists;
@JSONField(name = "bitems")
@XStreamAlias("bitems")
//@XStreamImplicit(itemFieldName = "bitem")
public List<B> barr;
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
public String getStr2() {
return str2;
}
public void setStr2(String str2) {
this.str2 = str2;
}
public int getIvalue() {
return ivalue;
}
public void setIvalue(int ivalue) {
this.ivalue = ivalue;
}
public String getStr3() {
return str3;
}
public void setStr3(String str3) {
this.str3 = str3;
}
public List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
public List<B> getBarr() {
return barr;
}
public void setBarr(List<B> barr) {
this.barr = barr;
}
@Override
public String toString() {
return "str1:" + str1 + ",str2:" + str2 + ",str3:" + str3 + ",ivalue" + ivalue + ",lists:" + lists
+ "B list:" + barr;
}
}
public static class B {
private String bstr1;
private String bstr2;
public String getBstr1() {
return bstr1;
}
public void setBstr1(String bstr1) {
this.bstr1 = bstr1;
}
public String getBstr2() {
return bstr2;
}
public void setBstr2(String bstr2) {
this.bstr2 = bstr2;
}
@Override
public String toString() {
return "bstr1:" + bstr1 + ",bstr2:" + bstr2;
}
}
}
package com.iflytek.ossp.framework.example.common.serializable;
import java.util.Date;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import com.iflytek.ossp.framework.common.serializable.SerializationInstance;
import com.iflytek.ossp.framework.example.ExampleUtils;
import com.iflytek.ossp.framework.example.common.serializable.weather.cmcc.CmccWeatherBaseResponseObject;
import com.iflytek.ossp.framework.example.common.serializable.weather.cmcc.Result;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
/**
*
* Simple XML API示例:{@link SerializationInstance#simpleToXml(Object)},
* {@link SerializationInstance#simpleFromXml(Class, String)}<br />
* 详细参看:<a href=
* "http://simple.sourceforge.net/download/stream/doc/examples/examples.php"
* >http://simple.sourceforge.net/download/stream/doc/examples/examples.php</a> <br />
* <a href=
* "http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php"
* >http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php</a>
*
* <p>
* XStream的注解,Simple-XML基本都有等价的,{@link CmccWeatherBaseResponseObject}。<br />
* <code>CDATA => @Element(data = true)</code><br/>
* <code>@XStreamAlias("allresult") => @Root(name ="root"),@Element(name="test")</code>
* <br/>
* <code>@XStreamAsAttribute => @Attribute</code><br/>
* <code>@XStreamImplicit(itemFieldName = "result") => @ElementList(name = "resultlist", inline = true)</code>
* <br/>
* <code>@XStreamOmitField => 没有注解</code>
*
* @author jdzhan,2014-9-10
*
*/
public class SimpleXmlExample {
static SerializationInstance tools = SerializationInstance.sharedInstance();
public static void main(String[] args) throws Exception {
SimpleObject vo = new SimpleObject();
vo.setUserName("michael");
vo.setRealName("大大");
vo.setWife("小小");
vo.setHeight(173.3d);
vo.setBornDate(new Date());
Configuration conf = new Configuration();
Server server = new Server();
Security security = new Security();
server.setSecurity(security);
conf.setServer(server);
security.setKeyStore("KeyStore");
security.setSsl(false);
server.setHost("172.16.1.1");
server.setPort(8080);
conf.setId(1);
System.out.println("======================================================");
System.out.println("simple object example:");
// simple object
String xml = tools.simpleToXml(vo);
System.out.println(xml);
vo = tools.simpleFromXml(SimpleObject.class, xml);
System.out.println(vo);
System.out.println("======================================================");
System.out.println("nested object example:");
// nested object
xml = tools.simpleToXml(conf);
System.out.println(xml);
conf = tools.simpleFromXml(Configuration.class, xml);
System.out.println(conf.getServer().getSecurity().getKeyStore());
System.out.println("======================================================");
System.out.println("complex object & annotation vs XStream:");
// annotation
xml = ExampleUtils.readFile("classpath:serializable/cmcc.xml");
System.out.println(xml);
// XStream
CmccWeatherBaseResponseObject obj = tools.fromXml(CmccWeatherBaseResponseObject.class, xml);
System.out.println(obj);
// Simple-XML
xml = tools.simpleToXml(obj);
System.out.println(xml);
}
@Root(name = "root")
public static class SimpleObject {
@Element(name = "UserName")
private String userName;
@Attribute(name = "MyWife")
private String wife;
// CDATA
@Element(name = "RealName", data = true)
private String realName;
@Element(name = "BornDate")
private Date bornDate;
@Element(name = "Height")
private Double height;
public String toString() {
return "MyTestVo : [ userName = " + userName + " , wife = " + wife + " , realName = " + realName
+ " , height = " + height + " , bornDate = " + bornDate + " ]";
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public Date getBornDate() {
return bornDate;
}
public void setBornDate(Date bornDate) {
this.bornDate = bornDate;
}
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
}
@Root(name = "root")
@XStreamAlias("root")
public static class Configuration {
@Element
private Server server;
@Attribute
@XStreamAsAttribute
private int id;
public int getIdentity() {
return id;
}
public Server getServer() {
return server;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setServer(Server server) {
this.server = server;
}
}
public static class Server {
@Attribute
private int port;
@Element
private String host;
@Element
private Security security;
public int getPort() {
return port;
}
public String getHost() {
return host;
}
public Security getSecurity() {
return security;
}
public void setPort(int port) {
this.port = port;
}
public void setHost(String host) {
this.host = host;
}
public void setSecurity(Security security) {
this.security = security;
}
}
public static class Security {
@Attribute
@XStreamAsAttribute
private boolean ssl;
@Element
private String keyStore;
public boolean isSSL() {
return ssl;
}
public String getKeyStore() {
return keyStore;
}
public boolean isSsl() {
return ssl;
}
public void setSsl(boolean ssl) {
this.ssl = ssl;
}
public void setKeyStore(String keyStore) {
this.keyStore = keyStore;
}
}
}