Browse Source

0123 mybatis注解开发

Qing 1 year ago
parent
commit
64d57aa928

+ 2 - 0
springmvc-demo/src/main/java/com/sf/WebInitConfig.java

@@ -36,7 +36,9 @@ public class WebInitConfig
         CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
         characterEncodingFilter.setEncoding("utf-8");
         characterEncodingFilter.setForceRequestEncoding(true);
+        // 配置HiddenHttpMethodFilter
         HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
         return new Filter[]{characterEncodingFilter, hiddenHttpMethodFilter};
+//        return new Filter[]{characterEncodingFilter};
     }
 }

+ 73 - 0
springmvc-demo/src/main/java/com/sf/controller/RoleRestController.java

@@ -0,0 +1,73 @@
+package com.sf.controller;
+
+import com.sf.entity.Role;
+import com.sf.service.RoleService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+@Controller
+@RequestMapping("/roleRest")
+public class RoleRestController {
+
+    @Autowired
+    private RoleService roleService;
+
+    // localhost:8080/springmvc_demo/roleRest/list
+    @GetMapping("/list")
+    public String list(Model model) {
+        List<Role> roles = roleService.queryRoles();
+        model.addAttribute("roleList", roles);
+        // WEB-INF/templates/rest/list.html
+        return "rest/list";
+    }
+
+    // localhost:8080/springmvc_demo/roleRest/toAdd
+    @GetMapping("/toAdd")
+    public String toAdd() {
+        // WEB-INF/templates/rest/add.html
+        return "rest/add";
+    }
+
+    // localhost:8080/springmvc_demo/role/add
+    // localhost:8080/springmvc_demo/roleRest   post
+    @PostMapping()
+    public String add(Role role) {
+        roleService.add(role);
+        // localhost:8080/springmvc_demo/roleRest/list
+        return "redirect:/roleRest/list";
+    }
+
+    // localhost:8080/springmvc_demo/roleRest/toUpdate/1
+    // @PathVariable 和 @RequestParam
+    @GetMapping("/toUpdate/{id}")
+    public String toUpdate(@PathVariable("id") int id, Model model) {
+        Role role = roleService.queryRoleById(id);
+        model.addAttribute("role", role);
+        // WEB-INF/templates/rest/update.html
+        return "rest/update";
+    }
+
+    // localhost:8080/springmvc_demo/role/update
+    // localhost:8080/springmvc_demo/roleRest   put
+    // <input type="hidden" name="_method" value="put">
+    @PutMapping()
+    public String update(Role role) {
+        System.out.println("role: " + role);
+        roleService.update(role);
+        // localhost:8080/springmvc_demo/roleRest/list
+        return "redirect:/roleRest/list";
+    }
+
+    // localhost:8080/springmvc_demo/role/delete
+    // localhost:8080/springmvc_demo/roleRest/1   delete
+    // <input type="hidden" name="_method" value="put">
+    @DeleteMapping("/{id}")
+    public String delete(@PathVariable("id") int id) {
+        roleService.delete(id);
+        return "redirect:/roleRest/list";
+    }
+}

+ 10 - 0
springmvc-demo/src/main/java/com/sf/dao/AuthorDao.java

@@ -1,11 +1,21 @@
 package com.sf.dao;
 
 import com.sf.entity.Author;
+import org.apache.ibatis.annotations.Result;
+import org.apache.ibatis.annotations.Results;
+import org.apache.ibatis.annotations.Select;
 
 import java.util.List;
 
 public interface AuthorDao {
 
+    @Select("select * from author")
+    @Results(id = "authorMap", value = {
+            @Result(id = true, column = "id", property = "id"),
+            @Result(column = "author_id", property = "authorId"),
+            @Result(column = "author_name", property = "authorName"),
+            @Result(column = "author_desc", property = "authorDesc")
+    })
     List<Author> findAll();
 //
 //    int insert(Author author);

+ 12 - 3
springmvc-demo/src/main/resources/mapper/authorMapper.xml

@@ -3,9 +3,18 @@
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
 <mapper namespace="com.sf.dao.AuthorDao">
+
+<!--    <resultMap id="roleResultMap" type="com.sf.entity.Author">-->
+<!--        <id property="id" column="id" />-->
+<!--        <result property="authorId" column="author_id"/>-->
+<!--        <result property="authorName" column="author_name"/>-->
+<!--        <result property="authorDesc" column="author_desc"/>-->
+<!--    </resultMap>-->
     <!-- 查询 -->
-    <select id="findAll" resultType="com.sf.entity.Author">
-        select * from author
-    </select>
+<!--    <select id="findAll" resultType="com.sf.entity.Author">-->
+<!--    <select id="findAll" resultMap="roleResultMap">-->
+<!--        select *-->
+<!--        from author-->
+<!--    </select>-->
 
 </mapper>

+ 6 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/hello.html

@@ -12,7 +12,13 @@ Hello,
 <br>
 <a th:href="@{/testList}">testList</a><br>
 <a th:href="@{/role/list}">roleList</a><br>
+<a th:href="@{/roleRest/list}">roleRestList</a><br>
 <br>
 <a th:href="@{/localeChange}">localeChange</a><br>
+
+<br>
+<a th:href="@{/author}">author</a><br>
+<a th:href="@{/author/list}">authorList</a><br>
+
 </body>
 </html>

+ 36 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/rest/add.html

@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<html lang="en" xmlns:th="http:www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>添加角色</title>
+    <link rel="stylesheet" th:href="@{/webjars/bootstrap/5.3.2/css/bootstrap.css}">
+</head>
+<body class="container">
+<h3>添加角色</h3>
+<br>
+<!-- /user /role /auth -->
+<form th:action="@{/roleRest}" method="post" class="form-horizontal">
+    <div class="form-group">
+        <label class="col-sm-2 control-label">名字:</label>
+        <div class="col-sm-5">
+            <input type="text" id="name" name="name" class="form-control">
+        </div>
+    </div>
+
+    <div class="form-group">
+        <label class="col-sm-2 control-label">描述:</label>
+        <div class="col-sm-5">
+            <input type="text" id="desc" name="desc" class="form-control">
+        </div>
+    </div>
+
+    <div class="form-group">
+        <div class="col-sm-offset-2 col-sm-10">
+            <input type="submit" value="提交" class="btn btn-info">
+        </div>
+    </div>
+</form>
+
+
+</body>
+</html>

+ 57 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/rest/list.html

@@ -0,0 +1,57 @@
+<!DOCTYPE html>
+<html lang="en" xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>角色列表</title>
+    <link rel="stylesheet" th:href="@{/webjars/bootstrap/5.3.2/css/bootstrap.css}">
+    <!--引入jquery-->
+    <script type="text/javascript" th:src="@{/webjars/jquery/3.7.1/jquery.js}"></script>
+</head>
+<body class="container">
+
+<h2>角色如下</h2>
+<br>
+
+<table class="table table-hover">
+    <thead>
+    <tr>
+        <th>名字</th>
+        <th>描述</th>
+    </tr>
+    </thead>
+    <tbody>
+    <tr th:each="role:${roleList}">
+        <td th:text="${role.name}">name</td>
+        <td th:text="${role.desc}">role</td>
+        <td class="col-sm-2">
+            <a th:href="@{/roleRest/toUpdate/}+${role.id}">编辑</a>
+        </td>
+        <td class="col-sm-2">
+            <button th:attr="del_url=@{/roleRest/}+${role.id}" name="del_button">删除</button>
+        </td>
+    </tr>
+    </tbody>
+</table>
+
+<div class="form-group">
+    <div class="col-sm-2 control-label">
+        <a href="/roleRest/toAdd" th:href="@{/roleRest/toAdd}"
+           class="btn btn-info">添加</a>
+    </div>
+</div>
+
+<!--删除按钮借助的表单 通过jquery的点击监听功能 动态获取表单的请求路径(由按钮本身的属性值拼接)-->
+<form method="post" id="del_form">
+    <input type="hidden" name="_method" value="delete">
+</form>
+
+<script>
+    $(function () {
+        $("button[name='del_button']").click(function () {
+            $("#del_form").prop("action", $(this).attr("del_url")).submit();
+        });
+    });
+</script>
+
+</body>
+</html>

+ 45 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/rest/update.html

@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<html lang="en" xmlns:th="http:www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>修改角色</title>
+    <link rel="stylesheet" th:href="@{/webjars/bootstrap/5.3.2/css/bootstrap.css}">
+</head>
+<body class="container">
+<h3>修改角色</h3>
+<br>
+
+<form th:action="@{/roleRest}" th:object="${role}" method="post" class="form-horizontal">
+
+    <input type="hidden" name="_method" value="put">
+
+    <div class="form-group">
+        <label class="col-sm-2 control-label">名字:</label>
+        <div class="col-sm-5">
+            <input type="text" id="name" name="name"
+                   th:value="*{name}" class="form-control" readonly>
+        </div>
+    </div>
+
+    <div class="form-group">
+        <label class="col-sm-2 control-label">角色:</label>
+        <div class="col-sm-5">
+            <input type="text" id="desc" name="desc"
+                   th:value="*{desc}" class="form-control">
+        </div>
+    </div>
+
+    <div>
+        <input type="hidden" id="id" name="id" th:value="*{id}">
+    </div>
+
+    <div class="form-group">
+        <div class="col-sm-offset-2 col-sm-10">
+            <input type="submit" value="提交" class="btn btn-info">
+        </div>
+    </div>
+</form>
+
+
+</body>
+</html>

+ 11 - 0
springmvc-demo/src/main/webapp/WEB-INF/web.xml

@@ -71,4 +71,15 @@
 <!--        <url-pattern>/*</url-pattern>-->
 <!--    </filter-mapping>-->
 
+        <!--  配置HiddenHttpMethodFilter  -->
+<!--<filter>-->
+<!--<filter-name>hiddenHttpMethodFilter</filter-name>-->
+<!--<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>-->
+<!--</filter>-->
+<!--<filter-mapping>-->
+<!--<filter-name>hiddenHttpMethodFilter</filter-name>-->
+<!--<url-pattern>/*</url-pattern>-->
+<!--</filter-mapping>-->
+
+
 <!--</web-app>-->