SolveItproblems

SolveITProblems is a project of QFormat.net intellectual club

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

четверг, 4 апреля 2013 г.

Maven + Spring MVC + MyBatis. Step By Step

It's time to attach orm to our project. Only 5 steps.

Step 1.

POM dependency:





   <dependency>   
       <groupId>org.springframework</groupId>   
       <artifactId>spring-orm</artifactId>   
       <version>${org.springframework.version}</version>   
     </dependency>   
      <dependency>  
       <groupId>org.mybatis</groupId>  
       <artifactId>mybatis</artifactId>  
       <version>${mybatis.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.mybatis</groupId>  
       <artifactId>mybatis-spring</artifactId>  
       <version>1.2.0</version>  
     </dependency>  

Lets change the location of our resource files too, mybatis need some xml configurations.

  <resources>  
       <resource>  
         <directory>src/main/resources</directory>  
         <filtering>true</filtering>  
         <includes>  
           <include>**/*.xml</include>  
         </includes>  
       </resource>  
     </resources>  

After this, you can store all your xml configurations here.

Step 2.

Java Code. We need to add a new class (like DAO) which will call our queries and make some changes at our controller. First i'll show our new class.

 package com.test.web.restvcc.dao.impl;  
 import javax.annotation.Resource;  
 import org.apache.ibatis.session.SqlSessionFactory;  
 import org.mybatis.spring.SqlSessionFactoryBean;  
 import org.mybatis.spring.SqlSessionTemplate;  
 import org.springframework.orm.ibatis.SqlMapClientCallback;  
 import org.springframework.orm.ibatis.SqlMapClientTemplate;  
 import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;  
 /**  
  *  
  * @author Vusal J. Khalilov  
  */  
 public class BatisTest {  
   private SqlSessionTemplate sqlSession;  
   @Resource(name = "sqlSessionFactory")  
   public final void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {  
     this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);  
   }  
   public String yeap()  
   {          
     return sqlSession.selectOne("getCount","param").toString();      
   }  
 }  

Then take a look to controller:


 package com.test.web.restvcc.controllers;  
 import com.test.web.restvcc.dao.TestDao;  
 import com.test.web.restvcc.dao.impl.BatisTest;  
 import com.test.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;  
   private BatisTest batisTest;  
   public void setBatisTest(BatisTest batisTest) {  
     this.batisTest = batisTest;  
   }  
   public void setTestBean(TestDao testBean)  
   {  
     this.testBean=testBean;  
   }  
   @RequestMapping(value = "test")  
   public ModelAndView test() {  
     ModelAndView mav = new ModelAndView();  
     mav.addObject("Expression", "LookAndFell");         
     mav.addObject("listit", testBean.getList());  
     mav.addObject("yeap", batisTest.yeap());  
     mav.setViewName("test");  
     return mav;  
   }  
 }  

Step 3.
Go to XML Configuration:
Lets begin from spring configuration:


  <bean id="testController" class="com.test.web.restvcc.controllers.TestController" >      
     <property name="testBean" ref="test" />  
     <property name="batisTest" ref="BatisTest" />  
   </bean>  
  <bean id="sqlSessionFactory"  
      class="org.mybatis.spring.SqlSessionFactoryBean">  
     <property name="configLocation" value="classpath:iBatisConfig.xml" />  
     <property name="dataSource" ref="dataSource" />  
   </bean>    
  <bean id="BatisTest" class="com.test.web.restvcc.dao.impl.BatisTest" >  
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
  </bean>  

And we need 2 confuguration files of mybatis:


 <?xml version="1.0" encoding="UTF-8" ?>  
 <!DOCTYPE configuration  
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
   "http://mybatis.org/dtd/mybatis-3-config.dtd">  
 <configuration>  
   <mappers>  
     <mapper resource="Test.xml"/>      
   </mappers>  
 </configuration>  

 <?xml version="1.0" encoding="UTF-8" ?>  
 <!DOCTYPE mapper  
     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
 <mapper namespace="Test">  
   <select id="getCount" resultType="String" parameterType="String">  
     select id from customers where param=#{id}  
   </select>  
 </mapper>  

Step 4.
One line of code at our test.ftl and running :)


 <h1>--${yeap}--</h1>  

I really like the Spring :) Hope you too...

Комментариев нет:

Отправка комментария