- 创建BrandServiceImpl接口的实现方法
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; }}
经验总结扩展阅读
- 含具体案例 Java8新特性之Stream流
- 电影爱人完整剧情介绍?
- 会话跟踪技术 - Cookie 和 Session 快速上手 + 登陆注册案例
- 案例分享-https证书链不完整导致请求失败
- 不明原因不孕怎么办啊
- JavaWeb505错误,IDEA版问题解决
- 一 CPS攻击案例——基于脉冲宽度调制PWM的无人机攻击
- 黑蒜的制作方法
- 教育案例怎么写
- 大鱼歌词完整版
