|
DatabaseConfigurationForAFeed
Steps to setup a sample feed, adapter configuration along with database configuration details
Database Configuration for a FeedThis document describes how to create a feed configuration, adpater configuration and configure database settings.Contents
Creating a Mapping File in iBATIS FormatIn the conf/feedserver/sqlmap/ folder, create a mapping file for iBATIS to map Java objects to database queries and results on the table and view. For example: contact.xml mapping file: <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="test">
<resultMap id="result-map" class="java.util.HashMap">
<result property="id" column="id"/>
<result property="firstName" column="first_name"/>
<result property="lastName" column="last_name"/>
<result property="email" column="email"/>
<result property="rating" column="rating"/>
</resultMap>
<select id="contact-get-feed" resultMap="result-map">
select * from Contact
</select>
<select id="contact-get-entry" resultMap="result-map">
select * from Contact where id = #value#
</select>
<delete id="contact-delete-entry" >
delete from Contact where id = #value#
</delete>
<insert id="contact-insert-entry" parameterClass="map">
insert into Contact (first_name,last_name,email,rating) values
(#firstName#,#lastName#,#email#,#rating:NUMERIC#)
<selectKey keyProperty="id" resultClass="long">
<!-- For MySQL: SELECT LAST_INSERT_ID()-->
<!-- For Derby: --> values IDENTITY_VAL_LOCAL()
<!-- For PostGRE SQL: SELECT currval('"contact_id_seq"') -->
</selectKey>
</insert>
<update id="contact-update-entry" parameterClass="map">
update Contact set last_name = #lastName#, first_name = #firstName#,
email = #email#, rating = #rating:NUMERIC# where id = #id#
</update>
</sqlMap>Note: All the id attributes in the previous XML file use the following naming conventions:
The name of the file should be <feedname> for easy mapping and understanding. Creating an XML File with Database Connection Parameters for iBATISThe following example creates the sqlmap.xml file: <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<properties resource="database/dbConfig.properties"/>
<typeHandler javaType="string" jdbcType="NUMERIC"
callback="com.google.feedserver.ibatisCallbackHandlers.StringToNumericCallback" />
<!-- Configure a built-in transaction manager. If you're using an
app server, you probably want to use its transaction manager
and a managed datasource -->
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${JDBC.Driver}"/>
<property name="JDBC.ConnectionURL" value="${JDBC.ConnectionURL}"/>
<property name="JDBC.Username" value="${JDBC.Username}"/>
<property name="JDBC.Password" value="${JDBC.Password}"/>
<property name="Pool.MaximumActiveConnections" value="${Pool.MaximumActiveConnections}"/>
<property name="Pool.MaximumIdleConnections" value="${Pool.MaximumIdleConnections}"/>
<property name="Pool.MaximumCheckoutTime" value="${Pool.MaximumCheckoutTime}"/>
<property name="Pool.TimeToWait" value="${Pool.TimeToWait}"/>
</dataSource>
</transactionManager>
</sqlMapConfig>The database/dbConfig.properties file contains the actual values, which are also used by the Ant build script to create the database. The reference to contact.xml is inserted by the wrapper at runtime as shown below: <sqlMap resource="feedserver/sqlmap/contact.xml"/> The value sqlmap/contact.xml is specified as part of feed configuration as explained in the following sections. Creating an Adapter Properties FileCreate an adapter properties file in conf/feedserver/{namespace}/Adapter/ dir. For example: IBatisAdapter.properties className=com.google.feedserver.samples.adapters.IBatisCollectionAdapter isWrapper=false className is the fully qualified name of the adapter class that will interact with the datasource to perform CRUD operations isWrapper indicates whether the given adapter is a wrapper or simply an adapter Creating an Adapter Configuration Properties FileCreate an adapter configuration properties file in the conf/feedserver/{namespace}/AdapterConfig/ folder. For example: jdbcAdapterConfig.properties type=IBatisAdapter configValueType=xml configValue=@sqlmap.xml mixins=@IBatisAdapterWrapper Attributes:
The content type (xml, text, etc) should be as specified by 'configValueType'. If the value starts with '@', it implies that the value following '@' should be treated as the name of the file whose contents will be treated as configValue. The feed config store will have the responsibility of reading the file contents and setting them as the config value while loading the adapter config values.
Creating a Wrapper ConfigCreate an XML file under conf/feedserver/{namespace}/AdapterConfig/ directory For example: IBatisAdapterWrapper.xml <?xml version="1.0" encoding="UTF-8"?>
<mixin>
<wrapperName>IBatisAdapterWrapper</wrapperName>
<wrapperConfig></wrapperConfig>
</mixin>Attributes:
Create a IBatisAdapterWrapper.properties under conf/feedserver/{namespace}/Adapter/ directory having the wrapper details. className=com.google.feedserver.wrappers.IBatisAdapterWrapper configValue="" configType=XML isWrapper=true Attributes:
Creating a Feed Properties FileCreate a feed properties file in conf/feedserver/{namespace}/FeedConfig/ folder. For example: contact.properties subUri=contact title=feed author=FeedServer adapterName=jdbcAdapterConfig configValue=@sqlmap/contact.xml Attributes:
This attribute acts as the link between the feed and adapter configurations. The feed config store loads adapter values automatically for the specified adapter name while loading the feed configuration values.
If a value starts with '@', the value after '@' is the name of the file. The feed config store either adds the contents of file to the adapter 'configValue' or adds the name of the file. |