2015年7月21日 星期二

resultMap的鍳別器 Mybatis3.x(五)

※resultMap的discriminator

為了練習用,再增加幾個欄位
ALTER TABLE CHESS ADD SCORE NUMBER(3);
ALTER TABLE CHESS ADD SCHOOL VARCHAR(20);
ALTER TABLE CHESS ADD SALARY NUMBER(7);
ALTER TABLE CHESS ADD COMPANY VARCHAR(20);
ALTER TABLE CHESS ADD CLASS VARCHAR(3);

COMMENT ON COLUMN CHESS.SCORE IS '學生學棋的分數';
COMMENT ON COLUMN CHESS.SCHOOL IS '學生的學校名稱';
COMMENT ON COLUMN CHESS.SALARY IS '員工製造棋的薪水';
COMMENT ON COLUMN CHESS.COMPANY IS '員工公司名稱';
COMMENT ON COLUMN CHESS.CLASS IS '分類:1學生 2員工';

或者乾脆重新建一張
CREATE TABLE CHESS (    
    CHESS_NO    NUMBER(10), 
    NAME    VARCHAR2(10), 
    PRICE    NUMBER(5), 
    PRODUCT_DATE    DATE, 
    SCORE    NUMBER(3), 
    SCHOOL    VARCHAR2(20), 
    SALARY    NUMBER(7), 
    COMPANY    VARCHAR2(20), 
    CLAZZ    VARCHAR2(3), 
    CONSTRAINT CHESS_PK PRIMARY KEY (CHESS_NO)
);
COMMENT ON COLUMN CHESS CHESS_NO IS '棋編號';
COMMENT ON COLUMN CHESS NAME IS '棋名稱';
COMMENT ON COLUMN CHESS PRICE IS '棋價錢';
COMMENT ON COLUMN CHESS PRODUCT_DATE IS '棋的生產日期';
COMMENT ON COLUMN CHESS SCORE IS '學生學棋的分數';
COMMENT ON COLUMN CHESS SCHOOL IS '學生的學校名稱';
COMMENT ON COLUMN CHESS SALARY IS '員工製造棋的薪水';
COMMENT ON COLUMN CHESS COMPANY IS '員工公司名稱';
COMMENT ON COLUMN CHESS CLAZZ IS '分類:1學生 2員工';
COMMENT ON TABLE  CHESS IS '棋表';

現在就是說新增的欄位CLAZZ可以區分成學生和員工兩部分,在資料庫是一張表,但在java把它區分開來,變三張表
因為CLAZZ是區分用的,所以要在原本的vo增加class,其他兩張分別是Student.java和Employee.java,把對應的兩個屬性放在裡面,然後給setter/getter,繼承一下原本的Chess.java
發現class在java是關鍵字,改一下
ALTER TABLE CHESS RENAME COLUMN CLASS TO CLAZZ;

三張java表如下:
Chess.java
public class Chess {
    private Integer chessNo;
    private String name;
    private Integer price;
    private Date productDate;
    private String clazz;
}
//setter/getter

Student.java
public class Strudent extends Chess {
    private int score;
    private String school;
}
//setter/getter

Employee.java
public class Employee extends Chess {
    private int salary;
    private String company;
}
//setter/getter

重點來了,Chess.xml
<sql id="column">
    CHESS_NO, NAME, PRICE, PRODUCT_DATE, CLAZZ
</sql>

<resultMap type="Chess" id="ChessInterface">
    <id property="chessNo" column="CHESS_NO" />
    <result property="name" column="NAME" />
    <result property="price" column="PRICE" />
    <result property="productDate" column="PRODUCT_DATE" />
    
    <discriminator javaType="java.lang.String" column="CLAZZ">
        <case value="1" resultType="Student">
            <result property="score" column="SCORE"/>
            <result property="school" column="SCHOOL"/>
        </case>
        <case value="2" resultType="Employee">
            <result property="salary" column="SALARY"/>
            <result property="company" column="COMPANY"/>
        </case>
    </discriminator>
</resultMap>

<insert id="insertStudent" parameterType="Chess">
    <selectKey keyProperty="chessNo" order="BEFORE" resultType="java.lang.Integer">
        SELECT CHESS_SEQUENCE.NEXTVAL FROM DUAL
    </selectKey>
    INSERT INTO CHESS(
        <include refid="column" />, SCORE, SCHOOL
    )
    VALUES(#{chessNo}, #{name}, #{price}, #{productDate}, #{clazz}, #{score}, #{school})
</insert>

<insert id="insertEmployee" parameterType="Chess">
    <selectKey keyProperty="chessNo" order="BEFORE" resultType="java.lang.Integer">
        SELECT CHESS_SEQUENCE.NEXTVAL FROM DUAL
    </selectKey>
    INSERT INTO CHESS(
        <include refid="column" />, SALARY, COMPANY
    )
    VALUES(#{chessNo}, #{name}, #{price}, #{productDate}, #{clazz}, #{salary}, #{company})
</insert>
測試類沒什麼特別(clazz可以用enum去設計較漂亮):
Student stu = new Student();
stu.setName("孔明棋");
stu.setPrice(20);
stu.setProductDate(new Date());
stu.setClazz("1");
stu.setScore(70);
stu.setSchool("諸葛亮小學");
System.out.println(sqlSession.insert("Chess.insertStudent", stu));

Employee emp = new Employee();
emp.setName("孔明棋");
emp.setPrice(20);
emp.setProductDate(new Date());
emp.setClazz("2");
emp.setSalary(50000);
emp.setCompany("小棋玩具社");
System.out.println(sqlSession.insert("Chess.insertEmployee", emp));

沒有留言:

張貼留言