依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
AOP
IOC理论推导 控制反转,主动权交给用户
1.dao层是用来实现业务,和数据库打交道
dao层 接口
package com.tengxian.dao; public interface UserDao { void getUser(); }
dao接口的实现1,默认获取
package com.tengxian.dao; public class UserDaoImpl implements UserDao{ @Override public void getUser() { System.out.println("默认获取用户是数据"); } }
dao接口的实现类2,mysql获取
package com.tengxian.dao; public class UserDaoMysqlImpl implements UserDao{ @Override public void getUser() { System.out.println("Mysql 获取数据!"); } }
service层,
服务层接口
package com.tengxian.service; public interface UserService { void getUser(); }
服务处实现类
package com.tengxian.service; import com.tengxian.dao.UserDao; import com.tengxian.dao.UserDaoImpl; public class UserServiceImpl implements UserService{ //业务层调用Dao层 private UserDao userDao; //利用set进行动态实现值的注入 public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void getUser() { userDao.getUser(); } }
用户层:用户实际要用的是业务层,dao层他们不用接触
import com.tengxian.dao.UserDaoImpl; import com.tengxian.dao.UserDaoMysqlImpl; import com.tengxian.service.UserServiceImpl; public class MyTest { public static void main(String[] args) { //用户实际要用的是业务层,dao层他们不用接触 UserServiceImpl userService = new UserServiceImpl(); userService.setUserDao(new UserDaoImpl()); userService.getUser(); } }