标签:jar wired a20 ice pac sed blog hide 文件引入
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 }
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 }
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 }
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>
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 }
标签:jar wired a20 ice pac sed blog hide 文件引入
原文地址:https://www.cnblogs.com/newAndHui/p/9060381.html