application.yml配置文件
CustomerMapper.xml两表查询
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cjb.customers01.mapper.CustomerMapper"> <resultMap id="findCustomer" type="com.cjb.customers01.pojo.Customer"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="remark" column="remark"/> <result property="phone" column="phone"/> <result property="address" column="address"/> <result property="typeId" column="typeId"/> <association property="types" javaType="com.cjb.customers01.pojo.Types"> <id property="id" column="id"/> <result property="typeName" column="typeName"/> </association> </resultMap> <select id="findAll" resultMap="findCustomer"> select * from customer c,types t where c.typeId = t.id </select> <insert id="save" parameterType="customer"> INSERT INTO customer(name,remark,phone,address,typeId) VALUES (#{name},#{remark},#{phone},#{address},#{typeId}) </insert> <delete id="del" parameterType="int"> delete from customer where id = #{id} </delete> <update id="update" parameterType="Customer"> UPDATE customer <set> <if test="name!=null"> name = #{name}, </if> <if test="remark!=null"> remark = #{remark}, </if> <if test="phone!=null"> phone = #{phone}, </if> <if test="address!=null"> address = #{address}, </if> <if test="typeId!=null"> typeId = #{typeId}, </if> </set> where id = #{id} </update> <select id="findById" resultType="Customer"> select * from customer where id = #{id} </select> <select id="find" resultType="types"> select * from types </select> </mapper>
实体类
接口定义:
@Mapper public interface CustomerMapper { /** * 查询全部 * @return */ List<Customer> findAll(); /** * 根据id删除 * @param id * @return */ int del(int id); /** * 新增信息 * @param customer * @return */ int save(Customer customer); /** * 修改信息 * @param customer * @return */ int update(Customer customer); /** * 根据id查询 * @param id * @return */ Customer findById(int id); /** * * 小标查询全部 * @return */ List<Types> find(); }
controller类:
@Controller public class CustomerController { @Autowired private CustomerService customerService; @RequestMapping("/{page}.html") public String topage(@Param("page") String page){ return page; } @RequestMapping("findAll") public String findAll(Model model){ List<Customer> list = customerService.findAll(); model.addAttribute("list",list); return "list"; } @RequestMapping("save") public String save(Customer customer){ int count = customerService.save(customer); if (count>0){ return "redirect:/findAll"; }else{ return "404"; } } @RequestMapping("add") public String add(){ return "add"; } @RequestMapping("update") public String update(){ return "update"; } @RequestMapping("findById") public String findById(int id,Model model){ Customer byId = customerService.findById(id); model.addAttribute("byId",byId); return "update"; } @RequestMapping("findDel") public String indDel(int id){ int count = customerService.del(id); if (count>0){ return "redirect:/findAll"; }else{ return "404"; } } @RequestMapping("findUpdate") public String findUpdate(Customer customer){ int count = customerService.update(customer); if (count>0){ return "redirect:/findAll"; }else{ return "404"; } } }
查询页面 list.html
<!DOCTYPE html> <!--suppress ALL--> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>查询客户信息</title> </head> <body> <table align="center" border="1"> <tr> <td>名称</td> <td>备注</td> <td>手机号码</td> <td>地址</td> <td>类型名称</td> <td>操作</td> </tr> <tr th:each="cus:${list}"> <td th:text="${cus.name}"></td> <td th:text="${cus.remark}"></td> <td th:text="${cus.phone}"></td> <td th:text="${cus.address}"></td> <td th:text="${cus.types.typeName}"></td> <td> <a th:href="@{'findDel?id='+${cus.id}}">删除</a> <a th:href="@{'findById?id='+${cus.id}}">修改</a> </td> </tr> </table> </body> </html>
新增页面 add.html
<!DOCTYPE html> <!--suppress ALL--> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>新增客户信息</title> </head> <body> <form th:action="@{save}" method="post"> <table border="1" align="center"> <tr> <td> 名称:<input type="text" name="name"> </td> </tr> <tr> <td> 备注:<input type="text" name="remark"> </td> </tr> <tr> <td> 手机:<input type="text" name="phone"> </td> </tr> <tr> <td> 住址:<input type="text" name="address"> </td> </tr> <tr> <td> 类型id:<input type="text" name="typeId"> </td> </tr> <tr align="center"> <td> <input type="submit" value="提交"> </td> </tr> </table> </form> </body> </html>
修改页面 update.html
<!DOCTYPE html> <!--suppress ALL--> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>修改客户信息</title> </head> <body> <form th:action="@{findUpdate}" method="post"> <table border="1" align="center"> <input type="text" name="id" th:value="${byId.id}" hidden> <tr> <td> 名称:<input type="text" name="name" th:value="${byId.name}"> </td> </tr> <tr> <td> 备注:<input type="text" name="remark" th:value="${byId.remark}"> </td> </tr> <tr> <td> 手机:<input type="text" name="phone" th:value="${byId.phone}"> </td> </tr> <tr> <td> 住址:<input type="text" name="address" th:value="${byId.address}"> </td> </tr> <tr> <td> 类型id:<input type="text" name="typeId" th:value="${byId.typeId}"> </td> </tr> <tr align="center"> <td> <input type="submit" value="修改"> </td> </tr> </table> </form> </body> </html>