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<Store> getList();
Page<Store> getListPage(Map param);
}
2.StoreMapping.xml
<?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">
<mapper namespace="com.luyi.apiservice.mapper.StoreMapper">
<select id="getInfoById" parameterType="Integer" resultType="com.luyi.apiservice.models.Store">
<!-- #{对象属性} ,主键理论上任何值都可以 #{abc},#{xxx},可以不用对象属性-->
select * from ly_store where id = #{id}
</select>
<select id="getList" resultType="com.luyi.apiservice.models.Store">
select * from ly_store limit 0,10
</select>
<select id="getListPage" parameterType="map" resultType="com.luyi.apiservice.models.Store">
select id, name, mobile, created_at from ly_store
</select>
</mapper>
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<Store> 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<News> getListByPage(Integer page){
Page<News> 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<News> 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());