目录
见代码目录
步骤:
com.hrbust.po.Student类
com.hrbust.dao.StudentDao接口
studentDaoImpl
com.hrbust.aspect.LogAspect
applicationContext.xml
com.hrbust.test.StuTest
见代码目录
步骤:
- 在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 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 select() {
String sql = "select * from students";
RowMapper rowMapper = new BeanPropertyRowMapper(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
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 studentList=studentDao.select();
for(Student s : studentList)
{
System.out.println(s);
}
studentDao.insert(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 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 select() {
String sql = "select * from students";
RowMapper rowMapper = new BeanPropertyRowMapper(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
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 studentList=studentDao.select();
for(Student s : studentList)
{
System.out.println(s);
}
studentDao.insert(student);
}
}
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 select() {
String sql = "select * from students";
RowMapper rowMapper = new BeanPropertyRowMapper(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
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 studentList=studentDao.select();
for(Student s : studentList)
{
System.out.println(s);
}
studentDao.insert(student);
}
}
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 studentList=studentDao.select();
for(Student s : studentList)
{
System.out.println(s);
}
studentDao.insert(student);
}
}
敲码不易 点赞收藏的人有好运 期末必过



