1、实体类
public class Provider {
private Integer pid;
//供应商编码
private String providerCode;
//供应商名称
private String providerName;
//联系人
private String people;
//联系电话
private String phone;
//联系地址
private String address;
//传真
private String fax;
//描述
private String describe;
// 创建时间
private Date createDate;
public Provider() {
}
//get set 略
}
2、mapper接口
@Mapper//指定这是操作数据的mapper
public interface ProviderMapper {
List<Provider> getProviders(Provider provider);
Provider getProviderById(Integer id);
}
3、mapper.xml
<select id="getProviders" resultType="cn.qqqking.springboot.entities.Provider">
select * from provider where 1=1
<if test="providerName != null and providerName != ''">
<!--${} 用于字符串拼接-->
and providerName like '%${providerName}%'
</if>
</select>
<select id="getProviderById" resultType="cn.qqqking.springboot.entities.Provider">
select * from provider where pid = #{pid}
</select>
4、controlle层(1 进入添加页面 2 添加操作)
Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
ProviderDao providerDao;
@Autowired
ProviderMapper providerMapper;
// @RequestMapping(value = "/provide",method = RequestMethod.GET)
@GetMapping("/providers")
public String list(Map<String,Object> map, Provider provider){
logger.info("供应商列表查询:"+ provider);
List<Provider> providers = providerMapper.getProviders(provider);
map.put("providers",providers);
map.put("providerName",provider.getProviderName());
return "provider/list";
}
/**
* type = null 进去查看详情页面
* type = update 则进去update.html
* @param pid 供应商id
* @param type
* @param map
* @return
*/
@GetMapping("/provider/{pid}")
public String view(@PathVariable("pid") Integer pid,
@RequestParam(value = "type",defaultValue = "view") String type,
Map <String , Object> map){
logger.info("查询"+ pid + "的供应商详细信息");
Provider provider = providerMapper.getProviderById(pid);
map.put("provider",provider);
//return "provider/view";
//type 为 null 时 为默认值view => return "provider/view";
return "provider/" + type;
}
5、前端
页面
<tr th:each=" p : ${providers}">
<td th:text="${p.pid}">PRO-CODE—001</td>
<td th:text="${p.providerName}">测试供应商001</td>
<td th:text="${p.people}">韩露</td>
<td th:text="${p.phone}">15918230478</td>
<td th:text="${p.fax}">15918230478</td>
<td th:text="${#dates.format(p.createDate,'yyyy-MM-dd')}"> 2015-11-12</td>
<td>
<a href="view.html" th:href="@{/provider/} + ${p.pid}"><img th:href="@{/img/read.png}" src="../img/read.png" alt="查看" title="查看"/></a>
<a href="update.html" th:href="@{/provider/} + ${p.pid} + '?type=update'"><img th:href="@{/img/xiugai.png}" src="../img/xiugai.png" alt="修改" title="修改"/></a>
<a href="#" th:attr="del_uri=@{/provider/}+${p.pid}" class="delete" ><img th:href="@{/img/schu.png}" src="../img/schu.png" alt="删除" title="删除"/></a>
</td>
</tr>
通过名字模糊查询
<form id="searchForm" th:action="@{/providers}" th:method="post">
<div class="search">
<span>供应商名称:</span>
<input type="text" th:value="${providerName}" name="providerName" placeholder="请输入供应商的名称"/>
<input type="button" onclick="$('#searchForm').submit()" value="查询"/>
<a href="add.html" th:href="@{/provider}">添加供应商</a>
</div>
</form>