DAO--------->Service------>Controller.
DAO: 专门与数据库交互.
MySqlDao。 Mysql数据库交互
OraclaDao. Oracle数据库。
Service:
//MysqlDao mdao=new MysqlDao(); //思考 有没有缺点。硬编码。扩展性不好。
OracleDao odao=new OracleDao(); //如何解决。1.=左边的 可以使用接口
Spring创建的步骤.
- 加入依赖。
- 加入配置文件。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>
- 测试。
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 }