smm第五天作业

创建Car类,1) 借助set给属性注入值

借助构造方法给属性注入值。【两个都是三个参数的构造方法】

创建类,包含集合属性,如何给以下集合属性注入信息 3>创建部门和员工类将类交给spring管理,给dept中的list集合赋值.一种list赋值。util:list

emp:给emp中的dept赋值(2:01:22) 4>注解的练习:【熟练掌握】

创建Service以及Dao service依赖dao 。自动注入dao.

dao又多了一个实现类,如何注入?

纯注解,不需要配置文件

AOP练习 1>创建普通的类 public class EmpDao(){ //添加方法 //修改的方法 //查看的方法 } 2>创建切面类,切面类中规定对哪些类中的方法做增强 查看,添加,修改 在方法执行都需要获取数据库的链接,在方法执行之后都需要关闭数据库的链接【环绕】 添加,修改的方法,在方法之前执行,需要开启事务,如果没有异常提交事务,如果有异常,需要回滚事务 3>用配置和注解的方式实现

car:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Car {
    private Integer car_num;
    private String car_name;
    private Double car_price;
}

beans:

<bean id="car" class="com.openlab.beans.Car">
        <property name="car_num" value="100"></property>
        <property name="car_name" value="宝马"></property>
        <property name="car_price" value="1000000.00"></property>
    </bean>

test:

@Test
    public void testCar(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Car car = applicationContext.getBean("car",Car.class);
        System.out.println(car.getCar_num());
        System.out.println(car.getCar_name());
        System.out.println(car.getCar_price());
    }

结果:

构造方法1:

public Car(Integer car_num, String car_name, Double car_price){
        this.car_num = car_num;
        this.car_name = car_name;
        this.car_price = car_price;
    }

 beans:

<bean id="car1" class="com.openlab.beans.Car">
        <constructor-arg value="100"></constructor-arg>
        <constructor-arg value="宝马"></constructor-arg>
        <constructor-arg value="2000000"></constructor-arg>
    </bean>

test:

@Test
    public void testCar2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Car car = applicationContext.getBean("car1",Car.class);
        System.out.println(car.getCar_num());
        System.out.println(car.getCar_name());
        System.out.println(car.getCar_price());
    }

结果:

 构造方法2:

public Car(String car_name, Integer car_num, Double car_price){
        this.car_num = car_num;
        this.car_name = car_name;
        this.car_price = car_price;
    }

bean:

<bean id="car2" class="com.openlab.beans.Car">
        <constructor-arg value="宝马"></constructor-arg>
        <constructor-arg value="200"></constructor-arg>
        <constructor-arg value="3000000"></constructor-arg>
    </bean>

test:

@Test
    public void testCar3(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Car car = applicationContext.getBean("car2",Car.class);
        System.out.println(car.getCar_num());
        System.out.println(car.getCar_name());
        System.out.println(car.getCar_price());
    }

结果:

 dept:

public class Dept {
    private Integer dept_id;
    private String dept_name;
    private List<Emp> empList;
}

bean:

 <bean id="dept" class="com.openlab.beans.Dept">
        <property name="empList">
            <list>
                <ref bean="emp"></ref>
                <ref bean="emp2"></ref>
                <ref bean="emp3"></ref>
            </list>
        </property>
    </bean>

    <bean id="emp" class="com.openlab.beans.Emp">
        <property name="emp_id" value="1"></property>
        <property name="emp_name" value="jerry"></property>
        <property name="emp_address" value="0808"></property>
    </bean>

    <bean id="emp2" class="com.openlab.beans.Emp">
        <property name="emp_id" value="2"></property>
        <property name="emp_name" value="lucy"></property>
        <property name="emp_address" value="3030"></property>
    </bean>

    <bean id="emp3" class="com.openlab.beans.Emp">
        <property name="emp_id" value="3"></property>
        <property name="emp_name" value="jack"></property>
        <property name="emp_address" value="4567"></property>
    </bean>

test:

@Test
    public void testDept(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Dept dept = applicationContext.getBean("dept",Dept.class);
        for (Emp emp: dept.getEmpList()){
            System.out.println(emp.getEmp_id()+"\t"+emp.getEmp_name()+"\t"+emp.getEmp_address());
        }
    }

结果:

 emp的bean:

<bean id="emp_it" class="com.openlab.beans.Emp">
        <property name="dept" ref="dept1"></property>
    </bean>

    <bean id="dept1" class="com.openlab.beans.Dept">
        <property name="dept_id" value="1"></property>
        <property name="dept_name" value="IT"></property>
    </bean>

test:

@Test
    public void testEmp_Dept(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Emp emp = applicationContext.getBean("emp_it",Emp.class);
        Dept dept = emp.getDept();
        System.out.println(dept.getDept_id());
        System.out.println(dept.getDept_name());
    }

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值