2019-08-27 Mapping

EntityMapping

package com.foreknow.mapping;

import java.sql.ResultSet;
import java.sql.SQLException;

public interface EntityMapping {
/**
 * 是要将结果集(ResultSet)中的数据映射为一个对象
 */
    public Object mapping(ResultSet rs)throws SQLException;
}

AdminMapping

package com.foreknow.mapping;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.foreknow.model.Admin;

public class AdminMapping implements EntityMapping{

    @Override
    public Object mapping(ResultSet rs) throws SQLException {
        Admin admin = new Admin();
        admin.setId(rs.getInt("id"));
        admin.setUsername(rs.getString("username"));
        admin.setPasword(rs.getString("password"));
        return admin;
    }

}

MappingFactory

package com.foreknow.mapping;


import java.util.HashMap;
import java.util.Map;


public class MappingFactory {
    private static MappingFactory mappingFactory = null;
    private Map maps = new HashMap();
    private MappingFactory(){
        
    }
    public static MappingFactory getInstance(){
        if (mappingFactory == null) {
            mappingFactory = new MappingFactory();
            mappingFactory.maps.put("adminMapping", new AdminMapping());
        }
        return mappingFactory;
    }
    
    //从集合当中获取xxxMapping对象
    public EntityMapping getMapping(String key){
        return maps.get(key);
    }
}

你可能感兴趣的:(2019-08-27 Mapping)