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

    MyBatis supports persisting enum type properties out of the box. Assume that the STUDENTS table has a column gender of the type varchar to store either MALE or FEMALE as the value. And, the Student object has a gender property that is of the type enum as shown in the following code:

    public enum Gender {
        FEMALE,
        MALE;
    }

    By default, MyBatis uses EnumTypeHandler to handle enum type Java properties and stores the name of the enum value. You don't need any extra configuration to do this. You can use enum type properties just like primitive type properties as shown in the following code:

    public class Student {
        private Integer id;
        private String name;
        private String email;
        private PhoneNumber phone;
        private Address address;
        private Gender gender;
        
        //setters and getters
    }
    <insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="id">
        insert into students(name,email,addr_id, phone,gender) values(#{name},#{email},#{address.addrId},#{phone},#{gender})
    </insert>

    When you execute the insertStudent statement, MyBatis takes the name of the Gender enum (FEMALE/MALE) and stores it in the GENDER column.

    If you want to store the ordinal position of the enum instead of the enum name, you will need to explicitly configure it.

    So if you want to store 0 for FEMALE and 1 for MALE in the gender column, you'll need to register EnumOrdinalTypeHandler in the mybatis-config.xml file.

    <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.mybatis3.domain.Gender"/>

    Be careful to use ordinal values to store in the DB. Ordinal values are assigned to enum values based on their order of declaration. If you change the declaration order in Gender enum, the data in the database and ordinal values will be mismatched.

  • 相关阅读:
    “显示桌面”代码
    Jquery 判断CheckBox是否选中
    Jquery 得到隐藏列的值
    Jquery 得到DataGrid单击单元格后得到主键列值
    RadioButtonList的项增加onClick事件
    正则表达式 替换除中文、字母、数字以外的字符
    正则表达式中 中文 Unicode字符(转)
    Jquery 设置table、DataGrid等的某列单击的方法
    GridView自动生成列的隐藏
    AutoCAD利用VB交互创建应用程序交互
  • 原文地址:https://www.cnblogs.com/huey/p/5231519.html
Copyright © 2011-2022 走看看