From cd603a7d1dd5371baedb023f55cb48f39672b923 Mon Sep 17 00:00:00 2001 From: zyx <1029606625@qq.com> Date: Mon, 22 May 2023 12:20:36 +0800 Subject: [PATCH] 5/22 12:20 --- .idea/compiler.xml | 4 +- .idea/encodings.xml | 4 ++ .idea/misc.xml | 2 + spring522-annotation/pom.xml | 36 ++++++++++++++++ .../src/main/java/cn/czyx007/bean/User.java | 19 ++++++++ .../src/main/java/cn/czyx007/dao/UserDao.java | 12 ++++++ .../cn/czyx007/dao/impl/MysqlUserDaoImpl.java | 19 ++++++++ .../czyx007/dao/impl/OracleUserDaoImpl.java | 18 ++++++++ .../java/cn/czyx007/service/UserService.java | 11 +++++ .../czyx007/service/impl/UserServiceImpl.java | 27 ++++++++++++ .../src/main/resources/spring.xml | 13 ++++++ .../src/test/java/TestDemo.java | 19 ++++++++ spring522/pom.xml | 35 +++++++++++++++ .../src/main/java/cn/czyx007/bean/Dog.java | 22 ++++++++++ .../src/main/java/cn/czyx007/bean/User.java | 19 ++++++++ .../src/main/java/cn/czyx007/dao/UserDao.java | 12 ++++++ .../cn/czyx007/dao/impl/MysqlUserDaoImpl.java | 17 ++++++++ .../czyx007/dao/impl/OracleUserDaoImpl.java | 16 +++++++ .../java/cn/czyx007/service/UserService.java | 11 +++++ .../service/impl/MysqlUserServiceImpl.java | 24 +++++++++++ .../service/impl/OracleUserServiceImpl.java | 23 ++++++++++ spring522/src/main/resources/spring.xml | 36 ++++++++++++++++ spring522/src/test/java/TestDemo.java | 43 +++++++++++++++++++ 23 files changed, 441 insertions(+), 1 deletion(-) create mode 100644 spring522-annotation/pom.xml create mode 100644 spring522-annotation/src/main/java/cn/czyx007/bean/User.java create mode 100644 spring522-annotation/src/main/java/cn/czyx007/dao/UserDao.java create mode 100644 spring522-annotation/src/main/java/cn/czyx007/dao/impl/MysqlUserDaoImpl.java create mode 100644 spring522-annotation/src/main/java/cn/czyx007/dao/impl/OracleUserDaoImpl.java create mode 100644 spring522-annotation/src/main/java/cn/czyx007/service/UserService.java create mode 100644 spring522-annotation/src/main/java/cn/czyx007/service/impl/UserServiceImpl.java create mode 100644 spring522-annotation/src/main/resources/spring.xml create mode 100644 spring522-annotation/src/test/java/TestDemo.java create mode 100644 spring522/pom.xml create mode 100644 spring522/src/main/java/cn/czyx007/bean/Dog.java create mode 100644 spring522/src/main/java/cn/czyx007/bean/User.java create mode 100644 spring522/src/main/java/cn/czyx007/dao/UserDao.java create mode 100644 spring522/src/main/java/cn/czyx007/dao/impl/MysqlUserDaoImpl.java create mode 100644 spring522/src/main/java/cn/czyx007/dao/impl/OracleUserDaoImpl.java create mode 100644 spring522/src/main/java/cn/czyx007/service/UserService.java create mode 100644 spring522/src/main/java/cn/czyx007/service/impl/MysqlUserServiceImpl.java create mode 100644 spring522/src/main/java/cn/czyx007/service/impl/OracleUserServiceImpl.java create mode 100644 spring522/src/main/resources/spring.xml create mode 100644 spring522/src/test/java/TestDemo.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index abe39af..908d72d 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -8,11 +8,13 @@ + + + - diff --git a/.idea/encodings.xml b/.idea/encodings.xml index d873404..00d4fdc 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -13,5 +13,9 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index b59e23c..4dd72c6 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -14,6 +14,8 @@ diff --git a/spring522-annotation/pom.xml b/spring522-annotation/pom.xml new file mode 100644 index 0000000..224c839 --- /dev/null +++ b/spring522-annotation/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + cn.czyx007 + spring522-annotation + 1.0-SNAPSHOT + + + 11 + 11 + UTF-8 + + + + + org.springframework + spring-context + 5.3.23 + + + junit + junit + 4.13.2 + test + + + org.projectlombok + lombok + 1.18.20 + + + + \ No newline at end of file diff --git a/spring522-annotation/src/main/java/cn/czyx007/bean/User.java b/spring522-annotation/src/main/java/cn/czyx007/bean/User.java new file mode 100644 index 0000000..9ee6343 --- /dev/null +++ b/spring522-annotation/src/main/java/cn/czyx007/bean/User.java @@ -0,0 +1,19 @@ +package cn.czyx007.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:34 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class User { + private String id; + private String userName; + private String address; + private Integer age; +} diff --git a/spring522-annotation/src/main/java/cn/czyx007/dao/UserDao.java b/spring522-annotation/src/main/java/cn/czyx007/dao/UserDao.java new file mode 100644 index 0000000..25db375 --- /dev/null +++ b/spring522-annotation/src/main/java/cn/czyx007/dao/UserDao.java @@ -0,0 +1,12 @@ +package cn.czyx007.dao; + +import cn.czyx007.bean.User; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:35 + */ +public interface UserDao { + //根据id查询用户信息 + User getUser(String id); +} diff --git a/spring522-annotation/src/main/java/cn/czyx007/dao/impl/MysqlUserDaoImpl.java b/spring522-annotation/src/main/java/cn/czyx007/dao/impl/MysqlUserDaoImpl.java new file mode 100644 index 0000000..f96d9f8 --- /dev/null +++ b/spring522-annotation/src/main/java/cn/czyx007/dao/impl/MysqlUserDaoImpl.java @@ -0,0 +1,19 @@ +package cn.czyx007.dao.impl; + +import cn.czyx007.bean.User; +import cn.czyx007.dao.UserDao; +import org.springframework.stereotype.Repository; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:36 + */ +@Repository(value = "mysqlUserDao") +public class MysqlUserDaoImpl implements UserDao { + + @Override + public User getUser(String id) { + System.out.println("通过MySQl查询用户信息成功"); + return null; + } +} diff --git a/spring522-annotation/src/main/java/cn/czyx007/dao/impl/OracleUserDaoImpl.java b/spring522-annotation/src/main/java/cn/czyx007/dao/impl/OracleUserDaoImpl.java new file mode 100644 index 0000000..590f142 --- /dev/null +++ b/spring522-annotation/src/main/java/cn/czyx007/dao/impl/OracleUserDaoImpl.java @@ -0,0 +1,18 @@ +package cn.czyx007.dao.impl; + +import cn.czyx007.bean.User; +import cn.czyx007.dao.UserDao; +import org.springframework.stereotype.Repository; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:37 + */ +@Repository(value = "oracleUserDao") +public class OracleUserDaoImpl implements UserDao { + @Override + public User getUser(String id) { + System.out.println("通过Oracle查询用户信息成功"); + return null; + } +} diff --git a/spring522-annotation/src/main/java/cn/czyx007/service/UserService.java b/spring522-annotation/src/main/java/cn/czyx007/service/UserService.java new file mode 100644 index 0000000..dd2dcb7 --- /dev/null +++ b/spring522-annotation/src/main/java/cn/czyx007/service/UserService.java @@ -0,0 +1,11 @@ +package cn.czyx007.service; + +import cn.czyx007.bean.User; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:40 + */ +public interface UserService { + User findUserById(String id); +} diff --git a/spring522-annotation/src/main/java/cn/czyx007/service/impl/UserServiceImpl.java b/spring522-annotation/src/main/java/cn/czyx007/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..e6faea2 --- /dev/null +++ b/spring522-annotation/src/main/java/cn/czyx007/service/impl/UserServiceImpl.java @@ -0,0 +1,27 @@ +package cn.czyx007.service.impl; + +import cn.czyx007.bean.User; +import cn.czyx007.dao.UserDao; +import cn.czyx007.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:41 + */ +//若使用注解方式,则不需要主动提供sette方法 +@Service("userService") +public class UserServiceImpl implements UserService { + //自动注入 + @Autowired + //指定使用哪一个dao + @Qualifier("mysqlUserDao") + UserDao userDao; + + @Override + public User findUserById(String id) { + return userDao.getUser(id); + } +} diff --git a/spring522-annotation/src/main/resources/spring.xml b/spring522-annotation/src/main/resources/spring.xml new file mode 100644 index 0000000..4a1780b --- /dev/null +++ b/spring522-annotation/src/main/resources/spring.xml @@ -0,0 +1,13 @@ + + + + + + + \ No newline at end of file diff --git a/spring522-annotation/src/test/java/TestDemo.java b/spring522-annotation/src/test/java/TestDemo.java new file mode 100644 index 0000000..6bf499c --- /dev/null +++ b/spring522-annotation/src/test/java/TestDemo.java @@ -0,0 +1,19 @@ +import cn.czyx007.bean.User; +import cn.czyx007.service.impl.UserServiceImpl; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 11:44 + */ +public class TestDemo { + @Test + public void test1(){ + ApplicationContext app = new ClassPathXmlApplicationContext("spring.xml"); + UserServiceImpl userService = app.getBean("userService", UserServiceImpl.class); + User user = userService.findUserById("1"); + System.out.println("user = " + user); + } +} diff --git a/spring522/pom.xml b/spring522/pom.xml new file mode 100644 index 0000000..56d566b --- /dev/null +++ b/spring522/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + + cn.czyx007 + spring522 + 1.0-SNAPSHOT + + + 11 + 11 + UTF-8 + + + + org.springframework + spring-context + 5.3.23 + + + junit + junit + 4.13.2 + test + + + org.projectlombok + lombok + 1.18.20 + + + + \ No newline at end of file diff --git a/spring522/src/main/java/cn/czyx007/bean/Dog.java b/spring522/src/main/java/cn/czyx007/bean/Dog.java new file mode 100644 index 0000000..8202373 --- /dev/null +++ b/spring522/src/main/java/cn/czyx007/bean/Dog.java @@ -0,0 +1,22 @@ +package cn.czyx007.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/19 - 15:59 + */ +@Data +//@NoArgsConstructor +@AllArgsConstructor +public class Dog { + public Dog() { + System.out.println("狗出生了"); + } + + private String name; + public void eat(){ + System.out.println(name + "的狗吃骨头..."); + } +} diff --git a/spring522/src/main/java/cn/czyx007/bean/User.java b/spring522/src/main/java/cn/czyx007/bean/User.java new file mode 100644 index 0000000..9ee6343 --- /dev/null +++ b/spring522/src/main/java/cn/czyx007/bean/User.java @@ -0,0 +1,19 @@ +package cn.czyx007.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:34 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class User { + private String id; + private String userName; + private String address; + private Integer age; +} diff --git a/spring522/src/main/java/cn/czyx007/dao/UserDao.java b/spring522/src/main/java/cn/czyx007/dao/UserDao.java new file mode 100644 index 0000000..25db375 --- /dev/null +++ b/spring522/src/main/java/cn/czyx007/dao/UserDao.java @@ -0,0 +1,12 @@ +package cn.czyx007.dao; + +import cn.czyx007.bean.User; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:35 + */ +public interface UserDao { + //根据id查询用户信息 + User getUser(String id); +} diff --git a/spring522/src/main/java/cn/czyx007/dao/impl/MysqlUserDaoImpl.java b/spring522/src/main/java/cn/czyx007/dao/impl/MysqlUserDaoImpl.java new file mode 100644 index 0000000..6e21ba7 --- /dev/null +++ b/spring522/src/main/java/cn/czyx007/dao/impl/MysqlUserDaoImpl.java @@ -0,0 +1,17 @@ +package cn.czyx007.dao.impl; + +import cn.czyx007.bean.User; +import cn.czyx007.dao.UserDao; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:36 + */ +public class MysqlUserDaoImpl implements UserDao { + + @Override + public User getUser(String id) { + System.out.println("通过MySQl查询用户信息成功"); + return null; + } +} diff --git a/spring522/src/main/java/cn/czyx007/dao/impl/OracleUserDaoImpl.java b/spring522/src/main/java/cn/czyx007/dao/impl/OracleUserDaoImpl.java new file mode 100644 index 0000000..dafad04 --- /dev/null +++ b/spring522/src/main/java/cn/czyx007/dao/impl/OracleUserDaoImpl.java @@ -0,0 +1,16 @@ +package cn.czyx007.dao.impl; + +import cn.czyx007.bean.User; +import cn.czyx007.dao.UserDao; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:37 + */ +public class OracleUserDaoImpl implements UserDao { + @Override + public User getUser(String id) { + System.out.println("通过Oracle查询用户信息成功"); + return null; + } +} diff --git a/spring522/src/main/java/cn/czyx007/service/UserService.java b/spring522/src/main/java/cn/czyx007/service/UserService.java new file mode 100644 index 0000000..dd2dcb7 --- /dev/null +++ b/spring522/src/main/java/cn/czyx007/service/UserService.java @@ -0,0 +1,11 @@ +package cn.czyx007.service; + +import cn.czyx007.bean.User; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:40 + */ +public interface UserService { + User findUserById(String id); +} diff --git a/spring522/src/main/java/cn/czyx007/service/impl/MysqlUserServiceImpl.java b/spring522/src/main/java/cn/czyx007/service/impl/MysqlUserServiceImpl.java new file mode 100644 index 0000000..c8cce60 --- /dev/null +++ b/spring522/src/main/java/cn/czyx007/service/impl/MysqlUserServiceImpl.java @@ -0,0 +1,24 @@ +package cn.czyx007.service.impl; + +import cn.czyx007.bean.User; +import cn.czyx007.dao.UserDao; +import cn.czyx007.service.UserService; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:41 + */ +public class MysqlUserServiceImpl implements UserService { + //高耦合,换用spring解耦合 +// UserDao userDao = new MysqlUserDaoImpl(); + UserDao userDao; + + public void setUserDao(UserDao userDao) { + this.userDao = userDao; + } + + @Override + public User findUserById(String id) { + return userDao.getUser(id); + } +} diff --git a/spring522/src/main/java/cn/czyx007/service/impl/OracleUserServiceImpl.java b/spring522/src/main/java/cn/czyx007/service/impl/OracleUserServiceImpl.java new file mode 100644 index 0000000..759f82b --- /dev/null +++ b/spring522/src/main/java/cn/czyx007/service/impl/OracleUserServiceImpl.java @@ -0,0 +1,23 @@ +package cn.czyx007.service.impl; + +import cn.czyx007.bean.User; +import cn.czyx007.dao.UserDao; +import cn.czyx007.service.UserService; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:41 + */ +public class OracleUserServiceImpl implements UserService { +// UserDao userDao = new OracleUserDaoImpl(); + UserDao userDao; + + public void setUserDao(UserDao userDao) { + this.userDao = userDao; + } + + @Override + public User findUserById(String id) { + return userDao.getUser(id); + } +} diff --git a/spring522/src/main/resources/spring.xml b/spring522/src/main/resources/spring.xml new file mode 100644 index 0000000..68f6765 --- /dev/null +++ b/spring522/src/main/resources/spring.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring522/src/test/java/TestDemo.java b/spring522/src/test/java/TestDemo.java new file mode 100644 index 0000000..03c4c01 --- /dev/null +++ b/spring522/src/test/java/TestDemo.java @@ -0,0 +1,43 @@ +import cn.czyx007.bean.Dog; +import cn.czyx007.bean.User; +import cn.czyx007.service.impl.OracleUserServiceImpl; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author : 张宇轩 + * @createTime : 2023/5/22 - 9:31 + */ +public class TestDemo { + @Test + public void test1(){ + ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("spring.xml"); + Dog dog = app.getBean("dog", Dog.class); + dog.eat(); + Dog dog1 = app.getBean("dog", Dog.class); + System.out.println("是否是同一个对象 " + (dog == dog1)); + } + + //要注意交给spring管理后不能再自己new + @Test + public void test2(){ + OracleUserServiceImpl userService = new OracleUserServiceImpl(); + User user = userService.findUserById("1"); + System.out.println("user = " + user); + } + + @Test + public void test3(){ + ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("spring.xml"); + OracleUserServiceImpl userService = app.getBean("oracleUserService", OracleUserServiceImpl.class); + User user = userService.findUserById("1"); + System.out.println("user = " + user); + } + + @Test + public void test4(){ + ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("spring.xml"); + User user = app.getBean("user", User.class); + System.out.println("user = " + user); + } +}