Your SlideShare is downloading. ×
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Using MyBatis in Alfresco custom extensions - Alfresco Devcon 2012 - Berlin

1,471

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,471
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Using MyBatis in Alfresco custom extensions Sé bastien Le Marchand, SQLI Group @slemarchand
  • 2. What about MyBatis ?Persistence framework • 11 years old, formerly known as iBatis • Maps methods to SQL statements • Doesn’t map Java objects to database table, unlike ORM (Hibernate etc)SQL statements manually defined using XML or annotations • Full control of SQL execution • Let’s use all DB functionality (views, stored procedures, etc) • Easy coding: statements are executed with a single line
  • 3. Alfresco & MyBatis: a growing love story !MyBatis usage in Alfresco • Since 2008 • Progressive Hibernate removal • Now only MyBatis are used (*)Technical gains • Stability • Very large deployments support • Performance improvements(*): Hibernate library stills in Alfresco for jBPM
  • 4. MyBatis in custom extension: use casesCustom access to existing tables • Often for tables outside of Alfresco’s meta-model • alf_activity_* • alf_audit_* • … • Often for custom search queries • When standard Alfresco services don’t provide itAccess to custom tables • Sometime new features don’t fit into Alfresco’s meta-model
  • 5. How-to, step-by-stepStep 1. Spring context • For MyBatis infrastructureStep 2. MyBatis configuration • To reference types and mappersStep 3. MyBatis mapper • Where SQL statements templates are storedStep 4. Call SQL session template • From you java codeStep 5. Deal with vendor-specific SQL • Only if needed
  • 6. Step 1. Spring context    <!-- MyBatis config -->        <bean id="my_extension_SqlSessionFactory" class="org.alfresco.ibatis.HierarchicalSqlSessionFactoryBean"> <property name="useLocalCaches" value="${mybatis.useLocalCaches}"/> <property name="resourceLoader" ref="dialectResourceLoader"/> <property name="dataSource" ref="activitiesDataSource"/> <property name="configLocation"> <value>classpath:alfresco/module/my_extension/ibatis/my-SqlMapConfig.xml</value> </property> </bean>    <!-- MyBatis-Spring sqlSessionTemplate -->    <bean id="my_extension_SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="my_extension_mySqlSessionFactory"/> </bean> alfresco/module/my_extension/ibatis/ibatis-context.xml
  • 7. Step 2. MyBatis configuration<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <typeAliases> <typeAlias alias="InputType" type="com.company.my_extension.InputType"/> <typeAlias alias="OutputType" type="com.company.my_extension.OutputType"/> </typeAliases> <mappers> <mapper resource="alfresco/module/my_extension/ibatis/#resource.dialect#/my-SqlMap.xml"/> </mappers>    </configuration> alfresco/module/my_extension/ibatis/my-SqlMapConfig.xml
  • 8. Step 3. MyBatis mapper<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="my_extension">  <select id="search_query1" parameterType="InputType" resultType="OutputType"> select post_user_id as userId, count(post_user_id) as count from alf_activity_post where site_network = #{site} group by post_user_id order by count desc; InputType OutputType </select></mapper> +site +userId +count alfresco/module/my_extension/ibatis/org.hibernate.dialect.Dialect/my-SqlMap.xml
  • 9. Step 4. Call SQL session template<bean id="my_extension_myDAO" class="com.company.my_extension.ibatis.MyDAOImpl"> <property name="template" ref= "my_extension_SqlSessionTemplate" /></bean> alfresco/module/my_extension/ibatis/my-SqlMapConfig.xmlprotected SqlSessionTemplate template;public final void setTemplate (SqlSessionTemplate sqlSessionTemplate) { this.template = sqlSessionTemplate;}public List<OutputType> executeSearchQuery1(InputType input) { List<OutputType> results = (List<OutputType>)template.selectList("my_extension.search_query1", input); return results;} com.company.my_extension.ibatis.MyDAOImpl
  • 10. Step 5. Deal with vendor-specific SQLOptional • Only needed if standard SQL doesn’t fitMultiple mappers files • Default file • …/ibatis/org.hibernate.dialect.Dialect/my-SqlMap.xml • Dedicated file for each dialect • …/ibatis/org.hibernate.dialect.MySQLInnoDBDialect/my-SqlMap.xml • …/ibatis/org.hibernate.dialect.PostgreSQLDialect/my-SqlMap.xml • …
  • 11. Real-world exampleSocial Tops extension • Dashlet showing "Top People" • based on their activities, • with configurable policy for scores calculation. • Opensource • GitHub repository • 2nd at 2012 Dashlet Challenge
  • 12. Inside Social Tops extension
  • 13. Best practices…Prefer high-level repository query language when possibleBe aware of vendor-specific SQLImplement schema upgrade patch for custom tablesUse Alfresco implementation as exampleUse MyBatis documentation
  • 14. Get this presentation from SlideShare.net Stay in touch on Twitter: @slemarchand Social Topshttp://slemarchand.github.com/social-tops SQLI Group http://entreprise-digitale.com http://sqli.com

×