DAO--------->Service------>Controller.
DAO: specifically interact with the database .
MySqlDao . Mysql database interaction
OraclaDao. Oracle database.
Service:
// MysqlDao mdao = new MysqlDao () ; // think there are no disadvantages. hardcode. Poor scalability.
OracleDao odao = new OracleDao (); // how to solve. = 1. The left side of the interface may be used
Spring steps created .
- Add dependency.
- Participation in a configuration file. applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <bean id="mysql" class="com.zhiyou100.wc.dao.MysqlDao"/> 7 <bean id="oracle" class="com.zhiyou100.wc.dao.OracleDao " . 8/> 9 <bean id="s" class="com.zhiyou100.wc.service.Service"> 10 <property name="mdao" ref="mysql"></property> 11 </bean> 12 13 14 </beans>
- test.
1 package com.zhiyou100.wc.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.zhiyou100.wc.service.Service; 7 8 public class Test { 9 10 public static void main(String[] args) { 11 //Service service=new Service(); 12 13 ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml"); 14 15 Service s=(Service) app.getBean("s"); 16 s.show(); 17 18 } 19 20 }