SolveItproblems

SolveITProblems is a project of QFormat.net intellectual club

SolveITProblems это проект интеллектуального клуба QFormat.net

вторник, 2 апреля 2013 г.

Maven (install-file - adding library) + Spring MVC + FTL (Freemarker) + JDBC. Step By Step

So,  its time to "select" from database and using freemarker templates. We need 3 steps for ftl and 5 steps for jdbc. Lets begin from easyest - adding freemarker templates:

Step 1.

Adding dependancy to pom.xml





  <dependency>  
       <groupId>org.freemarker</groupId>  
       <artifactId>freemarker</artifactId>  
       <version>2.3.16</version>  
     </dependency>  

Step 2.

Add a new view resolver to our spring xml configuration:


  <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
    <property name="templateLoaderPath" value="/WEB-INF/templates/"/>  
   </bean>  
   <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
    <property name="cache" value="true"/>  
    <property name="prefix" value=""/>  
    <property name="suffix" value=".ftl"/>  
   </bean>  

/WEB-INF/templates - its a place where you store your ftl templates.

Step 3.

Thats it! Third step is adding ftl file to the templates directory. At our first topic (Maven + Spring MVC) about Spring we use jsp for view, here we have the same scenario but with ftl templates.

OK. Lets go to the JDBC. we need to run a "select".

Step 1.

Step 1 is the easyest :) Adding dependancy to pom.xml:


     <dependency>   
       <groupId>org.springframework</groupId>   
       <artifactId>spring-jdbc</artifactId>   
       <version>${org.springframework.version}</version>   
     </dependency>   
  <dependency>  
       <groupId>commons-dbcp</groupId>  
       <artifactId>commons-dbcp</artifactId>  
       <version>${commons-dbcp.version}</version>  
     </dependency>  
  <dependency>  
       <groupId>c3p0</groupId>  
       <artifactId>c3p0</artifactId>  
       <version>0.9.1</version>  
     </dependency>  

We haven't here dependancy for database driver. You should add it manually because each driver has its own group, artifact and version. But if you are going to use oracle, you should take a look to step 2.

Step 2.

You can't find Oracle db jdbc driver at maven repositories, so we need to add it manually to the maven. Download the jar file and run the following command:


 mvn install:install-file -Dfile=c:\pathToMyLib.jar -DgroupId=com.example.code   
 -DartifactId=example -Dversion={version} -Dpackaging=jar  

Step 3.

Spring uses DAO pattern. So we should create DAO interface and DAO Implementation.

 package com.azercell.web.restvcc.dao;  
 import java.util.List;  
 public interface TestDao {   
    public List getList();  
 }  
 ---  
 package com.azercell.web.restvcc.dao.impl;  
 import com.azercell.web.restvcc.dao.TestDao;  
 import java.sql.Connection;  
 import java.sql.ResultSet;  
 import java.util.ArrayList;  
 import java.util.List;  
 import javax.sql.DataSource;  
 import org.springframework.beans.factory.annotation.Autowired;  
 public class TestDaoImpl implements TestDao{  
   private DataSource dataSource;  
   public void setDataSource(DataSource dataSource)  
   {  
     this.dataSource=dataSource;  
   }  
   @Override  
   public List getList() {  
     ArrayList list=new ArrayList();  
     try {  
       Connection con = dataSource.getConnection();        
       ResultSet rs = con.createStatement().executeQuery("select * from (select * from example) where rownum < 15");  
       while (rs.next())  
       {  
         list.add(rs.getString("field"));  
       }  
     } catch (Exception e) {  
       System.out.println("Fuck :) "+e);  
     }  
     return list;  
   }  
 }  

So, our classes are ready to run the query.

Step 4.

The results of query will be shown by our ftl file through our controller. Lets prepare it:


 package com.azercell.web.restvcc.controllers;  
 import com.azercell.web.restvcc.dao.TestDao;  
 import com.azercell.web.restvcc.dao.impl.TestDaoImpl;  
 import java.util.ArrayList;  
 import java.util.Iterator;  
 import java.util.List;  
 import javax.annotation.Resource;  
 import javax.sql.DataSource;  
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.jdbc.core.JdbcTemplate;  
 import org.springframework.stereotype.Controller;  
 import org.springframework.web.bind.annotation.RequestMapping;  
 import org.springframework.web.servlet.ModelAndView;  
 @Controller  
 public class TestController {  
   private TestDao testBean;  
   public void setTestBean(TestDao testBean)  
   {  
     this.testBean=testBean;  
   }  
   @RequestMapping(value = "test")  
   public ModelAndView test() {  
     ModelAndView mav = new ModelAndView();     
     mav.addObject("listit", testBean.getList());      
     mav.setViewName("test");  
     return mav;  
   }  
 }  

To show our results in ftl a couple of lines will be enough:


 <table>  
 <#list listit as user>  
   <tr>  
     <td>${user}</td>  
   </tr>  
   </#list>  
 </table>  

Step 5.
Configure the spring and run it:


  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"  
       p:driverClass="oracle.jdbc.driver.OracleDriver" p:jdbcUrl="jdbc:oracle:thin:@precode.azercell.com:1522:ONSUBSTS"  
       p:user="subscription" p:password="subscription"     
   />  
   <bean id="testController" class="com.azercell.web.restvcc.controllers.TestController" >      
     <property name="testBean" ref="test" />  
   </bean>  
   <bean id="test" class="com.azercell.web.restvcc.dao.impl.TestDaoImpl" >      
     <property name="dataSource" ref="dataSource" />  
   </bean>  

Its not a full configuration file. Here 3 pieces of code: 1. - its our datasource (connection pool) 2. - our controller that gets a DAO 3. - our DAO implementation that gets a dataSource Ready :) .

1 комментарий:

  1. If you're using netbeans and embedded maven, better use netbeans own method:

    Project -> Dependencies -> (RightClick) Add Dependency (fill fields and OK) -> Then find the artifact in list, press Right Click and select - Manually install artifact.

    ОтветитьУдалить