osgi實戰項目(osmp)一步一步玩轉osgi之第一個服務(7)

前面幾篇文章已經講了一些組件了,今天咱們就開始實實在在的動手三分鐘完成咱們的第一個業務bundle,寫一個服務提供對數據的CRUD功能。java

 

咱們寫一個demo完成學生信息的crud功能。mysql

 

一、進入osmp下載目錄,複製粘貼osmp-utils 並更名爲 osmp-demoweb

二、進入osmp-demo目錄,修改pom.xml爲如下內容spring

 

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.osmp.baseweb</groupId>
		<artifactId>osmp-parent</artifactId>
		<version>1.0.0</version>
	</parent>

	<artifactId>osmp-demo</artifactId>
	<packaging>bundle</packaging>
	<name>osmp-demo</name>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<extensions>true</extensions>
				<configuration>
					<instructions>
						<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
						<Export-Package></Export-Package>
						<Import-Package>
							org.springframework.aop,
							org.springframework.aop.framework,
							org.springframework.cglib,
							org.springframework.cglib.proxy,
							org.springframework.cglib.core,
							org.springframework.cglib.reflect,
							org.aopalliance.aop,
							org.aopalliance.intercept,
							*;resolution:=optional
						</Import-Package>
					</instructions>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<dependencies>
		<dependency>
			<groupId>com.osmp.baseweb</groupId>
			<artifactId>osmp-intf-define</artifactId>
			<version>${osmp.version}</version>
		</dependency>
		<dependency>
			<groupId>com.osmp.baseweb</groupId>
			<artifactId>osmp-jdbc</artifactId>
			<version>${osmp.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.osgi</groupId>
			<artifactId>spring-osgi-core</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.osgi</groupId>
			<artifactId>org.osgi.core</artifactId>
		</dependency>
		
	</dependencies>
</project>

 

 

  • osmp-inf-define 組件是咱們基礎服務定義接口,業務開發須要實現裏面的BaseDataService接口
  • osmp-jdbc 是咱們須要用到OSMP提供的數據源和封裝好的dao模板功能。
  • spring-osgi-core,org.osgi.core 照抄,springDM必須依賴。
  • maven-bundle-plugin 插件是maven提供的在osgi環境下編譯bundle的插件。先暫時照抄,之後再解釋。

三、刪除utils組件裏類和配置,並修改包名,最後包結構以下:sql

osmp-demo    數據庫

          |src  |main | java | com/osmp/demo/serviceapache

                           | resources  | META-INF/springjson

                  |test  | javaapp

                           | resourceseclipse

          |pom.xml

 

四、在osmp根目錄下的父 pom.xml裏 <modules></modules> 新增 <module>osmp-demo</module>

五、打開 eclipse 右鍵 import -> Existing Maven Projects -> Browse... 選擇 osmp 目錄後。在下面的 Projects: 把osmp-demo勾取起後點 Finish

  


 

六、數據庫新建表 demo_user 

 

CREATE TABLE `demo` (
  `id` varchar(50) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `remark` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

七、新建實體對象User.java

 

/*   
 * Project: OSMP
 * FileName: User.java
 * version: V1.0
 */
package com.osmp.demo.service.user.entity;

import com.osmp.jdbc.define.Column;

/**
 * Description:
 * @author: wangkaiping
 * @date: 2016年8月25日 下午8:32:47上午10:51:30
 */
public class User {
	@Column(mapField="id",name="id")
	private String id;
	@Column(mapField="name",name="name")
	private String name;
	@Column(mapField="age",name="age")
	private int age;
	@Column(mapField="remark",name="remark")
	private String remark;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

}

 

@Column( mapField=數據庫字段名稱,name=實體對象名稱)若是二者一致的話能夠省略括號內的內容

 

八、新建用戶接口和實現類(UserService.java,UserServiceImpl)

 

/*   
 * Project: OSMP
 * FileName: UserService.java
 * version: V1.0
 */
package com.osmp.demo.service.user;

import java.util.List;
import java.util.Map;

import com.osmp.demo.service.user.entity.User;
import com.osmp.jdbc.define.Page;

/**
 * Description:
 * 
 * @author: wangkaiping
 * @date: 2014年11月28日 下午5:27:33
 */
public interface UserService {

	public String getUserName(String name);

	public int getUserAge(int age);
	
	public int cudUser(Map<String,Object> map);
	
	public List<User> queryList(Map<String, Object> map);
	
	public Page<User> queryListByPage(Map<String, Object> map);

}

 

/*   
 * Project: OSMP
 * FileName: UserServiceImpl.java
 * version: V1.0
 */
package com.osmp.demo.service.user.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.osmp.jdbc.define.Page;
import com.osmp.jdbc.support.JdbcDao;
import com.osmp.cache.define.annotation.Cacheable;
import com.osmp.demo.service.user.UserService;
import com.osmp.demo.service.user.entity.User;

/**
 * Description:
 * 
 * @author: wangkaiping
 * @date: 2014年11月28日 下午5:28:39
 */
@Service
public class UserServiceImpl implements UserService {

	private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

	@Autowired
	private JdbcDao jdbcDao;
	
	@Override
	public String getUserName(String name) {
		logger.info("==============demo test =========="+name);
		 //jdbcDao.update("jwms.update.area.time", "jwms", new Date(),"app001301");
		return "個人名字叫:" + name;
	}

	@Override
	public int getUserAge(int age) {
		return age;
	}

	@Override
	public int cudUser(Map<String, Object> map) {
		String sqlId = (String) map.get("sqlId");
		String dbName = (String) map.get("dbName");
		return jdbcDao.update(sqlId, map, dbName);
	}

	@Override
	public List<User> queryList(Map<String, Object> map) {
		String sqlId = (String) map.get("sqlId");
		String dbName = (String) map.get("dbName");
		return jdbcDao.queryForList(sqlId, dbName, map, User.class);
	}
	
	@Override
	public Page<User> queryListByPage(Map<String, Object> map) {
		String sqlId = (String) map.get("sqlId");
		String dbName = (String) map.get("dbName");
		int page = (int) map.get("page");
		int size = (int) map.get("size");
		return jdbcDao.queryForPage(sqlId, dbName, map, User.class, page, size);
	}

}

 

 

 九、 新建 DemoServiceImpl.java 實現BaseDataServices,最後osgi發佈出的服務

 

/*   
 * Project: OSMP
 * FileName: DemoServiceImpl.java
 * version: V1.0
 */
package com.osmp.demo.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.osmp.demo.service.user.UserService;
import com.osmp.intf.define.model.Parameter;
import com.osmp.intf.define.service.BaseDataService;

/**
 * Description:
 * @author: wangkaiping
 * @date: 2016年11月9日 下午5:31:09上午10:51:30
 */
public class DemoServiceImpl implements BaseDataService{

	@Autowired
	private UserService service;
	
	@Override
	public Object execute(Parameter parameter) {
		Object result = null;
		String op = parameter.getQueryMap().get("op");
		Map<String,Object> paramMap = new HashMap<String, Object>();
		paramMap.putAll(parameter.getQueryMap());
		switch (op) {
		case "cud":
			result = service.cudUser(paramMap);
			break;
		case "query":
			result = service.queryList(paramMap);
			break;
		case "queryPage":
			result = service.queryListByPage(paramMap);
			break;
		default:
			break;
		}
		
		return result;
	}

}

 

  • 實現BaseDataService接口,重寫execute方法
  • 從方法的Parameter獲取入參 op 操做類型,並調用不一樣的方法完成crud
  • 這裏方法傳遞咱們使用map做爲參數傳遞

十、在src/main/resources/META-INF/spring 目錄下新建spring配置文件 bundle-context.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd">

	<context:component-scan base-package="com.osmp.demo.service">
	</context:component-scan>
	
	
	<bean id="jdbcDao" class="com.osmp.jdbc.support.JdbcDao"></bean>
	
	<bean id="osmp.demo.service" class="com.osmp.demo.service.DemoServiceImpl" />
	<osgi:service interface="com.osmp.intf.define.service.BaseDataService"
		ref="osmp.demo.service">
		<osgi:service-properties>
			<entry key="name" value="osmp-demo" />
			<entry key="mark" value="DEMO例子" />
		</osgi:service-properties>
	</osgi:service>

</beans>

 

  • 這裏咱們的JdbcDao類是從依賴osmp-jdbc組件的。並在DemoServiceImpl裏自動注入的。
  • 與傳統的spring相比,只是在spring命名空間裏新增了一個spring-osgi的namespaces
  • 經過osgi:service 將DemoServiceImpl 發佈爲一個osgi服務,服務名稱爲 osmp-demo 

十一、編寫sql腳本。demo.xml

 

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE sqls SYSTEM "osmp_sql.dtd">
<sqls>

	<select id="demo.queryuser">
		select * from demo_user 
		<where>
			<if test="name not empty">
				  and name=:name
			</if>
			<if test="age not empty">
				  and age=:age
			</if>
		</where>
	</select>
	
    <insert id="demo.adduser">
        insert into demo_user(id,name,age,remark) values(:id,:name,:age,:remark)  
    </insert>
    
    <delete id="demo.deluser">
	delete demo_user where id=:id
    </delete>
	
    <update id="demo.upuser">
	update demo_user set
        <if test="name not empty">
            name = :name
        </if>
        <if test="age not empty">
            name = :age
        </if>
        <if test="remark not empty">
            remark = :remark
        </if>
        <where>
        	id = :id
        </where>
    </update>
	
</sqls>

 

十二、編寫鏈接池配置文件 demo.osmp.jdbc.properties

 

osmp.jdbc.name=demo
osmp.jdbc.driverClassName=com.mysql.jdbc.Driver
osmp.jdbc.url=jdbc:mysql://10.2.1.9:3306/demo?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
osmp.jdbc.username=xxx
osmp.jdbc.password=xxx
osmp.jdbc.initialSize=5
osmp.jdbc.maxActive=100
osmp.jdbc.minIdle=5
osmp.jdbc.maxWait=3000
osmp.jdbc.validationQuery=select 1
osmp.jdbc.timeBetweenEvictionRunsMillis=100000
osmp.jdbc.minEvictableIdleTimeMillis=30000
osmp.jdbc.removeAbandonedTimeout=30000

 

 

1三、將sql腳本和 鏈接池配置文件分別上傳到${servicemix_home}/etc/sqls ${servicemix_home}/etc/datasources 目錄

1四、osmp-demo mvn install 將osmp-demo.jar上傳到${servicemix_home}/deploy 目錄

 

到此咱們的服務開發完畢。

寫道

新增一條數據

http://xxx.xxx.xxx.xxx:8181/cxf/service/ osmp-demo?source={"from":"test"}&parameter={"dbName":"demo","op":"cud","sqlId":"demo.adduser","id":"100000","name":"zhangsang","age":"20","remark":'woshizhangsan"}

查詢一條數據(根據名稱查詢)

http://xxx.xxx.xxx.xxx:8181/cxf/service/osmp-demo?source={"from":"test"}&parameter={"dbName":"demo","op":"query","sqlId":"demo.queryuser","name":"zhangsang"}

分頁查詢
http://xxx.xxx.xxx.xxx:8181/cxf/service/osmp-demo?source={"from":"test"}&parameter={"dbName":"demo","op":"queryPage","sqlId":"demo.queryuser","page":"1","size":"10"}

刪除
http://xxx.xxx.xxx.xxx:8181/cxf/service/osmp-demo?source={"from":"test"}&parameter={"dbName":"demo","op":"cud","sqlId":"demo.deluser","id":"100000"}

 

  • osmp-demo 服務名稱。對應上面spring配置文件裏發佈的osgi:service裏的 服務名稱 name
  • source={「from」:「test」} 這裏是OSMP規定的。用來標識請求來源的,能夠本身擴充 from 字段必須有。
  • parameter json參數
  • dbName:數據庫名稱 這裏咱們是demo
  • op:咱們在DemoServiceImpl裏定義的操做符
  • sqlId:對於咱們操行這個操做時須要使用到的sql腳本里的哪一個一個sql語句
  • 其它的參數根據業務本身定義。

 

這裏入參我是使用json的格式來定義的。能夠無限擴充。

當你作完這一切的時候差很少三分鐘的時候,你能夠定義一個通用的增刪改查服務。之後你只須要給一個sql的標識和一個數據庫的名稱。你就能夠獲得你想要的結果,根本只要會sql的就能夠開發。是否是感受有點兒意思了。。。。。

 

sql腳本基本上只要你能寫出來的語句。都應該能被解析執行。下面的一些sql例子是真實線上跑起來的。你們能夠看看。

 

 

 



 

 我真心以爲,osmp 已經被業務線給玩壞了。。。。

 

 感興趣的同窗能夠把上面的demo部署後在osmp-web裏進行實時管理。隨時隨地遠程任意的替換和平滑升級。。。

 

感受是否是開發一個osgi的服務是如此的簡單了呢。。不再用像eclipse那樣新建plugin  工程。一個 一個的去選依賴的組件。。。。。。

 

 

今天就先到這兒吧,下一節咱們會繼續解講osgi環境下集成cache組件。

相關文章
相關標籤/搜索