博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
05_ssm基础(四)之Spring基础二
阅读量:4625 次
发布时间:2019-06-09

本文共 10327 字,大约阅读时间需要 34 分钟。

24.spring配置dbcp并完成CRUD操作

  1.准备jar包

  

   2.编辑Product模型

1 package com.day02.ssm.spring.model; 2  3 public class Product{ 4     private Integer id; 5     private String productName;  //产品名称 6     private Integer salePrice; 7     private Integer costPrice; 8  9     public Integer getId() {10         return id;11     }12 13     public void setId(Integer id) {14         this.id = id;15     }16 17     public String getProductName() {18         return productName;19     }20 21     public void setProductName(String productName) {22         this.productName = productName;23     }24 25     public Integer getSalePrice() {26         return salePrice;27     }28 29     public void setSalePrice(Integer salePrice) {30         this.salePrice = salePrice;31     }32 33     public Integer getCostPrice() {34         return costPrice;35     }36 37     public void setCostPrice(Integer costPrice) {38         this.costPrice = costPrice;39     }40 }
Product

 

  3.编辑接口

1 package com.day02.ssm.spring.dao; 2  3 import com.day02.ssm.spring.model.Product; 4  5 import java.sql.SQLException; 6  7 /** 8  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html 9  * 疑问咨询wx:85129834810  */11 public interface IProductDao {12     //增13     public void save(Product product) throws SQLException;14     //删15     public void delete(int id);16     //改17     public void update(Product product);18     //查19     public Product query(int id);20 21 }
IProductDao

 

  4.编辑实现类

1 package com.day02.ssm.spring.dao.impl; 2  3 import com.day02.ssm.spring.dao.IProductDao; 4 import com.day02.ssm.spring.model.Product; 5 import org.apache.commons.dbcp.BasicDataSource; 6  7 import java.sql.Connection; 8 import java.sql.PreparedStatement; 9 import java.sql.SQLException;10 11 /**12  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html13  * 疑问咨询wx:85129834814  */15 public class ProductDao implements IProductDao {16     private BasicDataSource basicDataSource;17     @Override18     public void save(Product product) {19         try {20             //连接21             Connection connection = basicDataSource.getConnection();22             String sql = "INSERT INTO product (product_name,sale_price) VALUES (?,?)";23             //创建编译语句24             PreparedStatement preparedStatement = connection.prepareStatement(sql);25             preparedStatement.setString(1,product.getProductName());26             preparedStatement.setInt(2,product.getSalePrice());27             //执行28             preparedStatement.executeUpdate();29             //释放30             preparedStatement.close();31             connection.close();32         } catch (SQLException e) {33             e.printStackTrace();34         }35     }36 37     @Override38     public void delete(int id) {39 40     }41 42     @Override43     public void update(Product product) {44 45     }46 47     @Override48     public Product query(int id) {49         return null;50     }51 52     public BasicDataSource getBasicDataSource() {53         return basicDataSource;54     }55 56     public void setBasicDataSource(BasicDataSource basicDataSource) {57         this.basicDataSource = basicDataSource;58     }59 }
ProductDao

  5.编写spring配置文件bdcp-config.xml

1 
2
6
7
8
9
10
11
12
13
14
15
16
dbcp-config.xml

  6.将bdcp-config.xml配置文件引入到主配置文件中(非常容易忘记)

  

  7.测试dao

1 package com.day02.ssm.spring.test; 2  3 import com.day02.ssm.spring.dao.impl.ProductDao; 4 import com.day02.ssm.spring.model.Product; 5 import com.day02.ssm.spring.model.Ticket; 6 import org.junit.Test; 7 import org.junit.runner.RunWith; 8 import org.springframework.beans.factory.BeanFactory; 9 import org.springframework.beans.factory.annotation.Autowired;10 import org.springframework.test.context.ContextConfiguration;11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;12 13 /**14  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html15  * 疑问咨询wx:85129834816  */17 @RunWith(SpringJUnit4ClassRunner.class)//把junit加载到spring容器中去18 @ContextConfiguration("classpath:spring-config.xml")19 public class TestProductDao {20 21    // private ProductDao productDao=new ProductDao();22     @Autowired23     private ProductDao productDao;//从spring中获取dao对象24     @Test25     public void test(){26         Product product = new Product();27         product.setProductName("苹果");28         product.setSalePrice(89);29         productDao.save(product);30     }31 32 }
TestProductDao

25.spring中的硬编码解决

  1.配置文件

  

  2.使用

  

 

 26.spring中的继承使用

  

 

 27.Autowired详细讲解

  Autowired和Qualifier标签

1.通过@Autowired标签可以让Spring自动的把属性需要的对象从Spring容器中找出来,并注入给该属性。(生产上就用这一招)
2.第三方程序:Spring3.0之前,需要手动配置@Autowired解析注解程序,Spring就会自动的加入针对@Autowired标签的解析程序。从Spring3.0开始,可以不再需要改配置了,但是只能在Spring容器中才生效.。
    <context:annotation-config />,
   无论使用哪一个版本,都得配置.
3.@Autowired标签贴在字段或者setter方法上。
4.@Autowired可以同时为一个属性注入多个对象。
      public void setXxx(OtherBean1 other1,OtherBean2 other2) {}
5.使用@Autowired标签可以注入Spring内置的重要对象,比如BeanFactory,ApplicationContext。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpringTest {
    @Autowired
    private ApplicationContext ctx;
}
6.默认情况下@Autowired标签必须要能找到对应的对象,否则报错。不过,可使用required=false来避免该问题:@Autowired(required=false)
7.@Autowired找bean的方式:
    1)、首先按照依赖对象的类型找,如果找到则使用setter方法或者字段直接注入;
    2)、如果在Spring上下文中找到多个匹配的类型,再按照名字去找,如果没有匹配则报错;
    3)、可以通过使用@Qualifier("otherBean")标签来规定依赖对象按照bean的id+类型去找;
public class SomeBean {
    @Autowired
    private OtherBean1 other1;
    @Autowired(required = false)
            @Qualifier("otherbean2")
    private OtherBean2 other2;
    public String toString() {
        return "SomeBean [other=" + other1 + ", other2=" + other2 + "]";
    }
}
 演示代码:

  SomeBean

1 package com.day02.ssm.spring.model2; 2  3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.beans.factory.annotation.Qualifier; 5  6 /** 7  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html 8  * 疑问咨询wx:851298348 9  */10 public class SomeBean {11     private String name;12     //添加OtherBean1对象13     @Autowired14     @Qualifier("otherBean11")15     private OtherBean1 otherBean1;16     private OtherBean2 otherBean2;17     18     public String getName() {19         return name;20     }21 22     public void setName(String name) {23         this.name = name;24     }25    // @Autowired26    /* public void setOtherBean1(OtherBean1 otherBean1) {27         this.otherBean1 = otherBean1;28     }*/29   /*  @Autowired30     public void setTwo(OtherBean1 otherBean1,OtherBean2 otherBean2) {31         System.out.println("==otherBean1=============otherBean2=====");32         this.otherBean1 = otherBean1;33         this.otherBean2 = otherBean2;34 35     }*/36 }
SomeBean

  OtherBean1

1 package com.day02.ssm.spring.model2; 2  3 /** 4  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html 5  * 疑问咨询wx:851298348 6  */ 7 public class OtherBean1 { 8     private String name; 9 10     public String getName() {11         return name;12     }13 14     public void setName(String name) {15         this.name = name;16     }17 }
OtherBean1

  OtherBean2

1 package com.day02.ssm.spring.model2; 2  3 /** 4  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html 5  * 疑问咨询wx:851298348 6  */ 7 public class OtherBean2 { 8     private String name; 9 10     public String getName() {11         return name;12     }13 14     public void setName(String name) {15         this.name = name;16     }17 }
OtherBean2

     spring配置文件

1 
2
9
10
11
12
13
14
15 16
17
18
19
20 21
22
23
24 25
26
27
28
29
View Code

  测试代码

1 package com.day02.ssm.spring.model2; 2  3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.test.context.ContextConfiguration; 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 10 /**11  * 课程笔记:http://www.cnblogs.com/newAndHui/category/1153640.html12  * 疑问咨询wx:85129834813  */14 @RunWith(SpringJUnit4ClassRunner.class)//把junit加载到spring容器中去15 @ContextConfiguration("classpath:spring-config2.xml")16 public class TestSomeBean {17 18     @Autowired(required = false)19      private SomeBean  someBean;20    // @Autowired21    // private ApplicationContext ctx;22 23     @Test24     public void test() {25        // SomeBean someBean = (SomeBean) ctx.getBean("someBean11");26         System.out.println("someBean"+ someBean);27     }28 29 }
TestSomeBean

 28.Resource标签

  @Resource标签:

  1,@Resource标签是JavaEE规范的标签;
  2,@Resource标签也可以作用于字段或者setter方法;
  3,也可以使用@Resource标签注入一些spring内置的重要对象,比如BeanFactory.ApplicationContext;
  4,@Resource必须要求有匹配的对象;
  5,<context:annotation-config>既引入了@Autowired标签的解析器,也引入了@Resource的解析器;
  6,@Resource标签找bean的方式:
    1),首先按照名字去找,如果找到,就使用setter或者字段注入;
    2),如果按照名字找不到,再按照类型去找,但如果找到多个匹配类型,报错;
    3),可以直接使用name属性指定bean的名称;但是,如果指定的name,就只能按照name去找,如果找不到,就不会再按照类型去找;
public class SomeBean {
    @Resource(name="otherBean1")
    private OtherBean1 other1;
    @Resource(name="otherBean2")
    private OtherBean2 other2;
    public String toString() {
        return "SomeBean [otherBean=" + other1 + ", otherBean2="
                + other2 + "]";
    }

 

@Resource(name = "other1") 和 @Autowired ,@Qualifier("other1")选用:

1:使用Autowired标签,让代码和Spring耦合在一起,若某一天不使用Spring,就得改源代码. 重点:生产上一般使用:Autowired
2:Resource标签,本身就是javaEE的,本身不存在耦合问题.
随便使用哪一组,要求:都得掌握.

29.使用注解简化IoC

  使用标签简化IoC:

    1.使用标签来完成IoC,就必须有IoC标签的解析器
    使用context:component-scan来扫描spring需要管理的bean
    base-package就告诉spring,去哪些包及其子包里去扫描bean,如果有多个包需要被扫描;只需要用逗号隔开多个包即可
     <!--   扫描包-->
    <context:component-scan base-package="com.day02.ssm.spring.model2" />
    <!--开启注解-->
    <context:annotation-config/>

  2.标注Bean的注解:@Component

  默认情况,直接使用类的名字(首字母小写作为bean的名字)
  如果要修改bean的名称;直接使用value属性来重新定义bean的名称
  @Component("otherbean")
  public class OtherBean {}
  

  Spring的零配置

  

 

  IoC和DI操作,选用注解还是XML?

1):使用XML,直观,侵入性低,统一配置.
2):使用注解,简单,快速,不是很直观,让配置分散.
有时候,只能使用xml,不能使用注解?
   比如配置一个DataSource.
     你不可能去DataSource类上去贴标签.
一般的,我们使用xml做统一的配置,若有特殊的配置,可以使用注解来辅助.
 二者都得掌握.

 

转载于:https://www.cnblogs.com/newAndHui/p/9060381.html

你可能感兴趣的文章
ACM-ICPC2018北京网络赛 Tomb Raider(暴力)
查看>>
循环 数组
查看>>
锁(3)-- DB锁
查看>>
Microsoft(C)注册服务器(32位)CPU占用高
查看>>
find -exec
查看>>
linux查找目录下的所有文件中是否含有某个字符串 (转)
查看>>
CodeForces468B Two Sets 解题报告
查看>>
3-3文件修改
查看>>
20145221 《信息安全系统设计基础》第0周学习总结
查看>>
Ubuntu 安装PostgreSQL
查看>>
数据可视化(6)--Google Charts实例
查看>>
hdu4339 Query
查看>>
关于Android 打开新的Activity 虚拟键盘的弹出与不弹出
查看>>
“万能数据库查询分析器”在四大软件下载网站的排行榜中均入围前10,可喜可贺...
查看>>
和菜鸟一起学linux总线驱动之smartcard操作模式和协议与参数选择
查看>>
android 开发工具(转)
查看>>
python中的uuid4
查看>>
CSS 必知的7个知识点
查看>>
asp.net mvc 生成条形码
查看>>
单调队列
查看>>