1. 程式人生 > >05_ssm基礎(四)之Spring基礎二

05_ssm基礎(四)之Spring基礎二

主配置文件 文件 cep HR turn ner ret 操作 none

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:851298348
10  */
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.html
13  * 疑問咨詢wx:851298348
14  */
15 public class ProductDao implements IProductDao {
16     private BasicDataSource basicDataSource;
17     @Override
18     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     @Override
38     public void delete(int id) {
39 
40     }
41 
42     @Override
43     public void update(Product product) {
44 
45     }
46 
47     @Override
48     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 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4         xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6      <!--配置dataSources-->
 7     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 8         <property name="url" value="jdbc:mysql://localhost:3306/station_data"/>
 9         <property name="username" value="root"/>
10         <property name="password" value="admin"/>
11     </bean>
12    <!-- ProductDao配置dao-->
13     <bean id="productDao" class="com.day02.ssm.spring.dao.impl.ProductDao">
14         <property name="basicDataSource" ref="dataSource"/>
15     </bean>
16 </beans>
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.html
15  * 疑問咨詢wx:851298348
16  */
17 @RunWith(SpringJUnit4ClassRunner.class)//把junit加載到spring容器中去
18 @ContextConfiguration("classpath:spring-config.xml")
19 public class TestProductDao {
20 
21    // private ProductDao productDao=new ProductDao();
22     @Autowired
23     private ProductDao productDao;//從spring中獲取dao對象
24     @Test
25     public void test(){
26         Product product = new Product();
27         product.setProductName("蘋果");
28         product.setSalePrice(89);
29         productDao.save(product);
30     }
31 
32 }
TestProductDao

05_ssm基礎(四)之Spring基礎二