XML + Annotation based configuration for MyBatis

前端 未结 2 2039
挽巷
挽巷 2021-01-16 04:06

Is it possible to have both the XML + Annotation based configuration for MyBatis in an application.

The reason that I am asking this is because, in my application I

相关标签:
2条回答
  • 2021-01-16 04:45

    Its possible to have both XML + Annotation based configuration

    For xml based configuration:

    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>
    <!-- assuming you have a dao named UserDao -->
    <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();
            //int success = -100;
            String name=null;
            try {
                name = (String)session.selectOne("com.yourcomp.domain.User.getUserById",userId);
            }catch(Exception e){
    
            }finally {
                session.close();
            }
            return name;
        }
    }
    

    Now For using annotation based configuration add the followings:

    You might have an interface like follows:

    public interface MyMapper {
        @Select(value="select something from some_table")
        public String getSomeValue(@Param("someParam") String someParam);
    
    }
    

    now add the followings to your context file:

    <beans:bean id="annotatedMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
      <beans:property name="mapperInterface" value="com.yourcomp.MyMapper" />
      <!-- remember the sqlsession factory defined earlier for xml based conf ? -->
      <beans:property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </beans:bean>
    

    Hope this helps :)

    0 讨论(0)
  • 2021-01-16 04:48

    The below tags can also be used in annotation based approach.
    <foreach> </foreach>

    In that case you won't require two mapper files.

    0 讨论(0)
提交回复
热议问题