目录
见代码目录
步骤:
- 在eclipse下创建项目stuManagement
- 添加.jar包(参照chapter03)
- 在src下创建com.hrbust.po.Student类,包含属性:String类型stuNo,stuName和score(Integer)
- 在src下创建com.hrbust.dao.StudentDao接口及其实现类StudentDaoImpl,使用jdbcTemplate实现Student的增删改查操作
- 在src下创建com.hrbust.aspect.LogAspect切面,定义环绕通知myAround(),实现日志环绕记录功能。
- 在src下创建applicationContext.xml文件,文件配置参考chapter03
- 在src下创建com.hrbust.test.StuTest类,调用selectStu()、insertStu()方法。
com.hrbust.po.Student类
package com.hrbust.po;
public class Student {
String stuNo;
String stuName;
Integer score;
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"stuNo='" + stuNo + '\'' +
", stuName='" + stuName + '\'' +
", score=" + score +
'}';
}
}
com.hrbust.dao.StudentDao接口
package com.hrbust.dao;
import com.hrbust.po.Student;
import java.util.List;
public interface StudentDao {
public int insert(Student student);
public List<Student> select();
}
studentDaoImpl
package com.hrbust.dao;
import com.hrbust.po.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.util.List;
public class StudentDaoImpl implements StudentDao{
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
public int insert(Student student) {
String sql = "insert into students(stuNo,stuName,score) value(?,?,?)";
Object[] obj = new Object[]{
student.getStuNo(),
student.getStuName(),
student.getScore()
};
int num = this.jdbcTemplate.update(sql,obj);
System.out.println("插入信息成功");
return num;
}
public List<Student> select() {
String sql = "select * from students";
RowMapper<Student> rowMapper = new BeanPropertyRowMapper<Student>(Student.class);
System.out.println("查询用户信息");
return this.jdbcTemplate.query(sql,rowMapper);
}
}
com.hrbust.aspect.LogAspect
package com.hrbust.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect
public class LogAspect {
@Pointcut("execution( * com.hrbust.dao.StudentDaoImpl.*(..))")
public void pointcut(){
}
@Around("pointcut()")
public Object myAround(ProceedingJoinPoint point)throws Throwable{
System.out.println("这是环绕通知:执行目标方法之前,模拟开启事务");
Object object = point.proceed();
System.out.println("这是环绕之后的部分:执行方法之后,模拟关闭事务");
return object;
}
}
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: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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/spring?useUnicode=true&
characterEncoding=utf-8&serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean name="studentDao" class="com.hrbust.dao.StudentDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean name="LogAdvice" class="com.hrbust.aspect.LogAspect"/>
<aop:aspectj-autoproxy/>
</beans>
com.hrbust.test.StuTest
package com.hrbust.test;
import com.hrbust.dao.StudentDao;
import com.hrbust.po.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class StuTest {
public static void main(String[] arg){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDao studentDao = context.getBean("studentDao",StudentDao.class);
Student student = new Student();
student.setStuNo("1234");
student.setStuName("小彤彤");
student.setScore(100);
List<Student> studentList=studentDao.select();
for(Student s : studentList)
{
System.out.println(s);
}
studentDao.insert(student);
}
}
敲码不易 点赞收藏的人有好运 期末必过