• 4.6 基于Spring-Boot的Mysql+jpa的增删改查学习记录 > 我的程序猿之路:第三十六章


     

     

    1.项目结构

          -JDK  1.8

          -SpringBoot  2.0.6

          -Thymeleaf

          -Hot Start

        

       1.1 后台(5个)

          javacomexampledemoeansuser.java                               (创建实体)

          javacomexampledemo epositoryuserRepository.java        (数据持久)

          javacomexampledemouserServiceuserService.java          (业务接口)

          javacomexampledemouserServiceuserServiceImpl.java    (接口实现)

          javacomexampledemowebHeController.java                      (表现)

      1.2  web(4个)

         resources emplatesadd.html                                                  (增加页面)

        
         以及   配置文件  目录(代码在最后面)

         resources emplatesedit.html                                                  (修改页面)

         resources emplatesindex.html                                               (首页页面)

     

     

         resources emplateslogin.html                                                (列表页面)

           数据库

        2  .代码

       1.user.java

     1 package com.example.demo.beans;
     2 
     3 import org.apache.catalina.User;
     4 
     5 import javax.persistence.Entity;
     6 import javax.persistence.GeneratedValue;
     7 import javax.persistence.GenerationType;
     8 import javax.persistence.Id;
     9 import java.io.Serializable;
    10 
    11 @Entity
    12 public class user implements Serializable {
    13 
    14     @Id
    15     @GeneratedValue
    16     private int id;
    17    private String name;
    18    private String falg;
    19 
    20     public int getId() {
    21         return id;
    22     }
    23 
    24     public void setId(int id) {
    25         this.id = id;
    26     }
    27 
    28     public String getName() {
    29         return name;
    30     }
    31 
    32     public void setName(String name) {
    33         this.name = name;
    34     }
    35 
    36     public String getFalg() {
    37         return falg;
    38     }
    39 
    40     public void setFalg(String falg) {
    41         this.falg = falg;
    42     }
    43     public user(){
    44 
    45     }
    46     public user(String name,String falg){
    47         super();
    48         this.name=name;
    49         this.falg=falg;
    50     }
    51 }

    2.userRepository.java

     1 package com.example.demo.repository;
     2 import com.example.demo.beans.user;
     3 import org.springframework.data.jpa.repository.JpaRepository;
     4 import org.springframework.data.jpa.repository.Modifying;
     5 import org.springframework.data.jpa.repository.Query;
     6 import org.springframework.transaction.annotation.Transactional;
     7 
     8 import java.util.List;
     9 
    10 public interface userRepository extends JpaRepository<user, Long> {
    11 
    12       public user findAllBy();
    13       public List<user> findAllById(Integer id);
    14        @Transactional
    15        @Modifying
    16        @Query("delete from user where id = ?1")
    17        public void deleteByCustomerId(Integer id);
    18 
    19 }

    3.userService.java

     1 package com.example.demo.userService;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import com.example.demo.beans.user;
     5 import java.util.List;
     6 
     7 public interface userService {
     8 
     9     public List<user> getUserList();
    10     public List<user> findUserById(Integer id);
    11     public void edit(user user);
    12     public void del(Integer id);
    13     public void saveUser(user user);
    14 
    15 
    16 }

    4.userServiceImpl.java

     1 package com.example.demo.userService;
     2 
     3 import com.example.demo.repository.userRepository;
     4 import com.example.demo.beans.user;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Service;
     7 import com.example.demo.userService.userService;
     8 import java.util.List;
     9 @Service
    10 public class userServiceImpl implements userService {
    11     @Autowired
    12     private userRepository ur;
    13     @Override
    14     public List<user> getUserList() {
    15         return ur.findAll();
    16     }
    17     public List<user> findUserById(Integer id){
    18      //   long iss= (int)id;
    19         return ur.findAllById(id);
    20     }
    21     public void edit(user user){
    22         ur.save(user);
    23     }
    24     public void del(Integer id){
    25         System.out.println("This is userServiceImpl");
    26         ur.deleteByCustomerId(id);
    27     }
    28     public void saveUser(user user){
    29         ur.save(user);
    30 
    31     }
    32 }

    5.HeController.java

     1 package com.example.demo.web;
     2 import org.springframework.stereotype.Controller;
     3 import org.springframework.ui.Model;
     4 import org.springframework.web.bind.annotation.PathVariable;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import com.example.demo.userService.userService;
     7 import com.example.demo.beans.user;
     8 import org.springframework.web.bind.annotation.RequestParam;
     9 
    10 import javax.annotation.Resource;
    11 import javax.servlet.http.HttpServletRequest;
    12 import java.util.List;
    13 
    14 @Controller
    15 @RequestMapping("index.do")
    16 public class HeController {
    17 
    18    @Resource
    19     private userService userService;
    20 
    21     @RequestMapping("/index")
    22     public String aaa(){
    23         return "index";
    24     }
    25     @RequestMapping("info/login")
    26     public String bbb(HttpServletRequest r,Model model){
    27         String name_ = r.getParameter("name");
    28         String falg_ = r.getParameter("falg");
    29          List<user> users =userService.getUserList();
    30          for(user u:users){
    31              System.out.println(u.getName());
    32              System.out.println(u.getFalg());
    33          }
    34          model.addAttribute("users",users);
    35         return "login";
    36     }
    37     @RequestMapping("/toEdit/{id}")
    38     public String edit(Model model,@PathVariable Integer id){
    39         System.out.println("__"+id);
    40        List<user> u = userService.findUserById(id);
    41        user uu = new user();
    42        String a ="";
    43        String b ="";
    44        for(int i=0;i<u.size();i++){
    45            a = u.get(i).getName();
    46            b =  u.get(i).getFalg();
    47        }
    48        uu.setId(id);
    49        uu.setName(a);
    50        uu.setFalg(b);
    51        model.addAttribute("user",uu);
    52         return "edit";
    53     }
    54     @RequestMapping("/info/saveEd")
    55     public String saveEd(HttpServletRequest request){
    56         String id = request.getParameter("id");
    57         String name = request.getParameter("name");
    58         String falg = request.getParameter("falg");
    59 
    60         System.out.println("saveEd"+name+"--"+falg);
    61         user u = new user();
    62         u.setId(Integer.parseInt(id));
    63         u.setName(name);
    64         u.setFalg(falg);
    65         userService.edit(u);
    66         return "redirect:/index.do/info/login";
    67     }
    68     @RequestMapping("/toDel/{id}")
    69     public String del(@PathVariable("id") Integer id){
    70         System.out.println(id+"--id");
    71         userService.del(id);
    72         return "redirect:/index.do/info/login";
    73     }
    74     @RequestMapping("info/insert")
    75     public String insert(){
    76         System.out.println("insert");
    77         return "add";
    78     }
    79     @RequestMapping("/toAdd")
    80     public String add(HttpServletRequest request){
    81         System.out.println("--add");
    82         String name = request.getParameter("name");
    83         String falg = request.getParameter("falg");
    84         user u = new user();
    85         u.setName(name);
    86         u.setFalg(falg);
    87         userService.saveUser(u);
    88         return "redirect:/index.do/info/login";
    89     }
    90 }

    6.add.html

     1 <!DOCTYPE html>
     2 <html xmlns:th="http://www.thymeleaf.org">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Spring-boot-add</title>
     6 </head>
     7 <body>
     8 <form action="/index.do/toAdd" method="post">
     9     <table>
    10         <tr>
    11             <td>name:<input type="text"  name="name"></td>
    12         </tr>
    13         <tr>
    14             <td>falg:<input type="text" name="falg"></td>
    15         </tr>
    16         <tr>
    17             <td><input type="submit" value="submit"></td>
    18         </tr>
    19     </table>
    20 </form>
    21 </body>
    22 </html>

    7.edit.html

     1 <!DOCTYPE html>
     2 <html xmlns:th="http://www.thymeleaf.org">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Spring-boot-edit</title>
     6 </head>
     7 <body>
     8 <form th:action="@{/index.do/info/saveEd}" method="post">
     9     <table>
    10         <tr >
    11 
    12             <td><input type="text" th:value="${user.name }" th:name="name"><input type="hidden" th:value="${user.id}" th:name="id"> </td>
    13         </tr>
    14         <tr>
    15             <td><input type="text" th:value="${user.falg }" th:name="falg"></td>
    16         </tr>
    17         <tr>
    18             <td><input  value="修改" type="submit"/></td>
    19         </tr>
    20     </table>
    21 
    22 </form>
    23 </body>
    24 </html>

    8.index.html

     1 <!DOCTYPE html>
     2 <html xmlns:th="http://www.thymeleaf.org">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Spring-Boot-Web-index</title>
     6 </head>
     7 <body>
     8 <a href="/index.do/info/login">进入</a>
     9 </body>
    10 </html>

    9.login.html

     1 <!DOCTYPE html>
     2 <html xmlns:th="http://www.thymeleaf.org">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Spring-boot-login</title>
     6     <link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
     7 </head>
     8 <script>
     9 
    10 </script>
    11 <body>
    12 <div class="with:80%">
    13     <table class="table table-hover">
    14         <tr><td><a href="/index.do/info/insert" >add</a></td></tr>
    15         <tr>
    16             <th>id</th>
    17             <th>User Name</th>
    18             <th>Password</th>
    19             <th>edit</th>
    20             <th>delete</th>
    21         </tr>
    22 
    23         <tbody>
    24         <tr th:each="u : ${users}">
    25              <td th:text="${u.id}"></td>
    26             <td th:text="${u.name }"></td>
    27             <td th:text="${u.falg }">u.falg</td>
    28            <td><a th:href="@{'/index.do/toEdit/'+${u.id}}">edit</a></td>
    29             <td><a th:href="@{'/index.do/toDel/'+${u.id}}">delete</a></td>
    30         </tr>
    31         </tbody>
    32     </table>
    33 </div>
    34 </body>
    35 </html>

    10.pom.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5 
     6     <groupId>com.example</groupId>
     7     <artifactId>demo</artifactId>
     8     <version>0.0.1-SNAPSHOT</version>
     9     <packaging>jar</packaging>
    10 
    11     <name>demo</name>
    12     <description>Demo project for Spring Boot</description>
    13 
    14     <parent>
    15         <groupId>org.springframework.boot</groupId>
    16         <artifactId>spring-boot-starter-parent</artifactId>
    17         <version>2.0.6.RELEASE</version>
    18         <relativePath/> <!-- lookup parent from repository -->
    19     </parent>
    20 
    21     <properties>
    22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    24         <java.version>1.8</java.version>
    25         <!-- set thymeleaf version -->
    26         <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
    27         <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
    28     </properties>
    29 
    30 
    31     <dependencies>
    32         <dependency>
    33             <groupId>org.springframework.boot</groupId>
    34             <artifactId>spring-boot-starter</artifactId>
    35         </dependency>
    36       <!--  添加支持web的模块 -->
    37         <dependency>
    38             <groupId>org.springframework.boot</groupId>
    39             <artifactId>spring-boot-starter-web</artifactId>
    40         </dependency>
    41         <!--spring boot tomcat(默认可以不用配置,但当需要把当前web应用布置到外部servlet容器时就需要配置,并将scope配置为provided)-->
    42         <dependency>
    43             <groupId>org.springframework.boot</groupId>
    44             <artifactId>spring-boot-starter-tomcat</artifactId>
    45             <scope>provided</scope>
    46         </dependency>
    47         <!--  pom.xml文件中默认有两个模块: -->
    48         <!--  spring-boot-starter:核心模块,包括自动配置支持、日志和YAML; -->
    49         <!--  spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito。 -->
    50         <dependency>
    51             <groupId>org.springframework.boot</groupId>
    52             <artifactId>spring-boot-starter-test</artifactId>
    53             <scope>test</scope>
    54         </dependency>
    55         <!--   SpringBoot中使用Thymeleaf模板   -->
    56         <dependency>
    57             <groupId>org.springframework.boot</groupId>
    58             <artifactId>spring-boot-starter-thymeleaf</artifactId>
    59         </dependency>
    60         <!--   启动项目热部署  -->
    61         <dependency>
    62             <groupId>org.springframework.boot</groupId>
    63             <artifactId>spring-boot-devtools</artifactId>
    64             <optional>true</optional>
    65         </dependency>
    66         <!-- Hibername 的 spring data jpa -->
    67         <dependency>
    68             <groupId>org.springframework.boot</groupId>
    69             <artifactId>spring-boot-starter-data-jpa</artifactId>
    70         </dependency>
    71         <!-- mysql数据库 -->
    72         <dependency>
    73             <groupId>mysql</groupId>
    74             <artifactId>mysql-connector-java</artifactId>
    75         </dependency>
    76     </dependencies>
    77 
    78     <build>
    79         <plugins>
    80             <plugin>
    81                 <groupId>org.springframework.boot</groupId>
    82                 <artifactId>spring-boot-maven-plugin</artifactId>
    83                 <configuration><fork>true</fork></configuration>
    84             </plugin>
    85         </plugins>
    86     </build>
    87 
    88 
    89 </project>

    11.application.properties

     1 #缓存关闭
     2 spring.thymeleaf.cache=false
     3 #识别templates/下的文件
     4 spring.thymeleaf.prefix=classpath:/templates/
     5 # 热部署开关,false即不启用热部署
     6 spring.devtools.restart.enabled: true
     7 #   Mysql 数据库配置   bigen
     8 spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
     9 spring.datasource.username=root
    10 spring.datasource.password=root
    11 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    12 #  Mysql 数据库配置   end
    13 #  hibernate 的jpa  bigen
    14 spring.jpa.properties.hibernate.hbm2ddl.auto=update
    15 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
    16 spring.jpa.show-sql= true
    17 #  hibernate 的jpa  end

                                                                               

                                                                                                                 能力有限,有许多不足,以上内容仅供参考  

      参考大神网站(纯洁的微笑):http://www.ityouknow.com/springboot/2016/08/20/spring-boo-jpa.html

                                                       http://www.ityouknow.com/springboot/2016/02/03/spring-boot-web.html

                                                       http://www.ityouknow.com/springboot/2016/05/01/spring-boot-thymeleaf.html

  • 相关阅读:
    我的2020书单
    记录一次克隆硬盘的经历
    npm简单介绍
    批量处理Excel从格式xls到xlsx
    SQL经典实例笔记
    在字符串中找出连续最长的数字串
    将数字转化成字符串
    寄存器位读写,结构体位域定义,位域操作,位操作
    android C/C++ source files 全局宏定义 .
    android2.3 -添加自定义按键:作唤醒功能 .
  • 原文地址:https://www.cnblogs.com/fanyuyi-boke/p/qiao_duo_shao_nian_dai_ma_neng_ba_shou_zhi_mo_ping36.html
Copyright © 2020-2023  润新知