Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start myBatis implemention with spring?

Tags:

spring

mybatis

I want to work with myBatis. I have read MyBatis-3-user-guide. Now i am trying to implement it. Recently i have learned spring. So it is difficult for me to implement it. So i need some helpful resource by which i can implement it step by step.

like image 381
Biswas Avatar asked Dec 22 '22 03:12

Biswas


2 Answers

Add the MyBatis-Spring jar in your class path at first

Start with your spring context file.In your context file add following lines

<beans:bean id="dataSource"
              class="org.springframework.jdbc.datasource.DriverManagerDataSource"
              p:driverClassName="yourDriverClassName"
              p:url="yourUrl"
              p:username="yourUsername"
              p:password="yourPassword" />

<beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="configLocation" value="/WEB-INF/mybatis-config.xml" />
</beans:bean> 
<beans:bean id="userDao" class="com.yourcomp.dao.Userdao">
    <beans:property name="sqlSessionFactory" ref="sqlSessionFactory" />
</beans:bean>

your mybatis-config.xml should be something like this:

<?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>
  <settings>

  </settings>
  <typeAliases>

    <typeAlias alias="User" type ="com.yourcomp.domain.User" />

  </typeAliases>
  <mappers>

    <mapper resource="com/yourcomp/domain/UserMapper.xml"/>
    <mapper resource="com/yourcomp/domain/AnotherDomainObjectMapper.xml"/>
  </mappers>

</configuration>

and your userMapper.xml in src/com/yourcomp/domain/ might be something like this

<?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="org.pbl.rms.RMSUserDao">-->

<mapper namespace="com.yourcomp.domain.User">

  <resultMap id="userMap" type="User">
  <id property="userId" column="USER_ID" javaType="int" jdbcType="NUMERIC"/>
  <result property="userName"  column="USER_NAME" javaType="String" jdbcType="VARCHAR"/>
  <result property="userFullName"  column="USER_FULL_NAME" javaType="String" jdbcType="VARCHAR"/>
  <result property="password"  column="PASSWORD" javaType="String" jdbcType="VARCHAR"/>
  <result property="passwordExpiryDate"  column="PASWRD_EXPIRY_DATE" javaType="java.util.Date" jdbcType="DATE"/>
  <result property="status" column="STATUS" javaType="_integer" jdbcType="DECIMAL"/>
  </resultMap>

  <select id="getUserById" parameterType="map" resultMap="userMap">
  select  * from user where USER_ID=#{userId}
  </select>

</mapper>

now in your DAO layer you might have class like follows:

public class UserDAO{

    private SqlSessionFactory sqlSessionFactory;

    public UserDAO() {

    }

    public UserDAO(SqlSessionFactory sqlSessionFactory ) {
        this.sqlSessionFactory = sqlSessionFactory;

    }
    public String getUserById(Integer userId) {
        SqlSession session = sqlSessionFactory.openSession();

        String name=null;
        try {
            name = (String)session.selectOne("com.yourcomp.domain.User.getUserById",userId);
        }catch(Exception e){

        }finally {
            session.close();
        }
        return name;
    }
}
like image 200
Moinul Hossain Avatar answered Jan 12 '23 14:01

Moinul Hossain


You need MyBatis-Spring for this . See the reference here and here These provide you the step by step configs . If you need any specific details , you need to re frame your query .

Also check the spring reference for IBatis support.

like image 29
Aravind A Avatar answered Jan 12 '23 14:01

Aravind A