spring-第二天

Spring-第二天

一、注解

1.@Component 实现bean的注入
2.@Service 标记当前类为service类
3.@Autowired 自动装配
4.@RunWith 让测试运行于Spring测试环境
5.@ContextConfiguration 引入spring配置文件

二、 单元测试

优点:单元测试可以更加方便我们测试程序的各个功能模块,并且极大缩解决bug的时间
如何使用:导入依赖包后,在需要进行测试的类中使用ctrl+shfit+t快速创建,使用@RunWith引入spring测试环境、@ContextConfiguration引入spring配置文件

三、AOP

OOA(object-oriented analysis,面向对象的分析)
OOD(Object-oriented design,面向对象的设计)
OOP(Object Oriented Programming,面向对象编程)
1、AOP是指面向切面编程,是OOP的一种延续,可以在不改动某个功能模块代码的基础上对某个方法添加新的功能。
利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
2、AOP相关概念:
目标对象:即真实对象,是指要添加功能的方法所属的对象。
代理:是指使用AOP所产生的对象。
连接点:在spring是指目标对象中的所有方法。
切入点:是指在目标对象中想要增加功能的方法。
通知:是指要增加的功能。
切面:切入点和通知的结合。
织入:使用AOP产生代理的过程。
3、如何实现:
首先需要创建一个目标类,并且实现某个接口中的方法。
然后创建另一个类,在该类中实现需要增加的功能。
在sping中导入命名空间,编写spring.xml文件

<aop:config>
        <aop:aspect ref="aspect">
            <aop:before method="sendBefore" pointcut="execution(public void com.example.dao.MessageDao.sendMsg())"></aop:before>
            <aop:after method="sendAfter" pointcut="execution(public void com.example.dao.MessageDao.sendMsg())"></aop:after>
        </aop:aspect>
    </aop:config>

其中method中是需要添加的功能的方法名,pointcut中是目标类的所对应的路径和切入点的方法名。
before和after表示添加的功能对应切入点的位置。

四、使用Spring-JDBC

步骤:1、导入依赖包,创建数据库表并编写实体类,导入昨天已定义好的jdbc.properties并且在bean标签中创建数据源

 <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

2、将数据源映射到jdbcTemplate中

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
     </bean>

3、在dao层创建接口和实现类,并生成测试方法。

修改:int row=jdbcTemplate1.update("update people set name=?,age=?,sex=?,money=? where id=?","赵六",null,null,null,4);
删除:int row=jdbcTemplate1.update("delete from people where id=?",3);
新增:int row=jdbcTemplate1.update("insert into people (name,age,sex,money) values(?,?,?,?)","李四",17,"女",1000.00);
根据id查询:People people=jdbcTemplate1.queryForObject("select * from people where id=?",new BeanPropertyRowMapper<People>(People.class),1);
查询全部: List<People> peopleList=jdbcTemplate1.query("select * from people",new BeanPropertyRowMapper<People>(People.class));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值