java Mybatis使用

1.StoreMapper Mapper

import com.github.pagehelper.Page;
import com.luyi.apiservice.models.Store;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
@Mapper
public interface StoreMapper {
    Store getInfoById(int id);
    List getList();
    Page getListPage(Map param);
}

2.StoreMapping.xml





    
    
    

3.StoreService

import com.github.pagehelper.Page;
import com.luyi.apiservice.mapper.StoreMapper;
import com.luyi.apiservice.models.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class StoreService {
    @Autowired
    StoreMapper storeMapper;
    @Autowired
    JdbcTemplate jdbcTemplate;

    // 获取用户信息
    public Store getStoreById(int id){
        return storeMapper.getInfoById(id);
    }
    public List getList(){
        return storeMapper.getList();
    }
    public Page getSListByLimit(Map param){
        System.out.println(param);
        return storeMapper.getListPage(param);
    }

    /**
     * 获取单个信息
     * @param name
     * @return
     */
    public String getNameById(Integer store_id,String name){
        if(store_id > 0){
            if(name == null){
                name = "name";
            }
            String sql = "select "+name+" from ly_store where id=?";
            try {
                String info = jdbcTemplate.queryForObject(sql,new Object[]{store_id},String.class);
                return info;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }
    
    // 获取分页数据
    public Page getListByPage(Integer page){
        Page list = newsMapper.getListByPage(page);
        for( News info: list) {
            String img = AppConstant.WEB_URL+info.getImage();
            info.setImage(img);
        }
        return list;
    }
}

5.输出层

    PageHelper.startPage(page,10);
    Page data = newsService.getListByPage(page);
    JSONObject result = new JSONObject();
    result.put("list",data);
    result.put("pages",data.getPages());
    result.put("last_page",data.getPages());
    result.put("total",data.getTotal());

你可能感兴趣的:(java Mybatis使用)