Spring Aspectj xml注解,JDBCSessionTemplate

   先创建一个借口,并写上实现的方法:
public interface Some {
    public void add();
    public void ent();
}
创建接口实现类,并继承该接口,实现该接口中的方法:
public class SomeImpl implements Some {
    public void add() {
        System.out.println("--------add-------");
    }

    public void ent() {
        System.out.println("--------ent-------");
    }
}
在创建一个增强类;
public class MyAspect {

    //前置增强
    public void  before(){
        System.out.println("=======前置增强======");
    }
     public  void before(JoinPoint jp){
         System.out.println("前置通知方法before() jp="+jp);
     }

    //后置增强
    public void afterReturing(){
        System.out.println("=========后置曾强=======");
    }
    public  void afterReturing(String result){
        System.out.println("后置方法通知 result="+result);
    }

    //环绕增强通知
   public  Object  around(ProceedingJoinPoint  pjo) throws Throwable {
       System.out.println("环绕通知方法,目标执行之前");
       Object  result=pjo.proceed();
       System.out.println("环绕通知方法,目标方法执行之后");     
       String temp=null;
       if(result!=null){
     temp= (String) result;
     temp=temp.toUpperCase();
     }
        return  temp;
}
  //异常通知  
     public  void  afterThrowing(){ 
       System.out.println("异常通知方法");  
  }   
     public  void  afterThrowing(Exception ex){ 
       System.out.println("异常通知方法 ex="+ex.getMessage());  
  }  
  //最终通知   
     public  void after(){   
       System.out.println("最终通知方法"); 
    }
 }
applicationContestSpringAspectjXML.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
">
        <!--注解Aspectj-->
        <!--目标对象-->
        <bean id="Someimp" class="cn.happy.SpringAspecthxml.SomeImpl"></bean>
        <!--增强 通知-->
        <bean id="aspect" class="cn.happy.SpringAspecthxml.MyAspect"></bean>
        <!--aop-->
        <aop:config>
                <aop:pointcut id="mycut" expression="execution(* *..SpringAspecthxml.*.*(..))"></aop:pointcut>
                <aop:aspect ref="aspect">
                      //前置增强
                  <aop:before method="before" pointcut-ref="mycut"></aop:before>
                   <!-- <aop:before method="before(org.aspectj.lang.JoinPoint)" pointcut-ref="mycut"></aop:before>-->
                     //后置增强
                    <aop:after-returning method="afterReturing" pointcut-ref="mycut"></aop:after-returning>
                  <!--  <aop:after-returning method="afterReturing(java.lang.String)" pointcut-ref="mycut" returning="result"></aop:after-returning>-->
                        //环绕增强
                   <!--  <aop:around method="around" pointcut-ref="mycut"></aop:around>-->
                         //最终增强
                   <!-- <aop:after method="after" pointcut-ref="mycut"></aop:after>-->
                            //异常增强
                    <!--   <aop:after-throwing method="afterThrowing" pointcut-ref="mycut"></aop:after-throwing>-->
                    <!--<aop:after-throwing method="afterThrowing(java.lang.Exception)" pointcut-ref="mycut" throwing="ex"></aop:after-throwing>-->
                </aop:aspect>
        </aop:config>
</beans>
测试类:
public class SpringAspectjXML {
    @Test
    public void  pectxml(){
        ApplicationContext  apl=new ClassPathXmlApplicationContext("applicationContestSpringAspectjXML.xml");
        Some  some= (Some) apl.getBean("Someimp");
       some.add();
       some.ent();
    }
}
前置,后置测试结果:
=======前置增强======
        --------add-------
        =========后置曾强=======
        =======前置增强======
        --------ent-------
        =========后置曾强=======
----------------------------------JDBCSessionTemplate-------------------------------
创建一个实体类,并写上属性值,并将其进行get,set封装
public class User {
    private  Integer id;
    private  String userCode;  //用户编号
    private  String userName;  //用户名称
    private  String userPassword; //用户密码
    private  Integer gender;  //性别
    private Date  birthday;  //出生日期
    private  String  phone;  //电话
    private  Integer userRole; //用户角色id
    private  Integer createdBy;  //创建者
    private  Date  creationDate;  //创建时间
    private  Integer modifyBy;  //更新者
    private  Date  modifyDate;  //更新时间
    private  String userRoleName;  //用户角色名称
创建dao层,在该层中创建UserDao接口
public interface UserDao {
    public List<User> findall();
}
在dao层中创建接口实现类UserDaoImpl并继承JdbcDaoSupport类,并实现userDao接口中的方法
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
    public List<User> findall() {
        String sql="select * from smbms_user";
        //接口的一个实现类 匿名内部类
        List<User> list=this.getJdbcTemplate().query(sql, new RowMapper<User>() {
            /**
             *
             * @param   rs:单条记录读取器
             * @param i  索引
             * @return
             * @throws SQLException
             */
            public User mapRow(ResultSet rs, int i) throws SQLException {
                User book=new User();
                book.setId(rs.getInt("id"));
                book.setUserName(rs.getString("userName"));
                book.setCreationDate(rs.getDate("creationDate"));
                return book;
            }
        });
        return list;
    }
}
创建Service中,并在该层中创建UserService接口,该接口与dao层中的接口是一致的
public interface UserService {
    public List<User> findall();
}
在创建一个接口实现类UserServiceImpl
public class UserServiceImpl implements UserService {
    private UserDao  udao;

    public UserDao getUdao() {
        return udao;
    }

    public void setUdao(UserDao udao) {
        this.udao = udao;
    }

    public List<User> findall() {
        return udao.findall();
    }
}
applicationContestSqlSessionTemplate.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
      ">

    <!--识别jdbc.properties文件-->
 <context:property-placeholder location="jdbc.properties"></context:property-placeholder>

 <!--   &lt;!&ndash;创建数据源 Spring &ndash;&gt;
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
-->
   <!-- &lt;!&ndash;创建数据源   dbcp &ndash;&gt;
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>-->

   <!-- &lt;!&ndash;创建数据源   c3p0&ndash;&gt;
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>-->

    <!--创建数据源   druid-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--jdbcTemplate配置-->
      <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
          <property name="dataSource" ref="dataSource"></property>
      </bean>
    <!--dao配置-->
    <bean id="userDao" class="cn.happy.SpringJDBCSqlSessionTemplate.cn.happy.dao.daoimpl.UserDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

     <!--service userservice-->
     <bean id="userservice" class="cn.happy.SpringJDBCSqlSessionTemplate.cn.happy.service.serviceimpl.UserServiceImpl">
         <property name="udao" ref="userDao"></property>
     </bean>
</beans>
测试类:
public class SpringJDBCTemplate {
    @Test
    // 01.jdbctemplate
    public void test02(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContestSqlSessionTemplate.xml");
        UserService service = (UserService) ctx.getBean("userservice");
        List<User> list = service.findall();
        for (User item:list) {
            System.out.println(item.getUserName());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值