编程语言
首页 > 编程语言> > JavaWeb同步学习笔记之三十八、JavaWeb_MVC案例之通过配置切换底层存储源

JavaWeb同步学习笔记之三十八、JavaWeb_MVC案例之通过配置切换底层存储源

作者:互联网

JavaWeb_MVC案例之通过配置切换底层存储源

MVC案例之通过配置切换底层存储源

#type=xml
type=jdbc

.
   2)CustomerServlet 中不能在通过 private CustomerDAO customerDAO = new CustomerDAOXMLImpl(); 的方式来写死实现类。
   3)需要通过一个类的一个方法来获取具体的实现类的对象。
   
先新建一个CustomerDAOXMLImpl实现类(没有具体实现,只写了输出的方法,以供测试)。

/**  
 * All rights Reserved,Designed By XS
 * @Title: CustomderDAOXMLImpl.java
 * @Package com.xs.mvcapp.dao.impl
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月7日 下午9:03:36
 * @version V1.0
 */
package com.xs.mvcapp.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import com.xs.mvcapp.dao.CriteriaCustomer;
import com.xs.mvcapp.dao.CustomerDAO;
import com.xs.mvcapp.domain.Customer;

/**   
 * @ClassName: CustomderDAOXMLImpl
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月7日 下午9:03:36
 * @version V1.0
 */
public class CustomerDAOXMLImpl implements CustomerDAO {

	/**   
	 * <p>Title: getForListWitCriteriaCustomer</p>
	 * <p>Description: </p>
	 * @see com.xs.mvcapp.dao.CustomerDAO#getForListWitCriteriaCustomer(com.xs.mvcapp.dao.CriteriaCustomer)
	 * @param cc
	 * @return
	 */
	@Override
	public List<Customer> getForListWitCriteriaCustomer(CriteriaCustomer cc) {
		System.out.println("getForListWitCriteriaCustomer");
		return null;
	}

	/**   
	 * <p>Title: getAll</p>
	 * <p>Description: </p>
	 * @see com.xs.mvcapp.dao.CustomerDAO#getAll()
	 * @return
	 */
	@Override
	public List<Customer> getAll() {
		System.out.println("getAll");
		return null;
	}

	/**   
	 * <p>Title: save</p>
	 * <p>Description: </p>
	 * @see com.xs.mvcapp.dao.CustomerDAO#save(com.xs.mvcapp.domain.Customer)
	 * @param customer
	 */
	@Override
	public void save(Customer customer) {
		System.out.println("save");
	}

	/**   
	 * <p>Title: get</p>
	 * <p>Description: </p>
	 * @see com.xs.mvcapp.dao.CustomerDAO#get(int)
	 * @param id
	 * @return
	 */
	@Override
	public Customer get(int id) {
		System.out.println("get");
		return null;
	}

	/**   
	 * <p>Title: delete</p>
	 * <p>Description: </p>
	 * @see com.xs.mvcapp.dao.CustomerDAO#delete(int)
	 * @param id
	 */
	@Override
	public void delete(int id) {
		System.out.println("id");
	}

	/**   
	 * <p>Title: getCountWithName</p>
	 * <p>Description: </p>
	 * @see com.xs.mvcapp.dao.CustomerDAO#getCountWithName(java.lang.String)
	 * @param name
	 * @return
	 */
	@Override
	public BigDecimal getCountWithName(String name) {
		System.out.println("getCountWithName");
		return null;
	}

	/**   
	 * <p>Title: update</p>
	 * <p>Description: </p>
	 * @see com.xs.mvcapp.dao.CustomerDAO#update(com.xs.mvcapp.domain.Customer)
	 * @param customer
	 */
	@Override
	public void update(Customer customer) {
		System.out.println("update");
	}
}

然后新建一个CustomerDAOFactory类。

/**  
 * All rights Reserved,Designed By XS
 * @Title: CustomerDAOFactory.java
 * @Package com.xs.mvcapp.dao
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月7日 下午9:16:24
 * @version V1.0
 */
package com.xs.mvcapp.dao;

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

import com.xs.mvcapp.dao.impl.CustomerDAOJdbcImpl;
import com.xs.mvcapp.dao.impl.CustomerDAOXMLImpl;

/**   
 * @ClassName: CustomerDAOFactory
 * @Description: TODO
 * @author: XS
 * @date: 2019年3月7日 下午9:16:24
 * @version V1.0
 */
public class CustomerDAOFactory {
	
	private Map<String, CustomerDAO> daos = new HashMap<String, CustomerDAO>();
	
	private CustomerDAOFactory() {
		daos.put("jdbc", new CustomerDAOJdbcImpl());
		daos.put("xml", new CustomerDAOXMLImpl());
	}
	
	private static CustomerDAOFactory instance = new CustomerDAOFactory();
	
	public static CustomerDAOFactory getInstance() {
		return instance;
	}
	
	private String type = null;
	
	public void setType(String type) {
		this.type = type;
	}
	
	public CustomerDAO getCustomerDAO() {
		return daos.get(type);
	}
}

在Web应用启动时,为了初始化type,新建一个InitServlet。

package com.xs.mvcapp.servlet;

import java.io.InputStream;
import java.util.Properties;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import com.xs.mvcapp.dao.CustomerDAOFactory;

/**
 * Servlet implementation class InitServlet
 */
public class InitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see Servlet#init(ServletConfig)
	 */
	@Override
	public void init() throws ServletException {
		
		InputStream in = getServletContext().getResourceAsStream("/WEB-INF/classes/switch.properties");
		Properties properties = new Properties();
		
		try {
			properties.load(in);
			String type = properties.getProperty("type");
			CustomerDAOFactory.getInstance().setType(type);
			
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
}

修改web.xml文件,使InitServlet在Web应用被启动是加载。

	<servlet>
		<servlet-name>InitServlet</servlet-name>
    	<servlet-class>com.xs.mvcapp.servlet.InitServlet</servlet-class>
    	<load-on-startup>1</load-on-startup>
	</servlet>

当前 WEB 应用才启动的时候,InitServlet 被创建,并由 Servlet 容器调用其 init() 方法:
1.读取类路径下的 switch.properties 文件。
2.获取 switch.properties 的 type 属性值。
3.赋给了 CustomerDAOFactory 的 type 属性值。

将CustomerServlet中的

private CustomerDao customerDAO = new CustomerDAOJdbcImpl();

修改为

private CustomerDAO customerDAO = CustomerDAOFactory.getInstance().getCustomerDAO();

创建 CustomerServlet 时,为 customerDAO 属性赋值是通过 CustomerDAOFactory 的 getCustomerDAO() 方法完成的 。此时的 type 已经在 InitServlet 中被赋值了。

标签:JavaWeb,三十八,dao,MVC,import,xs,com,public,mvcapp
来源: https://blog.csdn.net/baidu_38688346/article/details/88374921