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

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace:名称空间数据库的字段名称 和 实体类的属性名称 不一样 则不能自动封装数据* 方法: 1.起别名 : 用as ,对不一样的列明起别名,让别名和列明相同 2.resultMap:完成不一样的属性名和列名的映射 1.定义<resultMap>标签 2.在<select>标签中,使用resultMap属性替换resultType属性?-->?<mapper namespace="com.wfy.mapper.BrandMapper">?    <resultMap id="brandResultMap"  type="brand">        <result property="brandName" column="brand_name"/>        <result property="companyName" column="company_name"/>    </resultMap>??    <delete id="DeleteByIds">      delete from brand where id in            <foreach collection="ids" item="id" open="(" separator="," close=")">               #{id}            </foreach>    </delete><!--  where brand_name=#{brand.brandName}-->    <select id="SelectByPageAndCondition" resultMap="brandResultMap">  select *    from brand <where>     <if test="brand.brandName !=null and brand.brandName!=''">         and brand_name like #{brand.brandName}     </if>     <if test="brand.companyName !=null and brand.companyName!=''">         and company_name like #{brand.companyName}     </if>     <if test="brand.status !=null">         and status=#{brand.status}     </if> </where>?       limit #{begin} ,#{size}?    </select>    <select id="SelectTotalCountByCondition" resultType="java.lang.Integer">          select count(*)            from brand        <where>            <if test="brandName !=null and brandName!=''">              and brand_name like #{brandName}            </if>            <if test="companyName !=null and companyName!=''">              and company_name like #{companyName}            </if>            <if test="status !=null">              and status=#{status}            </if>        </where>??</select>??</mapper>

注意这里的映射文件需要在resource目录下创建分层目录,通过‘\’进行分割
及文件的目录名为:“ com\wfy\mapper”
  1. Service层(业务逻辑层)
因为我们需要不断的调用SqlSession的方法,通过mapper映像的方式,为了降低程序的耦合度,同时为了简化代码,通过接口的方式来实现对方法的调用
  • 创建BrandService接口
package com.wfy.service;?import com.wfy.pojo.Brand;import com.wfy.pojo.PageBean;?import java.util.List;?public interface BrandService {?    //查询所有数据    public List<Brand> SelectAll();    //新增数据    void AddBrand(Brand brand); //更改数据信息    //获取查询到的数据进行表格显现    Brand SelectById(int id);    void UpdateBrand(Brand brand);?  //删除指定数据    void DeleteById(int id);?    //删除多条数据    void DeleteByIds(int[] ids);?   //分页查询     currentPage--当前页码    pageSize--每页展示条数    PageBean<Brand> SelectByPage(int currentPage,int pageSize);?    //分页条件查询    PageBean<Brand> SelectByPageAndCondition(int currentPage,int pageSize,Brand brand);?}

经验总结扩展阅读