parent
e0d067c8c2
commit
633a6cbba8
@ -0,0 +1,38 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
@ -0,0 +1,60 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>spring_aop_01</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context</artifactId>
|
||||||
|
<version>6.0.11</version>
|
||||||
|
</dependency>
|
||||||
|
<!--spring切面包-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-aspects</artifactId>
|
||||||
|
<version>6.0.11</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!--aop联盟包-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>aopalliance</groupId>
|
||||||
|
<artifactId>aopalliance</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
|
<!--Apache Commons日志包-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-logging</groupId>
|
||||||
|
<artifactId>commons-logging</artifactId>
|
||||||
|
<version>1.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!--lombok -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.12</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,87 @@
|
|||||||
|
package com.msb.aspect;
|
||||||
|
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.*;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
@Order(2)
|
||||||
|
public class DaoAspect {
|
||||||
|
|
||||||
|
//定义公共切点, 切点表达式直接指向接口
|
||||||
|
@Pointcut("execution(* com.msb.dao.*.add*(..))")
|
||||||
|
public void addPointCut(){}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前置通知: 切点方法执行之前先执行的功能
|
||||||
|
* 参数列表可以用JoinPoint接收切点对象
|
||||||
|
* 可以获取方法执行的参数
|
||||||
|
* */
|
||||||
|
@Before("addPointCut()")
|
||||||
|
public void methodBefore(JoinPoint joinPoint){
|
||||||
|
// joinPoint 目标增强的方法就是 addUser,可以获取参数等
|
||||||
|
// Object[] args = joinPoint.getArgs();
|
||||||
|
// System.out.println("args: "+ Arrays.toString(args));
|
||||||
|
|
||||||
|
// 前置通知
|
||||||
|
System.out.println("methodBefore invoked.....");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 后置通知:方法执行之后要增强的功能
|
||||||
|
* 无论切点方法是否出现异常都会执行的方法
|
||||||
|
* 参数列表可以用JoinPoint接收切点对象
|
||||||
|
* */
|
||||||
|
@After("addPointCut()")
|
||||||
|
public void methodAfter(JoinPoint joinPoint){
|
||||||
|
System.out.println("methodAfter invoked...");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回通知:切点方法正常运行结束后增强的功能
|
||||||
|
* 如果方法运行过程中出现异常,则该功能不运行
|
||||||
|
* 参数列表可以用 JoinPoint joinPoint接收切点对象
|
||||||
|
* 可以用Object res接收方法返回值,需要用returning指定返回值名称
|
||||||
|
* */
|
||||||
|
@AfterReturning(value = "addPointCut()", returning = "res")
|
||||||
|
public void methodAfterReturning(JoinPoint joinPoint, Object res){
|
||||||
|
System.out.println("methodAfterReturning invoked...");
|
||||||
|
// System.out.println(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异常通知:切点方法出现异常时运行的增强功能
|
||||||
|
* 如果方法运行没有出现异常,则该功能不运行
|
||||||
|
* 参数列表可以用Exception ex接收异常对象 需要通过throwing指定异常名称
|
||||||
|
* */
|
||||||
|
@AfterThrowing(value = "addPointCut()", throwing = "exception")
|
||||||
|
public void methodAfterThrowing(Exception exception){
|
||||||
|
System.out.println("methodAfterThrowing invoked....");
|
||||||
|
// System.out.println(exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 环绕通知:在切点方法之前和之后都进行功能的增强
|
||||||
|
* 需要在通知中定义方法执行的位置,并在执行位置之前和之后自定义增强的功能
|
||||||
|
* 方法列表可以通过ProceedingJoinPoint获取执行的切点
|
||||||
|
* 通过proceedingJoinPoint.proceed()方法控制切点方法的执行位置
|
||||||
|
* proceedingJoinPoint.proceed()方法会将切点方法的返回值获取到,并交给我们,可以做后续处理
|
||||||
|
* 我们在环绕通知的最后需要将切点方法的返回值继续向上返回,否则切点方法在执行时接收不到返回值
|
||||||
|
* */
|
||||||
|
@Around(value = "addPointCut()")
|
||||||
|
public Object methodAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
||||||
|
System.out.println("methodAroundA invoked...");
|
||||||
|
Object res = proceedingJoinPoint.proceed();
|
||||||
|
System.out.println("methodAroundB invoked...");
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,87 @@
|
|||||||
|
package com.msb.aspect;
|
||||||
|
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.*;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
@Order(1)
|
||||||
|
public class DaoAspect2 {
|
||||||
|
|
||||||
|
//定义公共切点, 切点表达式直接指向接口
|
||||||
|
@Pointcut("execution(* com.msb.dao.*.add*(..))")
|
||||||
|
public void addPointCut(){}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 前置通知: 切点方法执行之前先执行的功能
|
||||||
|
* 参数列表可以用JoinPoint接收切点对象
|
||||||
|
* 可以获取方法执行的参数
|
||||||
|
* */
|
||||||
|
@Before("addPointCut()")
|
||||||
|
public void methodBefore(JoinPoint joinPoint){
|
||||||
|
// // joinPoint 目标增强的方法就是 addUser,可以获取参数等
|
||||||
|
// Object[] args = joinPoint.getArgs();
|
||||||
|
// System.out.println("args: "+ Arrays.toString(args));
|
||||||
|
|
||||||
|
// 前置通知
|
||||||
|
System.out.println("methodBefore2 invoked.....");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 后置通知:方法执行之后要增强的功能
|
||||||
|
* 无论切点方法是否出现异常都会执行的方法
|
||||||
|
* 参数列表可以用JoinPoint接收切点对象
|
||||||
|
* */
|
||||||
|
@After("addPointCut()")
|
||||||
|
public void methodAfter(JoinPoint joinPoint){
|
||||||
|
System.out.println("methodAfter2 invoked...");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回通知:切点方法正常运行结束后增强的功能
|
||||||
|
* 如果方法运行过程中出现异常,则该功能不运行
|
||||||
|
* 参数列表可以用 JoinPoint joinPoint接收切点对象
|
||||||
|
* 可以用Object res接收方法返回值,需要用returning指定返回值名称
|
||||||
|
* */
|
||||||
|
@AfterReturning(value = "addPointCut()", returning = "res")
|
||||||
|
public void methodAfterReturning(JoinPoint joinPoint, Object res){
|
||||||
|
System.out.println("methodAfterReturning2 invoked...");
|
||||||
|
// System.out.println(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异常通知:切点方法出现异常时运行的增强功能
|
||||||
|
* 如果方法运行没有出现异常,则该功能不运行
|
||||||
|
* 参数列表可以用Exception ex接收异常对象 需要通过throwing指定异常名称
|
||||||
|
* */
|
||||||
|
@AfterThrowing(value = "addPointCut()", throwing = "exception")
|
||||||
|
public void methodAfterThrowing(Exception exception){
|
||||||
|
System.out.println("methodAfterThrowing2 invoked....");
|
||||||
|
// System.out.println(exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 环绕通知:在切点方法之前和之后都进行功能的增强
|
||||||
|
* 需要在通知中定义方法执行的位置,并在执行位置之前和之后自定义增强的功能
|
||||||
|
* 方法列表可以通过ProceedingJoinPoint获取执行的切点
|
||||||
|
* 通过proceedingJoinPoint.proceed()方法控制切点方法的执行位置
|
||||||
|
* proceedingJoinPoint.proceed()方法会将切点方法的返回值获取到,并交给我们,可以做后续处理
|
||||||
|
* 我们在环绕通知的最后需要将切点方法的返回值继续向上返回,否则切点方法在执行时接收不到返回值
|
||||||
|
* */
|
||||||
|
@Around(value = "addPointCut()")
|
||||||
|
public Object methodAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
||||||
|
System.out.println("methodAroundA2 invoked...");
|
||||||
|
Object res = proceedingJoinPoint.proceed();
|
||||||
|
System.out.println("methodAroundB2 invoked...");
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.msb.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||||
|
|
||||||
|
@ComponentScan("com.msb")
|
||||||
|
@Configuration
|
||||||
|
@EnableAspectJAutoProxy(proxyTargetClass = true)
|
||||||
|
public class SpringConfig {
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.msb.dao;
|
||||||
|
|
||||||
|
public interface EmpDao {
|
||||||
|
Integer addEmp(int empno, String name, String Job);
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.msb.dao;
|
||||||
|
|
||||||
|
public interface UserDao {
|
||||||
|
Integer addUser(Integer userId, String username);
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.msb.dao.impl;
|
||||||
|
|
||||||
|
import com.msb.dao.EmpDao;
|
||||||
|
import com.msb.dao.UserDao;
|
||||||
|
import com.msb.service.EmpService;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class EmpDaoImpl implements EmpDao {
|
||||||
|
@Override
|
||||||
|
public Integer addEmp(int empno, String name, String Job) {
|
||||||
|
System.out.println("EmpDaoImpl addEmp() ....");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.msb.dao.impl;
|
||||||
|
|
||||||
|
import com.msb.dao.UserDao;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class UserDaoImpl implements UserDao {
|
||||||
|
@Override
|
||||||
|
public Integer addUser(Integer userId, String username) {
|
||||||
|
System.out.println("UserDaoImpl add ....");
|
||||||
|
// int i = 1/0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package com.msb.service;
|
||||||
|
|
||||||
|
public interface EmpService {
|
||||||
|
|
||||||
|
int addEmp(int empno, String name, String job);
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.msb.service;
|
||||||
|
|
||||||
|
public interface UserService {
|
||||||
|
Integer addUser(Integer userId, String username);
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.msb.service.impl;
|
||||||
|
|
||||||
|
import com.msb.dao.EmpDao;
|
||||||
|
import com.msb.service.EmpService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class EmpServiceImpl implements EmpService {
|
||||||
|
|
||||||
|
// @Autowired
|
||||||
|
private final EmpDao empDao;
|
||||||
|
|
||||||
|
public EmpServiceImpl(EmpDao empDao){
|
||||||
|
this.empDao = empDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int addEmp(int empno, String name, String job) {
|
||||||
|
System.out.println("empServiceImpl add()...");
|
||||||
|
|
||||||
|
return empDao.addEmp(empno, name, job);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.msb.service.impl;
|
||||||
|
|
||||||
|
import com.msb.dao.UserDao;
|
||||||
|
import com.msb.service.UserService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserServiceImpl implements UserService {
|
||||||
|
// @Autowired
|
||||||
|
private final UserDao userDao;
|
||||||
|
|
||||||
|
public UserServiceImpl(UserDao userDao){
|
||||||
|
this.userDao = userDao;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Integer addUser(Integer userId, String username) {
|
||||||
|
System.out.println("UserServiceImpl add...");
|
||||||
|
|
||||||
|
return this.userDao.addUser(userId, username);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/context
|
||||||
|
http://www.springframework.org/schema/context/spring-context.xsd
|
||||||
|
http://www.springframework.org/schema/aop
|
||||||
|
http://www.springframework.org/schema/aop/spring-aop.xsd
|
||||||
|
">
|
||||||
|
<!-- 开启包扫描 -->
|
||||||
|
<!-- <context:component-scan base-package="com.msb"/>-->
|
||||||
|
|
||||||
|
<!--aop autoProxy 自动生成代理对象 -->
|
||||||
|
<!-- <aop:aspectj-autoproxy/>-->
|
||||||
|
|
||||||
|
|
||||||
|
<!-- 手动配置切面-->
|
||||||
|
<!-- 目标对象-->
|
||||||
|
<bean id="userDao" class="com.msb.dao.impl.UserDaoImpl"/>
|
||||||
|
<!-- 切面对象-->
|
||||||
|
<bean id="daoAspect" class="com.msb.aspect.DaoAspect"/>
|
||||||
|
<!-- 配置AOP增强功能-->
|
||||||
|
<aop:config>
|
||||||
|
<!-- 配置切点-->
|
||||||
|
<aop:pointcut id="addPointCut" expression="execution(* com.msb.dao.*.add*(..))"/>
|
||||||
|
<aop:aspect ref="daoAspect">
|
||||||
|
<!-- 增强作用的方法-->
|
||||||
|
<aop:before method="methodBefore" pointcut-ref="addPointCut"/>
|
||||||
|
<aop:after method="methodAfter" pointcut-ref="addPointCut"/>
|
||||||
|
<aop:after-returning method="methodAfterReturning" pointcut-ref="addPointCut" returning="res"/>
|
||||||
|
<aop:after-throwing method="methodAfterThrowing" pointcut-ref="addPointCut" throwing="exception"/>
|
||||||
|
<aop:around method="methodAround" pointcut-ref="addPointCut"/>
|
||||||
|
|
||||||
|
</aop:aspect>
|
||||||
|
</aop:config>
|
||||||
|
</beans>
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.msb;
|
||||||
|
|
||||||
|
import com.msb.config.SpringConfig;
|
||||||
|
import com.msb.dao.UserDao;
|
||||||
|
import com.msb.service.UserService;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
public class TestAspect {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBean(){
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||||
|
|
||||||
|
// userDao 生成一个对象再自动生产一个代理对象,然后再返回代理对象
|
||||||
|
UserDao userDao = applicationContext.getBean(UserDao.class);
|
||||||
|
Integer rows = userDao.addUser(1, "张三");
|
||||||
|
System.out.println(rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBean2(){
|
||||||
|
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
|
||||||
|
UserDao userDao = applicationContext.getBean(UserDao.class);
|
||||||
|
Integer rows = userDao.addUser(1, "张三");
|
||||||
|
// System.out.println(userDao.getClass().getSimpleName());
|
||||||
|
System.out.println(rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package com.msb;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.cglib.proxy.Enhancer;
|
||||||
|
import org.springframework.cglib.proxy.MethodInterceptor;
|
||||||
|
import org.springframework.cglib.proxy.MethodProxy;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public class TestCglib {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCglib(){
|
||||||
|
Person2 person2 = new Person2();
|
||||||
|
// 获取一个Person的代理对象
|
||||||
|
// 1 获得一个Enhancer对象
|
||||||
|
Enhancer enhancer=new Enhancer();
|
||||||
|
// 2 设置父类字节码
|
||||||
|
enhancer.setSuperclass(person2.getClass());
|
||||||
|
// 3 获取MethodIntercepter对象 用于定义增强规则
|
||||||
|
MethodInterceptor methodInterceptor=new MethodInterceptor() {
|
||||||
|
@Override
|
||||||
|
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
|
||||||
|
/*Object o, 生成之后的代理对象 personProxy
|
||||||
|
Method method, 父类中原本要执行的方法 Person>>> eat()
|
||||||
|
Object[] objects, 方法在调用时传入的实参数组
|
||||||
|
MethodProxy methodProxy 子类中重写父类的方法 personProxy >>> eat()
|
||||||
|
*/
|
||||||
|
Object res =null;
|
||||||
|
if(method.getName().equals("eat")){
|
||||||
|
// 如果是eat方法 则增强并运行
|
||||||
|
System.out.println("饭前洗手");
|
||||||
|
res=methodProxy.invokeSuper(o,objects);
|
||||||
|
System.out.println("饭后刷碗");
|
||||||
|
}else{
|
||||||
|
// 如果是其他方法 不增强运行
|
||||||
|
res=methodProxy.invokeSuper(o,objects); // 子类对象方法在执行,默认会调用父类对应被重写的方法
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// 4 设置methodInterceptor
|
||||||
|
enhancer.setCallback(methodInterceptor);
|
||||||
|
// 5 获得代理对象
|
||||||
|
Person2 personProxy = (Person2) enhancer.create();
|
||||||
|
// 6 使用代理对象完成功能
|
||||||
|
personProxy.eat("包子");
|
||||||
|
|
||||||
|
|
||||||
|
// person2.eat("包子");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Person2{
|
||||||
|
|
||||||
|
public Person2(){
|
||||||
|
}
|
||||||
|
|
||||||
|
public void eat(String foodName) {
|
||||||
|
System.out.println("正在吃"+foodName);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.msb;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationHandler;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
|
|
||||||
|
public class TestProxy {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Dinner dinner = new Person1("张三");
|
||||||
|
|
||||||
|
// 通过Proxy 动态代理获取一个代理对象,在代理对象里面,对某个方法进行增强
|
||||||
|
// ClassLoader loader, 被代理对象的类加载器
|
||||||
|
ClassLoader classLoader = dinner.getClass().getClassLoader();
|
||||||
|
|
||||||
|
// Class<?>[] interfaces, 被代理对象所实现的所有接口
|
||||||
|
Class[] interfaces= dinner.getClass().getInterfaces();
|
||||||
|
|
||||||
|
// InvocationHandler h 执行处理器对象,专门定义增强的规则
|
||||||
|
InvocationHandler handler = new InvocationHandler(){
|
||||||
|
// invoke 当我们让代理对象调用任何方法时,都会触发invoke方法的执行
|
||||||
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||||
|
// Object proxy, 代理对象
|
||||||
|
// Method method,被代理的方法
|
||||||
|
// Object[] args,被代理方法运行时的实参
|
||||||
|
Object res=null;
|
||||||
|
if(method.getName().equals("eat")){
|
||||||
|
System.out.println("饭前洗手");
|
||||||
|
// 让原有的eat的方法去运行
|
||||||
|
res =method.invoke(dinner, args);
|
||||||
|
System.out.println("饭后刷碗");
|
||||||
|
}else{
|
||||||
|
// 如果是其他方法,那么正常执行就可以了
|
||||||
|
res =method.invoke(dinner, args);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Dinner dinnerProxy = (Dinner) Proxy.newProxyInstance(classLoader, interfaces, handler);
|
||||||
|
dinnerProxy.eat("包子");
|
||||||
|
dinnerProxy.drink();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
interface Dinner{
|
||||||
|
void eat(String foodName);
|
||||||
|
void drink();
|
||||||
|
}
|
||||||
|
class Person1 implements Dinner{
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Person1(String name){
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void eat(String foodName) {
|
||||||
|
System.out.println(name+"正在吃"+foodName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void drink() {
|
||||||
|
System.out.println(name+"正在喝茶");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package com.msb;
|
||||||
|
|
||||||
|
public class TestStaticProxy {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Person person = new Person("张三");
|
||||||
|
Court court = new Lawyer(person);
|
||||||
|
court.doCourt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Court{
|
||||||
|
void doCourt();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 静态的代理类
|
||||||
|
class Lawyer implements Court {
|
||||||
|
private Person person;
|
||||||
|
|
||||||
|
public Lawyer(Person person){
|
||||||
|
this.person = person;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doCourt() {
|
||||||
|
System.out.println("律师证明张三没有再案发现场!");
|
||||||
|
person.doCourt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 面向的类,不需要修改的被代理类
|
||||||
|
class Person implements Court{
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Person(String name){
|
||||||
|
this.name= name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doCourt(){
|
||||||
|
System.out.println(name+"说:我没有杀人!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
@ -0,0 +1,45 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>spring_ioc_01</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context</artifactId>
|
||||||
|
<version>6.0.11</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid</artifactId>
|
||||||
|
<version>1.2.18</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>8.0.33</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>17</java.version>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.msb.bean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: Ma HaiYang
|
||||||
|
* @Description: MircoMessage:Mark_7001
|
||||||
|
*/
|
||||||
|
public class Book {
|
||||||
|
private String bname;
|
||||||
|
private String author;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Book{" +
|
||||||
|
"bname='" + bname + '\'' +
|
||||||
|
", author='" + author + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book(String bname, String author) {
|
||||||
|
this.bname = bname;
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBname() {
|
||||||
|
return bname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBname(String bname) {
|
||||||
|
this.bname = bname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAuthor(String author) {
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.msb.bean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: Ma HaiYang
|
||||||
|
* @Description: MircoMessage:Mark_7001
|
||||||
|
*/
|
||||||
|
public class Cat {
|
||||||
|
private String name;
|
||||||
|
private Mouse mouse1;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Cat{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", mouse1=" + mouse1 +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public Cat() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Cat(String name, Mouse mouse1) {
|
||||||
|
this.name = name;
|
||||||
|
this.mouse1 = mouse1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mouse getMouse1() {
|
||||||
|
return mouse1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMouse1(Mouse mouse1) {
|
||||||
|
this.mouse1 = mouse1;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.msb.bean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: Ma HaiYang
|
||||||
|
* @Description: MircoMessage:Mark_7001
|
||||||
|
*/
|
||||||
|
public class Dept {
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.msb.bean;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: Ma HaiYang
|
||||||
|
* @Description: MircoMessage:Mark_7001
|
||||||
|
*/
|
||||||
|
public class Emp {
|
||||||
|
private Dept dept;
|
||||||
|
|
||||||
|
public void setDept(Dept dept) {
|
||||||
|
this.dept = dept;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Emp{" +
|
||||||
|
"dept=" + dept +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.msb.bean;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
|
||||||
|
public class Mouse {
|
||||||
|
private String name;
|
||||||
|
private Date birthdate;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Mouse{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", birthdate=" + birthdate +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mouse() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mouse(String name, Date birthdate) {
|
||||||
|
this.name = name;
|
||||||
|
this.birthdate = birthdate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getBirthdate() {
|
||||||
|
return birthdate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBirthdate(Date birthdate) {
|
||||||
|
this.birthdate = birthdate;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,81 @@
|
|||||||
|
package com.msb.bean;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: Ma HaiYang
|
||||||
|
* @Description: MircoMessage:Mark_7001
|
||||||
|
*/
|
||||||
|
public class Student {
|
||||||
|
private String[] books;
|
||||||
|
private Set<String> bookSet;
|
||||||
|
private List<String> bookList;
|
||||||
|
private Map<String,String> bookMap;
|
||||||
|
private List<Book> bookList2;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Student{" +
|
||||||
|
"books=" + Arrays.toString(books) +
|
||||||
|
", bookSet=" + bookSet +
|
||||||
|
", bookList=" + bookList +
|
||||||
|
", bookMap=" + bookMap +
|
||||||
|
", bookList2=" + bookList2 +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getBooks() {
|
||||||
|
return books;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBooks(String[] books) {
|
||||||
|
this.books = books;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> getBookSet() {
|
||||||
|
return bookSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBookSet(Set<String> bookSet) {
|
||||||
|
this.bookSet = bookSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getBookList() {
|
||||||
|
return bookList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBookList(List<String> bookList) {
|
||||||
|
this.bookList = bookList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getBookMap() {
|
||||||
|
return bookMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBookMap(Map<String, String> bookMap) {
|
||||||
|
this.bookMap = bookMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Book> getBookList2() {
|
||||||
|
return bookList2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBookList2(List<Book> bookList2) {
|
||||||
|
this.bookList2 = bookList2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student(String[] books, Set<String> bookSet, List<String> bookList, Map<String, String> bookMap, List<Book> bookList2) {
|
||||||
|
|
||||||
|
this.books = books;
|
||||||
|
this.bookSet = bookSet;
|
||||||
|
this.bookList = bookList;
|
||||||
|
this.bookMap = bookMap;
|
||||||
|
this.bookList2 = bookList2;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:p="http://www.springframework.org/schema/p"
|
||||||
|
xmlns:c="http://www.springframework.org/schema/c"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||||
|
|
||||||
|
<bean id="user1" class="com.msb.bean.User"/>
|
||||||
|
|
||||||
|
<!-- new User() setUserid("1")... -->
|
||||||
|
<bean id="user2" class="com.msb.bean.User">
|
||||||
|
<property name="userid" value="2"/>
|
||||||
|
<property name="username" value="用户2"/>
|
||||||
|
<property name="password" value="123456"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
User user3=new User(1,"","")
|
||||||
|
-->
|
||||||
|
<bean id="user3" class="com.msb.bean.User">
|
||||||
|
<constructor-arg name="userid" value="3"/>
|
||||||
|
<constructor-arg name="username" value="小明"/>
|
||||||
|
<constructor-arg name="password" value="567890"/>
|
||||||
|
</bean>
|
||||||
|
<bean id="user4" class="com.msb.bean.User">
|
||||||
|
<constructor-arg index="0" value="4"/>
|
||||||
|
<constructor-arg index="1" value="小黑"/>
|
||||||
|
<constructor-arg index="2" value="654321"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
|
||||||
|
<!--
|
||||||
|
通过p名称空间和c名称空间实现DI
|
||||||
|
-->
|
||||||
|
<bean id="user5" class="com.msb.bean.User" p:userid="5" p:username="小东" p:password="111111" />
|
||||||
|
<bean id="user6" class="com.msb.bean.User" c:userid="6" c:username="小西" c:password="222222" />
|
||||||
|
</beans>
|
@ -0,0 +1,49 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:p="http://www.springframework.org/schema/p"
|
||||||
|
xmlns:c="http://www.springframework.org/schema/c"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||||
|
|
||||||
|
<!--告诉容器准备一个Date对象-->
|
||||||
|
<bean id="date1" class="java.util.Date"/>
|
||||||
|
|
||||||
|
<bean id="mouse1" class="com.msb.bean.Mouse">
|
||||||
|
<property name="name" value="Jerry"/>
|
||||||
|
<!--bean引用引用外部bean-->
|
||||||
|
<property name="birthdate" ref="date1"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean id="cat1" class="com.msb.bean.Cat">
|
||||||
|
<property name="name" value="Tom"/>
|
||||||
|
<!--引用内部bean-->
|
||||||
|
<property name="mouse1" >
|
||||||
|
<bean class="com.msb.bean.Mouse">
|
||||||
|
<property name="name" value="Jerry2"/>
|
||||||
|
<property name="birthdate">
|
||||||
|
<bean class="java.util.Date"/>
|
||||||
|
</property>
|
||||||
|
</bean>
|
||||||
|
</property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean id="mouse2" class="com.msb.bean.Mouse"/>
|
||||||
|
<bean id="cat2" class="com.msb.bean.Cat">
|
||||||
|
<property name="name" value="Tom2"/>
|
||||||
|
<property name="mouse1" ref="mouse2"/>
|
||||||
|
<!--用反射调用get*** 方法,获得对象之后,再赋值-->
|
||||||
|
<property name="mouse1.name" value="Jerry3"/>
|
||||||
|
<property name="mouse1.birthdate">
|
||||||
|
<bean class="java.util.Date"/>
|
||||||
|
</property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,49 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:p="http://www.springframework.org/schema/p"
|
||||||
|
xmlns:c="http://www.springframework.org/schema/c"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||||
|
|
||||||
|
<!--告诉容器准备一个Date对象-->
|
||||||
|
<bean id="date1" class="java.util.Date"/>
|
||||||
|
|
||||||
|
<bean id="mouse1" class="com.msb.bean.Mouse">
|
||||||
|
<property name="name" value="Jerry"/>
|
||||||
|
<!--bean引用引用外部bean-->
|
||||||
|
<property name="birthdate" ref="date1"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean id="cat1" class="com.msb.bean.Cat">
|
||||||
|
<property name="name" value="Tom"/>
|
||||||
|
<!--引用内部bean-->
|
||||||
|
<property name="mouse1" >
|
||||||
|
<bean class="com.msb.bean.Mouse">
|
||||||
|
<property name="name" value="Jerry2"/>
|
||||||
|
<property name="birthdate">
|
||||||
|
<bean class="java.util.Date"/>
|
||||||
|
</property>
|
||||||
|
</bean>
|
||||||
|
</property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean id="mouse2" class="com.msb.bean.Mouse"/>
|
||||||
|
<bean id="cat2" class="com.msb.bean.Cat">
|
||||||
|
<property name="name" value="Tom2"/>
|
||||||
|
<property name="mouse1" ref="mouse2"/>
|
||||||
|
<!--用反射调用get*** 方法,获得对象之后,再赋值-->
|
||||||
|
<property name="mouse1.name" value="Jerry3"/>
|
||||||
|
<property name="mouse1.birthdate">
|
||||||
|
<bean class="java.util.Date"/>
|
||||||
|
</property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,63 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:p="http://www.springframework.org/schema/p"
|
||||||
|
xmlns:c="http://www.springframework.org/schema/c"
|
||||||
|
xmlns:util="http://www.springframework.org/schema/util"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/util
|
||||||
|
http://www.springframework.org/schema/util/spring-util.xsd
|
||||||
|
">
|
||||||
|
<!-- 对象集合 -->
|
||||||
|
<util:list id="bookList">
|
||||||
|
<bean id="b1" class="com.msb.bean.Book" p:bname="JAVA" p:author="马士兵"/>
|
||||||
|
<bean id="b2" class="com.msb.bean.Book" p:bname="Go" p:author="马士兵"/>
|
||||||
|
<bean id="b3" class="com.msb.bean.Book" p:bname="JVM" p:author="马士兵"/>
|
||||||
|
</util:list>
|
||||||
|
<bean id="student1" class="com.msb.bean.Student">
|
||||||
|
<!--数组属性注入-->
|
||||||
|
<property name="books">
|
||||||
|
<array>
|
||||||
|
<value>JAVA</value>
|
||||||
|
<value>MySQL</value>
|
||||||
|
<value>Spring</value>
|
||||||
|
</array>
|
||||||
|
</property>
|
||||||
|
|
||||||
|
<!--set集合注入-->
|
||||||
|
<property name="bookSet">
|
||||||
|
<set>
|
||||||
|
<value>JAVA</value>
|
||||||
|
<value>MySQL</value>
|
||||||
|
<value>Spring</value>
|
||||||
|
</set>
|
||||||
|
</property>
|
||||||
|
|
||||||
|
<!--list集合注入-->
|
||||||
|
<property name="bookList">
|
||||||
|
<list>
|
||||||
|
<value>JAVA</value>
|
||||||
|
<value>MySQL</value>
|
||||||
|
<value>Spring</value>
|
||||||
|
</list>
|
||||||
|
</property>
|
||||||
|
|
||||||
|
<!--map集合注入-->
|
||||||
|
<property name="bookMap">
|
||||||
|
<map>
|
||||||
|
<entry key="JAVA" value="马士兵"/>
|
||||||
|
<entry key="Go" value="马士兵"/>
|
||||||
|
<entry key="JVM" value="马士兵"/>
|
||||||
|
</map>
|
||||||
|
</property>
|
||||||
|
|
||||||
|
<!--List对象集合注入-->
|
||||||
|
<property name="bookList2" ref="bookList"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:p="http://www.springframework.org/schema/p"
|
||||||
|
xmlns:c="http://www.springframework.org/schema/c"
|
||||||
|
xmlns:util="http://www.springframework.org/schema/util"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/util
|
||||||
|
http://www.springframework.org/schema/util/spring-util.xsd
|
||||||
|
">
|
||||||
|
<bean id="user" class="com.msb.bean.User" init-method="initMethod" destroy-method="destroyMethod">
|
||||||
|
<property name="userid" value="1"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:p="http://www.springframework.org/schema/p"
|
||||||
|
xmlns:c="http://www.springframework.org/schema/c"
|
||||||
|
xmlns:util="http://www.springframework.org/schema/util"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/util
|
||||||
|
http://www.springframework.org/schema/util/spring-util.xsd
|
||||||
|
">
|
||||||
|
<bean id="dept" class="com.msb.bean.Dept" />
|
||||||
|
<bean id="emp" class="com.msb.bean.Emp" autowire="byName"/>
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,24 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:p="http://www.springframework.org/schema/p"
|
||||||
|
xmlns:c="http://www.springframework.org/schema/c"
|
||||||
|
xmlns:util="http://www.springframework.org/schema/util"
|
||||||
|
xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/util
|
||||||
|
http://www.springframework.org/schema/util/spring-util.xsd
|
||||||
|
http://www.springframework.org/schema/context
|
||||||
|
http://www.springframework.org/schema/context/spring-context.xsd
|
||||||
|
">
|
||||||
|
<context:property-placeholder location="classpath:jdbc.properties"/>
|
||||||
|
|
||||||
|
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
|
||||||
|
<property name="username" value="${jdbc_username}"></property>
|
||||||
|
<property name="password" value="${jdbc_password}"></property>
|
||||||
|
<property name="url" value="${jdbc_url}"></property>
|
||||||
|
<property name="driverClassName" value="${jdbc_driver}"></property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,4 @@
|
|||||||
|
jdbc_username=root
|
||||||
|
jdbc_password=12345678
|
||||||
|
jdbc_driver=com.mysql.cj.jdbc.Driver
|
||||||
|
jdbc_url=jdbc:mysql://127.0.0.1:3306/learnjdbc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.msb;
|
||||||
|
|
||||||
|
import com.msb.bean.User;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
public class Test1 {
|
||||||
|
@org.junit.Test
|
||||||
|
public void testGetBean(){
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||||
|
User user1 = applicationContext.getBean("user1", User.class);
|
||||||
|
System.out.println(user1);
|
||||||
|
|
||||||
|
|
||||||
|
User user2 = applicationContext.getBean("user2", User.class);
|
||||||
|
System.out.println(user2);
|
||||||
|
|
||||||
|
User user3 = applicationContext.getBean("user3", User.class);
|
||||||
|
System.out.println(user3);
|
||||||
|
|
||||||
|
User user4 = applicationContext.getBean("user4", User.class);
|
||||||
|
System.out.println(user4);
|
||||||
|
|
||||||
|
User user5 = applicationContext.getBean("user5", User.class);
|
||||||
|
System.out.println(user5);
|
||||||
|
|
||||||
|
User user6 = applicationContext.getBean("user6", User.class);
|
||||||
|
System.out.println(user6);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.msb;
|
||||||
|
|
||||||
|
import com.msb.bean.Cat;
|
||||||
|
import com.msb.bean.Mouse;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
public class Test2 {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBean(){
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext1.xml");
|
||||||
|
Mouse mouse = applicationContext.getBean("mouse1", Mouse.class);
|
||||||
|
System.out.println(mouse);
|
||||||
|
|
||||||
|
|
||||||
|
Cat cat1 = applicationContext.getBean("cat1", Cat.class);
|
||||||
|
System.out.println(cat1);
|
||||||
|
|
||||||
|
Cat cat2 = applicationContext.getBean("cat2", Cat.class);
|
||||||
|
System.out.println(cat2);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.msb;
|
||||||
|
|
||||||
|
import com.msb.bean.Mouse;
|
||||||
|
import com.msb.bean.Student;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class Test3 {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBean(){
|
||||||
|
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext3.xml");
|
||||||
|
Student student1 = applicationContext.getBean("student1", Student.class);
|
||||||
|
System.out.println(Arrays.toString(student1.getBooks()));
|
||||||
|
System.out.println(student1.getBookSet());
|
||||||
|
System.out.println(student1.getBookList());
|
||||||
|
System.out.println(student1.getBookMap());
|
||||||
|
System.out.println(student1.getBookList2());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.msb;
|
||||||
|
|
||||||
|
import com.msb.bean.User;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
public class Test4 {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBean(){
|
||||||
|
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext4.xml");
|
||||||
|
User user = applicationContext.getBean("user", User.class);
|
||||||
|
System.out.println("第四部: 获取");
|
||||||
|
applicationContext.close();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.msb;
|
||||||
|
|
||||||
|
import com.msb.bean.Emp;
|
||||||
|
import com.msb.bean.User;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
public class Test5 {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBean(){
|
||||||
|
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext5.xml");
|
||||||
|
Emp emp = applicationContext.getBean("emp", Emp.class);
|
||||||
|
System.out.println(emp);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.msb;
|
||||||
|
|
||||||
|
import com.alibaba.druid.pool.DruidDataSource;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
public class Test6 {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBean(){
|
||||||
|
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext6.xml");
|
||||||
|
DruidDataSource druidDataSource = applicationContext.getBean("dataSource", DruidDataSource.class);
|
||||||
|
System.out.println(druidDataSource);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>spring_ioc_02</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context</artifactId>
|
||||||
|
<version>6.0.11</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.msb.bean;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class Student {
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Student{}";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.msb.bean;
|
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class User {
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User{}";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.msb.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
|
@ComponentScan(basePackages = "com.msb")
|
||||||
|
@PropertySource("classpath:demo.properties")
|
||||||
|
public class SpringConfig {
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.msb.controller;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class UserController {
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.msb.dao;
|
||||||
|
|
||||||
|
public interface UserDao {
|
||||||
|
void add();
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.msb.dao.impl;
|
||||||
|
|
||||||
|
import com.msb.dao.UserDao;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class UserDaoImplA implements UserDao {
|
||||||
|
@Override
|
||||||
|
public void add(){
|
||||||
|
System.out.println("UserDaoImplA add ....");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.msb.dao.impl;
|
||||||
|
|
||||||
|
import com.msb.dao.UserDao;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class UserDaoImplB implements UserDao {
|
||||||
|
@Override
|
||||||
|
public void add(){
|
||||||
|
System.out.println("UserDaoImplB add ....");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.msb.service;
|
||||||
|
|
||||||
|
public interface UserService {
|
||||||
|
void add();
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.msb.service.impl;
|
||||||
|
|
||||||
|
import com.msb.dao.UserDao;
|
||||||
|
import com.msb.service.UserService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("userDaoImplA")
|
||||||
|
private UserDao userDao;
|
||||||
|
|
||||||
|
|
||||||
|
// 普通数据类型的属性赋值 8+String
|
||||||
|
@Value("${name}")
|
||||||
|
private String name;
|
||||||
|
@Value("${gender}")
|
||||||
|
private String gender;
|
||||||
|
@Value("${age}")
|
||||||
|
private Integer age;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(){
|
||||||
|
System.out.println("userServiceImpl add().....");
|
||||||
|
userDao.add();
|
||||||
|
System.out.println(name);
|
||||||
|
System.out.println(gender);
|
||||||
|
System.out.println(age);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/context
|
||||||
|
http://www.springframework.org/schema/context/spring-context.xsd
|
||||||
|
">
|
||||||
|
<context:component-scan base-package="com.msb.bean"/>
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/context
|
||||||
|
http://www.springframework.org/schema/context/spring-context.xsd
|
||||||
|
">
|
||||||
|
<context:component-scan base-package="com.msb"/>
|
||||||
|
|
||||||
|
<context:property-placeholder location="classpath:demo.properties"/>
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,3 @@
|
|||||||
|
name=\u5C0F\u660E
|
||||||
|
gender=\u7537
|
||||||
|
age=31
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.msb;
|
||||||
|
|
||||||
|
import com.msb.bean.Student;
|
||||||
|
import com.msb.bean.User;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
public class Test1 {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBean(){
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||||
|
User user = applicationContext.getBean("user", User.class);
|
||||||
|
System.out.println(user);
|
||||||
|
|
||||||
|
Student student = applicationContext.getBean("student", Student.class);
|
||||||
|
System.out.println(student);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.msb;
|
||||||
|
|
||||||
|
import com.msb.config.SpringConfig;
|
||||||
|
import com.msb.service.UserService;
|
||||||
|
import com.msb.service.impl.UserServiceImpl;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
public class Test2 {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBean(){
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext1.xml");
|
||||||
|
UserService userService = applicationContext.getBean("userServiceImpl", UserServiceImpl.class);
|
||||||
|
userService.add();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBean2(){
|
||||||
|
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
|
||||||
|
UserService userService = applicationContext.getBean("userServiceImpl", UserServiceImpl.class);
|
||||||
|
userService.add();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
@ -0,0 +1,87 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>spring_jbdctemplate_01</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<!--spring核心容器包-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context</artifactId>
|
||||||
|
<version>6.0.11</version>
|
||||||
|
</dependency>
|
||||||
|
<!--spring切面包-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-aspects</artifactId>
|
||||||
|
<version>6.0.11</version>
|
||||||
|
</dependency>
|
||||||
|
<!--aop联盟包-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>aopalliance</groupId>
|
||||||
|
<artifactId>aopalliance</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
|
<!--德鲁伊连接池-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid</artifactId>
|
||||||
|
<version>1.1.10</version>
|
||||||
|
</dependency>
|
||||||
|
<!--mysql驱动-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>8.0.28</version>
|
||||||
|
</dependency>
|
||||||
|
<!--springJDBC包-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-jdbc</artifactId>
|
||||||
|
<version>6.0.11</version>
|
||||||
|
</dependency>
|
||||||
|
<!--spring事务控制包-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-tx</artifactId>
|
||||||
|
<version>6.0.11</version>
|
||||||
|
</dependency>
|
||||||
|
<!--spring orm 映射依赖-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-orm</artifactId>
|
||||||
|
<version>6.0.11</version>
|
||||||
|
</dependency>
|
||||||
|
<!--Apache Commons日志包-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-logging</groupId>
|
||||||
|
<artifactId>commons-logging</artifactId>
|
||||||
|
<version>1.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!--Junit单元测试-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--lombok -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.12</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.msb.dao;
|
||||||
|
|
||||||
|
import com.msb.pojo.Dept;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface DeptDao {
|
||||||
|
int[] batchAdd(List<Dept> deptList);
|
||||||
|
|
||||||
|
int[] batchUpdate(List<Dept> deptList);
|
||||||
|
|
||||||
|
int[] batchDelete(List<Integer> deptnos);
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.msb.dao;
|
||||||
|
|
||||||
|
import com.msb.pojo.Student;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface StudentDao {
|
||||||
|
int findStuCount();
|
||||||
|
|
||||||
|
Student findStuById(int id);
|
||||||
|
|
||||||
|
List<Student> findStuByGrade(int grade);
|
||||||
|
|
||||||
|
int createStudent(Student student);
|
||||||
|
|
||||||
|
int updateStudent(Student student);
|
||||||
|
|
||||||
|
int deleteById(int id);
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
package com.msb.dao.impl;
|
||||||
|
|
||||||
|
import com.msb.dao.DeptDao;
|
||||||
|
import com.msb.pojo.Dept;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class DeptDaoImpl implements DeptDao {
|
||||||
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
public DeptDaoImpl(JdbcTemplate jdbcTemplate) {
|
||||||
|
this.jdbcTemplate = jdbcTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量添加部门
|
||||||
|
* @param deptList
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int[] batchAdd(List<Dept> deptList) {
|
||||||
|
String sql = "insert into dept values(DEFAULT, ?, ?)";
|
||||||
|
|
||||||
|
List<Object[]> args = new ArrayList<>();
|
||||||
|
for (Dept dept: deptList) {
|
||||||
|
Object[] arg = new Object[]{dept.getDname(), dept.getLoc()};
|
||||||
|
args.add(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
return jdbcTemplate.batchUpdate(sql, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量更新数据
|
||||||
|
* @param deptList
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int[] batchUpdate(List<Dept> deptList) {
|
||||||
|
String sql = "update dept set dname=?, loc=? where deptno=?";
|
||||||
|
|
||||||
|
List<Object[]> args = new ArrayList<>();
|
||||||
|
for (Dept dept: deptList) {
|
||||||
|
Object[] arg = new Object[]{dept.getDname(), dept.getLoc(), dept.getDeptno()};
|
||||||
|
args.add(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
return jdbcTemplate.batchUpdate(sql, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量删除部门
|
||||||
|
* @param deptnos
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int[] batchDelete(List<Integer> deptnos) {
|
||||||
|
String sql = "delete from dept where deptno=?";
|
||||||
|
|
||||||
|
List<Object[]> args = new ArrayList<>();
|
||||||
|
for (Integer deptno: deptnos) {
|
||||||
|
Object[] arg = new Object[]{deptno};
|
||||||
|
args.add(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
return jdbcTemplate.batchUpdate(sql, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,98 @@
|
|||||||
|
package com.msb.dao.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import com.msb.dao.StudentDao;
|
||||||
|
import com.msb.pojo.Student;
|
||||||
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class StudentDaoImpl implements StudentDao {
|
||||||
|
|
||||||
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
public StudentDaoImpl(JdbcTemplate jdbcTemplate){
|
||||||
|
this.jdbcTemplate = jdbcTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询数据库条数
|
||||||
|
* queryForObject(sql, Integer.class)
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int findStuCount() {
|
||||||
|
String sql = "select count(*) from students";
|
||||||
|
|
||||||
|
return jdbcTemplate.queryForObject(sql, Integer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询某条记录
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Student findStuById(int id) {
|
||||||
|
String sql = "select * from students where id = ?";
|
||||||
|
BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
|
||||||
|
|
||||||
|
return jdbcTemplate.queryForObject(sql, rowMapper, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询多条记录的集合
|
||||||
|
* @param grade
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<Student> findStuByGrade(int grade) {
|
||||||
|
String sql = "select * from students where grade = ?";
|
||||||
|
BeanPropertyRowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
|
||||||
|
|
||||||
|
return jdbcTemplate.query(sql, rowMapper, grade);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加记录
|
||||||
|
* @param student
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int createStudent(Student student) {
|
||||||
|
String sql = "insert into students values(DEFAULT, ?, ?, ?,?)";
|
||||||
|
Object[] args = new Object[]{
|
||||||
|
student.getName(),student.getGender(),student.getGrade(),student.getScore()
|
||||||
|
};
|
||||||
|
return jdbcTemplate.update(sql, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新数据
|
||||||
|
* @param student
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int updateStudent(Student student) {
|
||||||
|
String sql = "update students set name=?, gender=?,grade=?,score=? where id = ?";
|
||||||
|
Object[] args = new Object[]{
|
||||||
|
student.getName(),student.getGender(),student.getGrade(),student.getScore(), student.getId()
|
||||||
|
};
|
||||||
|
return jdbcTemplate.update(sql, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除记录
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int deleteById(int id) {
|
||||||
|
String sql = "delete from students where id = ?";
|
||||||
|
return jdbcTemplate.update(sql, id);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.msb.pojo;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Data
|
||||||
|
public class Dept {
|
||||||
|
private Integer deptno;
|
||||||
|
private String dname;
|
||||||
|
private String loc;
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.msb.pojo;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Student implements Serializable {
|
||||||
|
private Integer id;
|
||||||
|
private String name;
|
||||||
|
private Integer gender;
|
||||||
|
private Integer grade;
|
||||||
|
private Integer score;
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.msb.service;
|
||||||
|
|
||||||
|
import com.msb.pojo.Dept;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface DeptService {
|
||||||
|
int[] batchAdd(List<Dept> deptList);
|
||||||
|
|
||||||
|
int[] batchUpdate(List<Dept> deptList);
|
||||||
|
|
||||||
|
int[] batchDelete(List<Integer> deptnos);
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.msb.service;
|
||||||
|
|
||||||
|
import com.msb.pojo.Student;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface StudentService {
|
||||||
|
int findStuCount();
|
||||||
|
|
||||||
|
Student findStuById(int id);
|
||||||
|
|
||||||
|
List<Student> findStuByGrade(int grade);
|
||||||
|
|
||||||
|
int createStudent(Student student);
|
||||||
|
|
||||||
|
int updateStudent(Student student);
|
||||||
|
|
||||||
|
int deleteById(int id);
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.msb.service.impl;
|
||||||
|
|
||||||
|
import com.msb.dao.DeptDao;
|
||||||
|
import com.msb.pojo.Dept;
|
||||||
|
import com.msb.service.DeptService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class DeptServiceImpl implements DeptService {
|
||||||
|
private final DeptDao deptDao;
|
||||||
|
|
||||||
|
public DeptServiceImpl(DeptDao deptDao){
|
||||||
|
this.deptDao = deptDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int[] batchAdd(List<Dept> deptList) {
|
||||||
|
return deptDao.batchAdd(deptList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int[] batchUpdate(List<Dept> deptList) {
|
||||||
|
return deptDao.batchUpdate(deptList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int[] batchDelete(List<Integer> deptnos) {
|
||||||
|
return deptDao.batchDelete(deptnos);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.msb.service.impl;
|
||||||
|
|
||||||
|
import com.msb.dao.StudentDao;
|
||||||
|
import com.msb.pojo.Student;
|
||||||
|
import com.msb.service.StudentService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class StudentServiceImpl implements StudentService {
|
||||||
|
private final StudentDao studentDao;
|
||||||
|
|
||||||
|
public StudentServiceImpl(StudentDao studentDao){
|
||||||
|
this.studentDao = studentDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int findStuCount() {
|
||||||
|
return studentDao.findStuCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Student findStuById(int id) {
|
||||||
|
return studentDao.findStuById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Student> findStuByGrade(int grade) {
|
||||||
|
return studentDao.findStuByGrade(grade);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int createStudent(Student student) {
|
||||||
|
return studentDao.createStudent(student);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int updateStudent(Student student) {
|
||||||
|
return studentDao.updateStudent(student);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int deleteById(int id) {
|
||||||
|
return studentDao.deleteById(id);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:p="http://www.springframework.org/schema/p"
|
||||||
|
xmlns:c="http://www.springframework.org/schema/c"
|
||||||
|
xmlns:util="http://www.springframework.org/schema/util"
|
||||||
|
xmlns:context="http://www.springframework.org/schema/context"
|
||||||
|
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||||
|
xsi:schemaLocation="
|
||||||
|
http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||||
|
http://www.springframework.org/schema/util
|
||||||
|
http://www.springframework.org/schema/util/spring-util.xsd
|
||||||
|
http://www.springframework.org/schema/context
|
||||||
|
http://www.springframework.org/schema/context/spring-context.xsd
|
||||||
|
http://www.springframework.org/schema/aop
|
||||||
|
http://www.springframework.org/schema/aop/spring-aop.xsd
|
||||||
|
">
|
||||||
|
<!--spring 注解扫描-->
|
||||||
|
<context:component-scan base-package="com.msb"/>
|
||||||
|
<!--读取jdbc配置文件-->
|
||||||
|
<context:property-placeholder location="classpath:jdbc.properties"/>
|
||||||
|
<!--配置德鲁伊连接池-->
|
||||||
|
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
|
||||||
|
<property name="username" value="${jdbc_username}"></property>
|
||||||
|
<property name="password" value="${jdbc_password}"></property>
|
||||||
|
<property name="url" value="${jdbc_url}"></property>
|
||||||
|
<property name="driverClassName" value="${jdbc_driver}"></property>
|
||||||
|
</bean>
|
||||||
|
<!--配置JDBCTemplate对象,并向里面注入DataSource-->
|
||||||
|
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
|
||||||
|
<!--通过set方法注入连接池-->
|
||||||
|
<property name="dataSource" ref="dataSource"></property>
|
||||||
|
</bean>
|
||||||
|
</beans>
|
@ -0,0 +1,4 @@
|
|||||||
|
jdbc_username=root
|
||||||
|
jdbc_password=12345678
|
||||||
|
jdbc_driver=com.mysql.cj.jdbc.Driver
|
||||||
|
jdbc_url=jdbc:mysql://localhost:3306/learnjdbc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.msb.test;
|
||||||
|
|
||||||
|
import com.msb.pojo.Dept;
|
||||||
|
import com.msb.service.DeptService;
|
||||||
|
import com.msb.service.StudentService;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TestDept {
|
||||||
|
@Test
|
||||||
|
public void testBatchAdd(){
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||||
|
DeptService deptService = applicationContext.getBean(DeptService.class);
|
||||||
|
List<Dept> deptList = new ArrayList<>();
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
deptList.add(new Dept(null, "cname"+i, "loc"+i));
|
||||||
|
}
|
||||||
|
int[] rows= deptService.batchAdd(deptList);
|
||||||
|
System.out.println(Arrays.toString(rows));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testBatchUpdate(){
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||||
|
DeptService deptService = applicationContext.getBean(DeptService.class);
|
||||||
|
List<Dept> deptList = new ArrayList<>();
|
||||||
|
for (int i = 31; i <= 40; i++) {
|
||||||
|
deptList.add(new Dept(i, "newName", "newLoc"));
|
||||||
|
}
|
||||||
|
int[] rows= deptService.batchUpdate(deptList);
|
||||||
|
System.out.println(Arrays.toString(rows));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBatchDelete(){
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||||
|
DeptService deptService = applicationContext.getBean(DeptService.class);
|
||||||
|
List<Integer> deptnos = new ArrayList<>();
|
||||||
|
for (int i = 31; i <= 40; i++) {
|
||||||
|
deptnos.add(i);
|
||||||
|
}
|
||||||
|
int[] rows= deptService.batchDelete(deptnos);
|
||||||
|
System.out.println(Arrays.toString(rows));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.msb.test;
|
||||||
|
|
||||||
|
import com.msb.pojo.Dept;
|
||||||
|
import com.msb.pojo.Student;
|
||||||
|
import com.msb.service.StudentService;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TestStudent {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStudentCurd(){
|
||||||
|
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||||
|
StudentService studentService = applicationContext.getBean(StudentService.class);
|
||||||
|
int rows = studentService.findStuCount();
|
||||||
|
System.out.println(rows);
|
||||||
|
|
||||||
|
Student student = studentService.findStuById(1);
|
||||||
|
System.out.println(student);
|
||||||
|
|
||||||
|
List<Student> students = studentService.findStuByGrade(1);
|
||||||
|
students.forEach(System.out::println);
|
||||||
|
|
||||||
|
// Student newStu = new Student(null, "demo", 1, 1, 90);
|
||||||
|
// int row = studentService.createStudent(newStu);
|
||||||
|
// System.out.println(row);
|
||||||
|
|
||||||
|
// int row = studentService.updateStudent(new Student(13, "张三", 1, 3, 90));
|
||||||
|
// System.out.println(row);
|
||||||
|
|
||||||
|
int row = studentService.deleteById(14);
|
||||||
|
System.out.println(row);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue