zoukankan      html  css  js  c++  java
  • spring代理例子

     

    ----------------------------------------------------------

    先来看一下目录结构

     

     

    显然service里面有两个java文件,UserDao是接口,UserDaoImpl是接口的实现类,在使用时都是用的接口向外对接

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------

    再来看看所用的jar包

     

     上面的jar基本上是做spring开发的所有jar,但肯定还是不全,需要什么再加什么吧

    jar百度网盘链接:https://pan.baidu.com/s/1utLam37cKHNlBaj9z9CSUw

    提取码:9eba

    -------------------------------------------------------------------------------------------------------------------------------------------------------------

    接下来我们看一下核心配置文件

    核心配置文件里面很多配置的东西,其实都可以复制,很多都是固定写法

    <?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"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
    >

    <!-- 这里要注意class是实现类,不是接口类-->
    <bean id = "userDao" class = "lf.com.service.UserDaoImpl"></bean>

    <!-- 建立连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/liuyan"></property>
    <property name="username" value="root"></property>
    <property name="password" value="1234"></property>
    </bean>

    <!-- 管理事务的类 -->
    <bean id="txManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:advice id="txadvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="select*" read-only="true"/>
    </tx:attributes>
    </tx:advice>

    <aop:config>
    <aop:pointcut expression="execution(* lf.com.service.UserDao.*(..))" id="mypoint"/>

    <!-- 这里也要注意,是pointcut-ref,不是pointcut -->
    <aop:advisor advice-ref="txadvice" pointcut-ref="mypoint"/>
    </aop:config>

    </beans>

     

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------

     

    接下来是bean类

    也就是传说中的封装,其实就是把数据库里的列,封装为一个类的属性,这样通过这个类来使用这些属性

     

    package lf.com.bean;

    /**
    *
    * @author Administrator
    * 这里User使用的是liuyan这个数据库里面的user表
    * 要求属性和表里的列名相同,类型也要相同
    *
    */
    public class User {
    private int id;
    private String userName;
    private String pwd;
    private int age;
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getUserName() {
    return userName;
    }
    public void setUserName(String userName) {
    this.userName = userName;
    }
    public String getPwd() {
    return pwd;
    }
    public void setPwd(String pwd) {
    this.pwd = pwd;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public User(int id, String userName, String pwd, int age) {
    super();
    this.id = id;
    this.userName = userName;
    this.pwd = pwd;
    this.age = age;
    }
    public User() {
    super();
    // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
    return "User [id=" + id + ", userName=" + userName + ", pwd=" + pwd
    + ", age=" + age + "]";
    }

    }

     

    ----------------------------------------------------------------------------------------------------------------------------------------------------

     

    service中UserDao接口

     

    package lf.com.service;

    import lf.com.bean.User;

    public interface UserDao {
    public User select(int id);
    }

     

    ----------------------------------------------------------------------------

     

    service中UserDaoImpl实现类

     

    package lf.com.service;

    import lf.com.bean.User;

    public class UserDaoImpl implements UserDao{

    @Override
    public User select(int id) {
    System.out.println("The id is"+ id);
    return null;
    //throw new UnsupportedOperationException();
    }

    }

     

    -----------------------------------------------------------------------------------------------------------

     

    测试类

     

    package lf.com.test;

    import lf.com.bean.User;
    import lf.com.service.UserDao;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class Test {

    /**
    * @param args
    */
    public static void main(String[] args) {
    System.out.println("before----------");

    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

    System.out.println("after------------");

    UserDao userDao = (UserDao)ac.getBean("userDao");

    System.out.println("userDado-------------");

    User user = userDao.select(3);

    if (user != null) {
    System.out.print("找到了用户");
    }else{
    System.out.println("没有找到用户");
    }
    }

    }

     

    ---------------------------------------------------------------------------------------

    结果:

    ----------------------------------

     

  • 相关阅读:
    CPP Info Memo part3
    在Google搜索结果显示原始链接(转自 月光博客)
    libc 之 locales
    Git 分支管理与本地 repository 创建
    py2exe issue: ImportError: No module named _fontdata_enc_winansi (http://stackoverflow.com/)
    CPP Info Memo (Part 1)
    CPP Info Memo part2
    HOWTO: Increase write speed by 'aligning' FAT32(通过对齐 FAT32 提高U盘访问速度, 转载)
    (转载)Gentoo中文man乱码
    如何选择开源许可证?(转载)
  • 原文地址:https://www.cnblogs.com/youcandomore/p/9302273.html
Copyright © 2011-2022 走看看