zoukankan      html  css  js  c++  java
  • MyBatis(3.2.3)

    In the SQL Mapper configuration file, we need to give the fully qualified name of the JavaBeans for the resultType and parameterType attributes.

    An example is as follows:

    <select id="findStudentById" parameterType="int" resultType="com.mybatis3.domain.Student">
        SELECT STUD_ID AS ID, NAME, EMAIL, DOB FROM STUDENTS WHERE STUD_ID=#{Id}
    </select>
    <update id="updateStudent" parameterType="com.mybatis3.domain.Student">
        UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email}, DOB=#{dob} WHERE STUD_ID=#{id}
    </update>

    Here we are giving the fully qualified name of the Student type com.mybatis3.domain.Student for the resultType and parameterType attributes.

    Instead of typing the fully qualified names everywhere, we can give the alias names and use these alias names in all the other places where we need to give the fully qualified names.

    An example is as follows:

    <typeAliases>
        <typeAlias alias="Student" type="com.mybatis3.domain.Student"/>
        <typeAlias alias="Tutor" type="com.mybatis3.domain.Tutor"/>
        <package name="com.mybatis3.domain"/>
    </typeAliases>

    Now in the SQL Mapper file, we can use the alias name Student as follows:

    <select id="findStudentById" parameterType="int" resultType="Student">
        SELECT STUD_ID AS ID, NAME, EMAIL, DOB FROM STUDENTS WHERE STUD_ID=#{id}
    </select>
    <update id="updateStudent" parameterType="Student">
        UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email}, DOB=#{dob} WHERE STUD_ID=#{id}
    </update>

    Instead of giving an alias name for each JavaBeans separately, you can give the package name where MyBatis can scan and register aliases using uncapitalized, nonqualified class names of the Bean.

    An example is as follows:

    <typeAliases>
        <package name="com.mybatis3.domain"/>
    </typeAliases>

    If there are Student.java and Tutor.java Beans in the com.mybatis3.domain package, com.mybatis3.domain.Student will be registered as student and com.mybatis3.domain.Tutor will be registered as tutor.

    An example is as follows:

    <typeAliases>
        <typeAlias alias="Student" type="com.mybatis3.domain.Student"/>
        <typeAlias alias="Tutor" type="com.mybatis3.domain.Tutor"/>
        <package name="com.mybatis3.domain"/>
        <package name="com.mybatis3.webservices.domain"/>
    </typeAliases>

    There is another way of aliasing JavaBeans, using the @Alias annotation.

    @Alias("StudentAlias")
    public class Student {
        // ...  
    }

    The @Alias annotation overrides the <typeAliases> configuration.

  • 相关阅读:
    (转)tomcat与地址栏图标之研究(多浏览器)
    (转)Android学习笔记---SQLite介绍,以及使用Sqlite,进行数据库的创建,完成数据添删改查的理解
    (转) Tomcat部署Web应用方法总结
    (转)mysql账号权限密码设置方法
    (转) mysql的连接,创建账号,修改密码
    (转)mysql各个主要版本之间的差异
    (转)浅析Mysql的my.ini文件
    (转)如何在Windows上安装多个MySQL
    [笔记]线段树学习笔记
    [教程]对拍程序(linux)+ 考试(做题)生成数据 + 提交注意事项
  • 原文地址:https://www.cnblogs.com/huey/p/5227538.html
Copyright © 2011-2022 走看看