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

  • 创建BrandServiceImpl接口的实现方法
因为需要不断的调用SqlSessionFactoy工厂,为了简化代码,创建Util包,编写一个SqlSessionFactoryUtils来简化代码逻辑
package com.wfy.util;?import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;?import java.io.IOException;import java.io.InputStream;?public class SqlSessionFactoryUtils {    private static SqlSessionFactory sqlSessionFactory;    static {       //静态代码块会随着类的加载而自动执行,且只执行一次        try {            String resource = "mybatis-config.xml";            InputStream inputStream = null;            inputStream = Resources.getResourceAsStream(resource);            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);      } catch (IOException e) {            throw new RuntimeException(e);      }  }    public static SqlSessionFactory getSqlSessionFactory(){             return sqlSessionFactory;  }}?package com.wfy.service.impl;?import com.wfy.mapper.BrandMapper;import com.wfy.pojo.Brand;import com.wfy.pojo.PageBean;import com.wfy.service.BrandService;import com.wfy.util.SqlSessionFactoryUtils;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;?import java.util.List;?public class BrandServiceImpl implements BrandService {    //1.创建sqlSessionFactory 工厂对象    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();    public List<Brand> SelectAll() {     //2.获取SqlSession对象        SqlSession sqlSession = factory.openSession();        //3.获取BrandMapper        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);        //4.调用方法        List<Brand> brands = mapper.SelectAll();        //5.释放资源        sqlSession.close();        return brands;  }?    public void AddBrand(Brand brand) {        SqlSession sqlSession = factory.openSession();        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);        mapper.AddBrand(brand);        sqlSession.commit();        sqlSession.close();  }?    public Brand SelectById(int id) {       //2.获取SqlSession对象        SqlSession sqlSession = factory.openSession();        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);        Brand brand = mapper.SelectById(id);        //关闭资源        sqlSession.close();?        return brand;?  }?    public void UpdateBrand(Brand brand) {        SqlSession sqlSession = factory.openSession();        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);        mapper.UpdateBrand(brand);        //提交事务、关闭资源        sqlSession.commit();        sqlSession.close();  }?    public void DeleteById(int id) {        SqlSession sqlSession = factory.openSession();        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);        mapper.deleteById(id);        sqlSession.commit();        sqlSession.close();  }?    public void DeleteByIds(int[] ids) {        //2.获取SqlSession对象        SqlSession sqlSession = factory.openSession();        //3.获取BrandMapper        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);        //4.调用方法          mapper.DeleteByIds(ids);?        sqlSession.commit();?        sqlSession.close();?  }?    public PageBean<Brand> SelectByPage(int currentPage, int pageSize) {        //2.获取SqlSession对象        SqlSession sqlSession = factory.openSession();        //3.获取BrandMapper        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);?        //4.计算开始索引=(当前页码数-1)*展示的信息条数        int begin=(currentPage-1) * pageSize;        //计算条目数        int size=pageSize;       //5.查询当前页数据        List<Brand> rows = mapper.SelectByPage(begin, size);        //6.查询总记录数        int totalCount = mapper.SelectTotalCount();        //7.封装PageBean对象        PageBean<Brand> pageBean=new PageBean<Brand>();        pageBean.setRows(rows);        pageBean.setTotalCount(totalCount);?        //8.释放资源        sqlSession.close();        return pageBean;?  }?    public PageBean<Brand> SelectByPageAndCondition(int currentPage, int pageSize, Brand brand) {        //2.获取SqlSession对象        SqlSession sqlSession = factory.openSession();        //3.获取BrandMapper        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);?        //4.计算开始索引=(当前页码数-1)*展示的信息条数        int begin=(currentPage-1) * pageSize;        //计算条目数        int size=pageSize;        //处理brand条件,设置模糊表达式        String brandName = brand.getBrandName();        if (brandName !=null&&brandName.length()>0){            brand.setBrandName("%"+brandName+"%");      }        String companyName = brand.getCompanyName();        if (companyName !=null&&companyName.length()>0){            brand.setCompanyName("%"+companyName+"%");      }        //5.查询当前页数据        List<Brand> rows = mapper.SelectByPageAndCondition(begin,size,brand);        //6.查询总记录数        int totalCount = mapper.SelectTotalCountByCondition(brand);        //7.封装PageBean对象        PageBean<Brand> pageBean=new PageBean<Brand>();        pageBean.setRows(rows);        pageBean.setTotalCount(totalCount);?        //8.释放资源        sqlSession.close();        return pageBean;  }}

经验总结扩展阅读