基于注解的控制器(2)

  我们首先对类进行介绍,Product是产品类,包括产品的名称、描述和价格属性,该类实现了Serializable接口,而ProductForm类与HTML表单相映射,是Product在服务端的代表,表单对象传递ServletRequest给其他组件,还有当数据校验失败时,表单对象将用于保存和展示用户在原始表单上的输入。DBOperator类负责与mysql数据库打交道,ProductService定义了服务层的接口,ProductServiceImpl实现了ProductService中定义的接口。

Product类代码如下,包含属性和属性的set和get方法:

 

public class Product implements Serializable{ private static final long serialVersionUID = 1L; private String name; private String description; private double price; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public static long getSerialversionuid() { return serialVersionUID; } }

View Code

ProductForm代码如下,和Product类唯一区别就是不用实现Serializable接口:

 

public class ProductForm { private String name; private String description; private double price; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }

View Code

DBOperator类包含两个方法,一个是执行sql语句,一个是从数据库中查询数据:

public class DBOperator { private JdbcTemplate jdbcTemplate; //通过setter的方式实现依赖注入 public void setJdbcTemplate(JdbcTemplate jdbcTemplate){ this.jdbcTemplate = jdbcTemplate; } public Map query(String sql,int id){ Map productInfo = this.jdbcTemplate.queryForMap(sql, id); return productInfo; } public void insert(String sql,Object[] obj){ this.jdbcTemplate.update(sql, obj); } }

View Code

  服务层的ProductService和实现类ProductServiceImpl如下,ProductServiceImpl类中通过@Autowired和@Service进行DBOperator对象的依赖,@Autowired注解会使DBOperator的一个实例被注入到ProductServiceImpl实例中,为了能是类能被spring扫描到,必须为其标注为@Service。

public interface ProductService {
   
public Long addProduct(Product product);
   
public Product queryProduct(long productID);
}

@Service
public class ProductServiceImpl implements ProductService{
   
//生成产品的ID,是数据库表中的主键
    private AtomicLong generator = new AtomicLong();
   
    @Autowired
   
private DBOperator operator; @Overridepublic Long addProduct(Product product) {
       
// TODO Auto-generated method stub
        Long id = generator.incrementAndGet();
        String name
= product.getName();
        String description
= product.getDescription();
       
double price = product.getPrice();
        Object[] obj
= new Object[]{id,name,description,price};
        String sql
= "insert into t_product values(?,?,?,?)";
        operator.insert(sql,obj);
       
return id;
    }

    @Override
   
public Product queryProduct(long productID) {
       
// TODO Auto-generated method stub String sql = "select * from t_product where id=?";
        Map productInfo
= this.operator.query(sql,(int)productID);
        String name
= (String) productInfo.get("name");
        String description
= (String) productInfo.get("description");
       
double price = (double) productInfo.get("price");
        Product product
= new Product();
        product.setDescription(description);
        product.setName(name);
        product.setPrice(price);
       
return product;
    }
}

View Code

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/44a320d08c8c2bf00f6e93a3dfbe3c9a.html