springmvc实现查数据下拉框
jsp页面部分 遍历输出数据库信息
<div class="weui_cell">
<div class="weui_cell_hd">
<label class="weui_label">店名</label>
</div>
<select class="weui_select" id="shopid" name="shopid">
<c:forEach items="${list}" var="shop"> //list为后台传回来的集合名称 shop为下列对象调用
<option value="${shop.id}" id="option2">${shop.shopname}</option> //shop.shopname shopname为实体类字段
</c:forEach>
</select>
</div>
------------------------------------------------------------------------------------------------------------------------------------------------------
controller部分
@Controller
@RequestMapping("/phone")
public class PhoneContorller {
@Autowired
public BtyShopService btyShopService; //调用需要用的Service
@RequestMapping(value = { "/Create"}, method = RequestMethod.GET)
public String createPage(Model model, ServletRequest request) {
List<BtyShop> list = btyShopService.ListAll(); //get方法加载 Service的方法调用 执行sql语句
request.setAttribute("list", list); //"list"为传回前台的集合对象
List<Region> region= regionService.ListAll();
request.setAttribute("region", region);
return "phone/Create"; //返回页面信息
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
Mapper部分
package com.mybatis.mapper;
import java.util.List;
import com.mybatis.model.Region;
public interface RegionMapper {
int deleteByPrimaryKey(Integer id);
int insert(Region record);
int insertSelective(Region record);
Region selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Region record);
int updateByPrimaryKey(Region record);
List<Region> getAll(); //为被调用的使用方法
}
---------------------------------------------------------------------------------------------------------------------------------------------
Service部分
package com.mybatis.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.mybatis.mapper.RegionMapper;
import com.mybatis.model.Region;
@Service
public class RegionService {
@javax.annotation.Resource
private RegionMapper regionMapper; // 调用Mapper
public List<Region> ListAll(){
return regionMapper.getAll(); //调用方法
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
Mapper方法里用到的sql语句
<mapper>
<resultMap id="BaseResultMap" type="com.mybatis.model.Region" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="regionName" property="regionname" jdbcType="VARCHAR" />
<result column="tel" property="tel" jdbcType="INTEGER" />
<result column="text" property="text" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, regionName, tel, text
</sql>
<select id="getAll" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_region
</select>
</mapper>