Lab6-JdbcTemplate Trinh Bay[1]

Embed Size (px)

Citation preview

  • 8/10/2019 Lab6-JdbcTemplate Trinh Bay[1]

    1/12

    LAB6: TRNH BY VI JdbcTemplate

    Nguyn Nghim [email protected] Trang 1

    MC TIUKt thc bi thc hnh ny, bn c khnng

    To menu ng Trnh by hng ha dng ct vi CSS

    M T

    Hot ng:

    Chy products.htm hin thtrang web gm menu loi hng ha (c tCSDL) v tt chng ha

    Nhp [mt loi hng ha] shin thcc hng ha thuc loi c chn

    THC HINTrong bi ny, bn sphi to mt project c tchc nh sau:

  • 8/10/2019 Lab6-JdbcTemplate Trinh Bay[1]

    2/12

    LAB6: TRNH BY VI JdbcTemplate

    Nguyn Nghim [email protected] Trang 2

    Bc 1: Th vin v cu hnh project

    Bc 2: CSDL

    Bc 4: To lp DAO v Entity

    Bc 5: To giao din

    Bc 6: To Controller

    Bc 7: Chy

  • 8/10/2019 Lab6-JdbcTemplate Trinh Bay[1]

    3/12

    LAB6: TRNH BY VI JdbcTemplate

    Nguyn Nghim [email protected] Trang 3

    Bc 1: Th vin v cu hnh project

    Th vin

    Bn cnh cc th vin ca Th vin cn thit cho ng dng

    SQLServerDriver

    o sqljdbc4.jar

    JdbcTemplate

    o commons-dbcp.jar

    o spring-jdbc-3.2.1.RELEASE.jar

    o

    spring-tx-3.2.1.RELEASE.jar

    Cu hnh Web.xml

    SpringMVCEmail

    index.jsp

    dispatcher

    org.springframework.web.servlet.DispatcherServlet

    contextConfigLocation/WEB-INF/spring-config-*.xml

    1

  • 8/10/2019 Lab6-JdbcTemplate Trinh Bay[1]

    4/12

    LAB6: TRNH BY VI JdbcTemplate

    Nguyn Nghim [email protected] Trang 4

    dispatcher*.htm

    Cu hnh Spring MVC np nhiu file cu hnh: spring-config-*.xml. Du * si din cho nhm k tbt

    k. Cthbi ny l mvc, gmail v upload

    spring-config-mvc.xml

    Khai bo bean InternalResourceViewResolver xl view

    Chr package tm kim cc component l com.lycato

    Chr ng dng Spring ny c php sdng annotation

    spring-config-jdbc.xml

  • 8/10/2019 Lab6-JdbcTemplate Trinh Bay[1]

    5/12

    LAB6: TRNH BY VI JdbcTemplate

    Nguyn Nghim [email protected] Trang 5

    http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    Trong file cu hnh ny bn phi khai bo 2 bean.

    BasicDataSource: bean ny cu hnh cc thng skt ni CSDL

    JdbcTemplate: bean ny c khai bo n lm vic vi CSDL c tim vo v sdng sau nytrong ng dng

    Bc 2: CSDL

    Hnh: CSDL Seminar cha 2 bng Products v Categories

    Bc 4: To lp m tv truy xut dliu

    Lp m tdliu (Entity)

    Category.java

    packagecom.lycato.entity;

    publicclassCategory {Integer id;String name, namevn;

    publicInteger getId() {returnid;

    }

  • 8/10/2019 Lab6-JdbcTemplate Trinh Bay[1]

    6/12

    LAB6: TRNH BY VI JdbcTemplate

    Nguyn Nghim [email protected] Trang 6

    publicvoidsetId(Integer id) {this.id= id;

    }publicString getName() {

    returnname;}publicvoidsetName(String name) {

    this.name= name;}publicString getNamevn() {

    returnnamevn;}publicvoidsetNamevn(String namevn) {

    this.namevn= namevn;}

    }

    Product.java

    packagecom.lycato.entity;

    publicclassProduct {

    Integer id, quantity, categoryId;String name, image;Double unitPrice;

    publicInteger getId() {returnid;

    }publicvoidsetId(Integer id) {

    this.id= id;}publicInteger getQuantity() {

    returnquantity;}publicvoidsetQuantity(Integer quantity) {

    this.quantity= quantity;

    }publicInteger getCategoryId() {returncategoryId;

    }publicvoidsetCategoryId(Integer categoryId) {

    this.categoryId= categoryId;}publicString getName() {

    returnname;}publicvoidsetName(String name) {

    this.name= name;}publicString getImage() {

    returnimage;

    }publicvoidsetImage(String image) {this.image= image;

    }publicDouble getUnitPrice() {

    returnunitPrice;}publicvoidsetUnitPrice(Double unitPrice) {

    this.unitPrice= unitPrice;}

  • 8/10/2019 Lab6-JdbcTemplate Trinh Bay[1]

    7/12

    LAB6: TRNH BY VI JdbcTemplate

    Nguyn Nghim [email protected] Trang 7

    }

    Lp truy xut dliu (DAO)Lp ny cha cc phng thc thao tc dliu (thm, sa, xa) v truy vn dliu.

    Insert(): thm

    Update(): sa

    Delete(): xa

    getXyz(): truy vn

    Lp no c ch thch bi @Repository c th tim vo ProductController trong ng dng bi

    @Autowire sdng sau ny.

    CategoryDAO.java

    packagecom.lycato.dao;

    importjava.io.Serializable;

    importjava.util.List;

    importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jdbc.core.BeanPropertyRowMapper;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.jdbc.core.RowMapper;importorg.springframework.stereotype.Repository;

    importcom.lycato.entity.Category;

    @RepositorypublicclassCategoryDAO{

    /*** Inject t

  • 8/10/2019 Lab6-JdbcTemplate Trinh Bay[1]

    8/12

    LAB6: TRNH BY VI JdbcTemplate

    Nguyn Nghim [email protected] Trang 8

    String sql = "DELETE FROM Categories WHERE Id=?";jdbc.update(sql, id);

    }

    /*** Truy vn 1 thc ththeo m* @paramid m thc thcn truy vn

    * @returnthc thtruy vn c*/

    publicCategory getById(Serializable id) {String sql = "SELECT * FROM Categories WHERE Id=?";returnjdbc.queryForObject(sql, getRowMapper(), id);

    }

    /*** Truy vn tt ccc thc th* @returndanh sch thc thtruy vn c*/

    publicList getAll() {String sql = "SELECT * FROM Categories";returngetBySql(sql);

    }

    /*** Truy vn cc thc ththeo cu lnh sql* @paramsql cu lnh truy vn* @returndanh sch thc thtruy vn c*/

    protectedList getBySql(String sql) {returnjdbc.query(sql, getRowMapper());

    }

    /*** nh xcu trc bn ghi theo thuc tnh ca bean* @returnnh xbn ghi theo thuc tnh bean*/

    privateRowMapper getRowMapper() {

    returnnewBeanPropertyRowMapper(Category.class);}

    }

    ProductDAO.java

    packagecom.lycato.dao;

    importjava.io.Serializable;importjava.util.List;

    importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jdbc.core.BeanPropertyRowMapper;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.jdbc.core.RowMapper;

    importorg.springframework.stereotype.Repository;

    importcom.lycato.entity.Product;

    @RepositorypublicclassProductDAO{

    /*** Inject t

  • 8/10/2019 Lab6-JdbcTemplate Trinh Bay[1]

    9/12

    LAB6: TRNH BY VI JdbcTemplate

    Nguyn Nghim [email protected] Trang 9

    /*** Thm mi 1 thc th* @paramentity l thc thmi*/

    publicvoidinsert(Product entity) {String sql = "INSERT INTO Products (Name, UnitPrice, Quantity, Image,

    CategoryId) VALUES (?,?,?,?,?)";jdbc.update(sql, entity.getName(), entity.getUnitPrice(),

    entity.getQuantity(), entity.getImage(), entity.getCategoryId());}

    /*** Cp nht thc th* @paramentity l thc thcn cp nht*/

    publicvoidupdate(Product entity) {String sql = "UPDATE Products SET Name=?, UnitPrice=?, Quantity=?, Image=?,

    CategoryId=? WHERE Id=?";jdbc.update(sql, entity.getName(), entity.getUnitPrice(),

    entity.getQuantity(), entity.getImage(), entity.getCategoryId(), entity.getId());}

    /*** Xa thc ththeo m* @paramid m thc thcn xa*/

    publicvoiddelete(Serializable id) {String sql = "DELETE FROM Products WHERE Id=?";jdbc.update(sql, id);

    }

    /*** Truy vn 1 thc ththeo m* @paramid m thc thcn truy vn* @returnthc thtruy vn c*/

    publicProduct getById(Serializable id) {String sql = "SELECT * FROM Products WHERE Id=?";returnjdbc.queryForObject(sql, getRowMapper(), id);

    }

    /*** Truy vn tt ccc thc th* @returndanh sch thc thtruy vn c*/

    publicList getAll() {String sql = "SELECT * FROM Products";returngetBySql(sql);

    }

    /**

    * Truy vn cc thc ththeo cu lnh sql* @paramsql cu lnh truy vn* @returndanh sch thc thtruy vn c*/

    protectedList getBySql(String sql) {returnjdbc.query(sql, getRowMapper());

    }

    /*** Truy vn thc ththeo tn* @paramname tn ca thc thcn truy vn

  • 8/10/2019 Lab6-JdbcTemplate Trinh Bay[1]

    10/12

    LAB6: TRNH BY VI JdbcTemplate

    Nguyn Nghim [email protected] Trang 10

    * @returndanh sch thc thtruy vn c*/

    publicList getByName(String name) {String sql = "SELECT * FROM Products WHERE Name LIKE ?";returnjdbc.query(sql, getRowMapper(), "%"+ name + "%");

    }

    /*** nh xcu trc bn ghi theo thuc tnh ca bean* @returnnh xbn ghi theo thuc tnh bean*/

    privateRowMapper getRowMapper() {returnnewBeanPropertyRowMapper(Product.class);

    }

    /*** Truy vn thc ththeo m loi* @paramcategoryId l m loi* @returndanh sch thc thtruy vn c*/

    publicList getByCategoryId(Integer categoryId) {String sql = "SELECT * FROM Products WHERE CategoryId=?";

    returnjdbc.query(sql, getRowMapper(), categoryId);}

    }

    Bc 5: To giao din

    Products.jsp

    Spring MVC Seminar 2014

    ${c.namevn}|

    ${p.name}

    $${p.unitPrice}

  • 8/10/2019 Lab6-JdbcTemplate Trinh Bay[1]

    11/12

    LAB6: TRNH BY VI JdbcTemplate

    Nguyn Nghim [email protected] Trang 11

    nn-styles.cssnh ngha style trnh by cho mi mt hng.

    ul{

    padding: 5px;margin: 5px;list-style: none;display: inline-block;width: 250px;text-align: center;border-radius: 10px;box-shadow:005pxgray;

    }li>img{

    max-width: 180px;height: 200px;

    }a{

    text-decoration: none;color:blue;font-variant: small-caps;

    }a:hover{

    color:red;}

    Bc 6: To Controllerpackagecom.lycato.controller;

    importjava.util.List;

    importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.ModelMap;importorg.springframework.web.bind.annotation.ModelAttribute;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;

    importcom.lycato.dao.CategoryDAO;importcom.lycato.dao.ProductDAO;importcom.lycato.entity.Category;

    @ControllerpublicclassProductController {

    /*** Inject t@RepositoryProductDAO*/@AutowiredProductDAO pdao;

    /*** Inject t@RepositoryCategoryDAO*/@AutowiredCategoryDAO cdao;

    /*** Truy vn Listv t vo model

  • 8/10/2019 Lab6-JdbcTemplate Trinh Bay[1]

    12/12

    LAB6: TRNH BY VI JdbcTemplate

    Nguyn Nghim [email protected] Trang 12

    */@ModelAttribute("categories")publicList getCategories() {

    returncdao.getAll();}

    /**

    * GET|POST: products.htm*/@RequestMapping("products")publicString showProducts(ModelMap model,

    @RequestParam(value="cate", required=false) Integer categoryId) {if(categoryId == null){

    model.addAttribute("products", pdao.getAll());}else{

    model.addAttribute("products", pdao.getByCategoryId(categoryId));}return"Products";

    }}

    Khi bn tng tc vo chng trnh tng:

    STT Hnh ng Phng thc M t

    1 Chy products.htm showProducts(), GET Hin thmenu loi v danh sch hng ha

    2 Nhp [loi] showProducts(categoryId),POST

    Hin thmenu loi v danh sch hng ha ca loic chn

    Bc 7: Chyhttp://localhost:8080/SpringMVCJdbc/products.htm