JavaWeb完整案例详细步骤( 四 )

  1. Dao层
  • 创建pojo实体类
package com.wfy.pojo;?public class Brand {    //id 主键    private Integer id;    //brandName 品牌名称    private String brandName;    //企业名称    private String companyName;    //排序字段    private Integer ordered;    //描述信息    private String description;    //状态: 0:禁用1:启动    private Integer status;    private String statusStr;?    public Integer getId() {        return id;  }?    public void setId(Integer id) {        this.id = id;  }?    public String getBrandName() {        return brandName;  }?    public void setBrandName(String brandName) {        this.brandName = brandName;  }?    public String getCompanyName() {        return companyName;  }?    public void setCompanyName(String companyName) {        this.companyName = companyName;  }?    public Integer getOrdered() {        return ordered;  }?    public void setOrdered(Integer ordered) {        this.ordered = ordered;  }?    public String getDescription() {        return description;  }?    public void setDescription(String description) {        this.description = description;  }?    public Integer getStatus() {        return status;  }?    //逻辑视图    public String getStatusStr(){        if(status==1){            return "启用";      }        return status==0 ?"禁用":"启用";  }?    public void setStatus(Integer status) {        this.status = status;  }?    @Override    public String toString() {        return "Brand{" +                "id=" + id +                ", brandName='" + brandName + '\'' +                ", companyName='" + companyName + '\'' +                ", ordered=" + ordered +                ", description='" + description + '\'' +                ", status=" + status +                '}';  }}
  • PageBean实体类--分页查询
package com.wfy.pojo;?import java.util.List;?//分页查询的JavaBeanpublic class PageBean<T> {?    //总记录数    private  int totalCount;?    //当前业数据    private List<T> rows;?    public int getTotalCount() {        return totalCount;  }?    public void setTotalCount(int totalCount) {        this.totalCount = totalCount;  }?    public List<T> getRows() {        return rows;  }?    public void setRows(List<T> rows) {        this.rows = rows;  }}
  • BrandMapper的接口和映射文件
package com.wfy.mapper;?import com.wfy.pojo.Brand;import org.apache.ibatis.annotations.*;?import java.util.List;?public interface BrandMapper {?    //查询所有    @Select("select * from brand")    @ResultMap("brandResultMap")    List<Brand> SelectAll();?    //新增品牌    @Insert("insert into brand values (null,#{brandName},#{companyName},#{ordered},#{description},#{status})")    void AddBrand(Brand brand);?    //根据id来获取指定品牌信息,实现数据的回显操作    @Select("select * from brand where id = #{id}")    @ResultMap("brandResultMap")    Brand SelectById(int id);    //更改品牌信息    @Update("updatebrand set brand_name=#{brandName},company_name=#{companyName},ordered=#{ordered},description=#{description} where id=#{id} ")    void UpdateBrand(Brand brand);?    //删除指定品牌数据    @Delete("delete from brand where id =#{id}")    void deleteById(int id);?    //删除多条数据    void DeleteByIds( @Param("ids") int[] ids);?    //分页查询begin--开始的索引size--获取信息的条目数    @Select("select * from brand limit #{begin},#{size} ")   @ResultMap("brandResultMap")    List<Brand> SelectByPage(@Param("begin") int begin,@Param("size") int size);?    //查询总信息条数    @Select("selectcount(*) from brand")    int SelectTotalCount();?    //分页条件查询begin--开始的索引size--获取信息的条目数    List<Brand> SelectByPageAndCondition(@Param("begin") int begin,@Param("size") int size,@Param("brand")Brand brand);?    //条件查询总信息条数    int SelectTotalCountByCondition(Brand brand);}

经验总结扩展阅读