1. 程式人生 > 其它 >linux系統命令使用者管理

linux系統命令使用者管理

開頭

註解開發的主配置檔案仍舊與之前相同,註解開發是面向於dao的。
某個dao只能使用註解開發,或配置檔案中的一種,不能同時使用
但可以ADao使用註解開發,BDao使用配置檔案開發

一、簡單的增刪改查

package com.czy.dao;

import com.czy.domain.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UserDao {
    /**
     * 查詢所有使用者
     * @return
     */
    @Select("select * from user")
    List<User> findAll();

    /**
     * 儲存使用者
     * @param user
     */
    @Insert("insert into user(username,address,sex,birthday) values(#{username},#{address},#{sex},#{birthday})")
    void saveUser(User user);

    @Update("update user set username = #{username},address = #{address},sex = #{sex},birthday = #{birthday} where id = #{id}")
    void updateUser(User user);

    @Delete("delete from user where id = #{id}")
    void deleteUser(Integer userId);

    @Select("select * from user where id = #{id}")
    User findById(Integer id);

    @Select("select * from user where username like '%${value}%'")
    List<User> findUserByName(String name);

    @Select("select count(*) from user")
    int findTotalUser();
}

二、註解實現複雜關係對映開發

1、屬性和資料庫列不對應關係(ResultMap)

    /**
     * 查詢所有使用者
     * @return
     */
    @Select("select * from user")
    @Results(value= {
            @Result(id=true ,column = "id", property = "userId"),
            @Result(column = "username", property = "userName"),
            @Result(column = "address", property = "userAddress"),
            @Result(column = "sex", property = "userSex"),
            @Result(column = "birthday", property = "userBirthday")
    },id="userMap"
    )
    List<User> findAll();


    @Select("select * from user where id = #{id}")
    @ResultMap({"userMap"})
    User findById(Integer id);

Results註解原始碼

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.apache.ibatis.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Results {
    String id() default "";

    Result[] value() default {};
}

Result註解原始碼

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.apache.ibatis.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.UnknownTypeHandler;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Result {
    boolean id() default false;

    String column() default "";

    String property() default "";

    Class<?> javaType() default void.class;

    JdbcType jdbcType() default JdbcType.UNDEFINED;

    Class<? extends TypeHandler> typeHandler() default UnknownTypeHandler.class;

    One one() default @One;

    Many many() default @Many;
}

ResultMap註解原始碼

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.apache.ibatis.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface ResultMap {
    String[] value();
}

2、多表查詢

一個使用者可以有多個賬戶,每個賬戶對應單個使用者
多對一(mybatis中的一對一)
自我實現

    /**
     * 查詢所有賬戶並獲取其所屬的使用者資訊
     * @return
     */
    @Select("select user.*, a.id aid, a.uid uid,a.money money from account a,user where a.uid = user.id")
    @Results(id = "accountMap",value = {
            @Result(id = true,column = "aid",property = "id"),
            @Result(column = "uid",property = "uid"),
            @Result(column = "money",property = "money"),
            @Result(column = "id",property = "user.userId"),
            @Result(column = "username",property = "user.userName"),
            @Result(column = "address",property = "user.userAddress"),
            @Result(column = "sex",property = "user.userSex"),
            @Result(column = "birthday",property = "user.userBirthday")
    })
    List<Account> findAll();

mybatis封裝實現

    /**
     * 查詢所有賬戶並獲取其所屬的使用者資訊
     * @return
     */
    @Select("select * from account")
    @Results(id = "accountMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "uid",property = "uid"),
            @Result(column = "money",property = "money"),
            @Result(property = "user",column = "uid",one = @One(select = "com.czy.dao.UserDao.findById",fetchType = FetchType.EAGER))
    })
    List<Account> findAll();

通過one標籤實現
one標籤原始碼

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.apache.ibatis.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.ibatis.mapping.FetchType;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface One {
    String select() default "";

    FetchType fetchType() default FetchType.DEFAULT;
}

其中的Fetch即為預載入(lazy)和立即載入(eager)的設定

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.apache.ibatis.mapping;

public enum FetchType {
    LAZY,
    EAGER,
    DEFAULT;

    private FetchType() {
    }
}

一對多

  @Select("select * from user")
    @Results(value= {
            @Result(id=true ,column = "id", property = "userId"),
            @Result(column = "username", property = "userName"),
            @Result(column = "address", property = "userAddress"),
            @Result(column = "sex", property = "userSex"),
            @Result(column = "birthday", property = "userBirthday"),
            @Result(property = "accounts",column = "id",many = @Many(select = "com.czy.dao.AccountDao.findByUid",fetchType = FetchType.LAZY))
    },id="userMap"
    )
    List<User> findAll();

many原始碼類似於One