Spring中使用的依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
使用注解开发的配置文件
创建ApplicationContext.xml配置文件
ApplicationContext.xml配置文件
<?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: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">
<!-- 开启aop注解(会自动识别注解)-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 开启注解扫描包 使用注解必须要使用注解扫描包-->
<context:component-scan base-package="dao,service"></context:component-scan>
<!--织入增强-->
<bean id="Aspect" class="MyAspect.MyAspect"></bean>
</beans>
注解开发常用的注解
-
@Component
: 用于标记一个类为Spring的组件,表示这个类将被Spring自动扫描并注册为一个Bean。 -
@Autowired
: 用于自动注入依赖,可以用在构造方法、属性、方法或者参数上。Spring会自动查找匹配的Bean并完成注入。 -
@Qualifier
: 用于指定注入Bean时的限定符,配合@Autowired
使用,指定具体的Bean实例 -
@Service:用于标注业务逻辑层
-
@Repository 用来标注访问层(dao的实现类)
使用注解实现增删改查
实体类
解释:@Data注解可以帮我们自动生成get set方法 和toString
1.创建pojo包 在pojo中创建一个学生类
===========================================================================================
package pojo;
import lombok.Data;
@Data
public class Student {
private Integer stuId;
private String stuName;
private Integer age;
private Integer gradeId;
}
dao包
接口中定义增删改查的方法
=========================================================================================
package dao;
import pojo.Student;
import java.util.List;
/**
* @author andy
* @version 1.0.0
* @date 2023年08月16日 14:03:02
* @packageName dao
* @className StudentDao
* @describe 使用注解注入开发
*/
public interface StudentDao {
/**
* 新增学生信息
* @return
*/
int AddStudent();
/**
* 删除学生信息
* @return
*/
int deleteStudent(Integer stuId);
/**
* 修改
* @param student
* @return
*/
int updateStudent(Student student);
/**
* 查询学生信息
* @return
*/
List<Student>searchAll();
}
接口实现类
解释:(此处增删改查的代码替成了输出语句)
1.@Repository 用来标注访问层
=========================================================================================
@Repository
public class StudentDaoImpl implements StudentDao{
@Override
public int AddStudent() {
System.out.println("新增学生信息成功!");
return 0;
}
@Override
public int deleteStudent(Integer stuId) {
System.out.println("删除学生信息成功!");
return 0;
}
@Override
public int updateStudent(Student student) {
System.out.println("修改学生信息成功!");
return 0;
}
@Override
public List<Student> searchAll() {
System.out.println("查询学生信息成功!");
return null;
}
}
service(业务逻辑层)
业务逻辑层的接口
========================================================================================
public interface StudentService {
/**
* 新增学生信息
* @return
*/
int AddStudent();
/**
* 删除学生信息
* @return
*/
int deleteStudent(Integer stuId);
/**
* 修改
* @param student
* @return
*/
int updateStudent(Student student);
/**
* 查询学生信息
* @return
*/
List<Student> searchAll();
}
业务逻辑层实现类
解释:
1.@Service("StudentService") 表示这个类是一个业务逻辑层
此注解相当于在ApplicationContext.xml配置文件中创建了<bean id="StudentService" class="dao.StudentDaoImpl"></bean>
2. @Autowired 可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作
=========================================================================================
@Service("StudentService")
public class StudentServiceImpl implements StudentService{
@Autowired
private StudentDao studentDao;
@Override
public int AddStudent() {
return studentDao.AddStudent();
}
@Override
public int deleteStudent(Integer stuId) {
return studentDao.deleteStudent(stuId);
}
@Override
public int updateStudent(Student student) {
return studentDao.updateStudent(student);
}
@Override
public List<Student> searchAll() {
return studentDao.searchAll();
}
}
测试类
@Test此处使用了junit的单元测试
=========================================================================================
public class TestStudent {
/**
* 新增测试
*/
@Test
public void AddStudent(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
StudentService service=(StudentService)context.getBean("StudentService");
service.AddStudent();
}
/**
* 删除测试
*/
@Test
public void DeleteStudent(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
StudentService service=(StudentService)context.getBean("StudentService");
service.deleteStudent(1);
}
/**
* 查询测试
*/
@Test
public void SelectStudent(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
StudentService service=(StudentService)context.getBean("StudentService");
service.searchAll();
}
/**
* 修改测试
*/
@Test
public void UpdateStudent(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
StudentService service=(StudentService)context.getBean("StudentService");
service.searchAll();
}
}