<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      此次目標為搭建一個簡單的基于springboot的ssm(spring + springmvc + mybatis)的maven項目,使用的數據庫為mysql。

      最終項目目錄結構

       

       

      創建過程

      1、項目創建步驟如下:

      為了創建快速。我們在idea中通過插件創建一個spring boot項目。

        

         

         

         

        

      到此,我們的初始項目創建完成了。項目結構如下圖所示

        

      此時運行 SsmspringbootApplication 這個文件,會報未配置dataSource

        

      如下圖所示進行一下配置(替換配置文件類型:.properties換為.yml然后增加datasource相關配置)即可完成一個最簡單的springboot項目進行跑通 

         

      # 數據庫連接配置信息
      spring:
        datasource:
          username: root
          password: root
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://localhost:3306/mysql_test?serverTimezone=UTC

      賬號密碼對應要配置成你自己的,到此項目可以跑起來了。最簡單的springboot項目搭建完成了,生成的pom.xml文件的內容如下,接下來我們開始加入我們的ssm。

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>2.3.3.RELEASE</version>
              <relativePath/> <!-- lookup parent from repository -->
          </parent>
          <groupId>cn.wj.example</groupId>
          <artifactId>ssmspringboot</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <name>ssmspringboot</name>
          <description>Demo project for Spring Boot</description>
      
          <properties>
              <java.version>1.8</java.version>
          </properties>
      
          <dependencies>
              <!--begin spring-->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-jdbc</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.mybatis.spring.boot</groupId>
                  <artifactId>mybatis-spring-boot-starter</artifactId>
                  <version>2.1.3</version>
              </dependency>
      
              <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <scope>runtime</scope>
              </dependency>
              <dependency>
                  <groupId>org.projectlombok</groupId>
                  <artifactId>lombok</artifactId>
                  <optional>true</optional>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-test</artifactId>
                  <scope>test</scope>
                  <exclusions>
                      <exclusion>
                          <groupId>org.junit.vintage</groupId>
                          <artifactId>junit-vintage-engine</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
              <!--end spring-->
          </dependencies>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
      
      </project>

       

      2、整合:基于開發的最基本的三層架架構進行開發

      需求:從數據庫中查詢出某一用戶的所有信息返回給前臺頁面

        
        先康康數據庫的表結構~

      CREATE TABLE `t_user` (
        `user_id` int(11) NOT NULL AUTO_INCREMENT,
        `user_name` char(20) NOT NULL,
        `user_sex` char(2) NOT NULL DEFAULT 'F',
        `user_address` char(20) NOT NULL,
        PRIMARY KEY (`user_id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8


        為了方便我這里通過mybatis-generator自動生成mapper dao model,詳情可參考SpringBoot之使用mybatis generator自動生成bean、mapper、mapper xml,自動生成后的項目結構如下:
        

         接下來手動加下service層跟controller層的實現,項目結構如下:
        

         service層

      package cn.wj.example.ssmspringboot.service;
      
      import cn.wj.example.ssmspringboot.pojo.User;
      
      /**
       * UserService
       * @author v_jingwen
       *
       */
      public interface UserService {
      
          /**
           * 新增用戶信息
           * @param user
           * @return
           */
          public int addUser(User user);
      
          /**
           * 根據ID查詢用戶信息
           * @param userId
           * @return
           */
          public User selectUserById(Integer userId);
      }
      package cn.wj.example.ssmspringboot.service.impl;
      
      import cn.wj.example.ssmspringboot.dao.UserMapper;
      import cn.wj.example.ssmspringboot.pojo.User;
      import cn.wj.example.ssmspringboot.service.UserService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      
      @Service
      public class UserServiceImpl implements UserService {
      
          @Autowired
          private UserMapper userMapper;
      
          @Override
          public int addUser(User user) {
              return userMapper.insert(user);
          }
      
          @Override
          public User selectUserById(Integer userId) {
              return userMapper.selectByPrimaryKey(userId);
          }
      }

        controller層

      package cn.wj.example.ssmspringboot.controller;
      
      import cn.wj.example.ssmspringboot.pojo.User;
      import cn.wj.example.ssmspringboot.service.UserService;
      import java.util.HashMap;
      import java.util.Map;
      
      import javax.servlet.http.HttpServletRequest;
      
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      /**  
       * 用戶 Controller
       *  
       * @author v_jingwen 
       * @date 2020年9月9日 下午15:18:08
       * 
       */
      @RestController
      @RequestMapping(value = "/ssmTest/user", produces = "application/json;charset=UTF-8")
      public class UserController {
          
          private final static Logger logger = LoggerFactory.getLogger(UserController.class);
      
          @Autowired
          private UserService userService;
      
          /***
           * 增加測試用戶
           * @param request
           */
          @RequestMapping(value="/addUser.do")
          public Map<String,Object> addUser(HttpServletRequest request){
              Map<String,Object> resultMap = new HashMap<>();
              try {
                  User user = new User();
                  user.setUserName("Nancy橙");
                  user.setUserSex("F");// F=女性,M=男性,
                  user.setUserAddress("廣東深圳市南山區");
                  int id = userService.addUser(user);
                  resultMap.put("data",id);// 返回記錄主鍵id
                  resultMap.put("code","200");
                  resultMap.put("msg","操作成功");
              } catch (Exception e) {
                  logger.error("error", e);
                  resultMap.put("code","500");
                  resultMap.put("msg","操作失敗"+e.getMessage());
              }
              return resultMap;
          }
          
          /***
           * 根據ID查詢用戶信息
           * @param request
           */
          @RequestMapping(value="/getUserById.do")
          public Map<String,Object> getUserById(HttpServletRequest request){
              Map<String,Object> resultMap = new HashMap<>();
              try {
                  String userId = request.getParameter("userId");// 記錄主鍵id
                  User user = userService.selectUserById(Integer.parseInt(userId));
                  resultMap.put("data",user);
                  resultMap.put("code","200");
                  resultMap.put("msg","操作成功");
              } catch (NumberFormatException e) {
                  logger.error("error", e);
                  resultMap.put("code","500");
                  resultMap.put("msg","操作失敗,userId必須為數字");
              } catch (Exception e) {
                  logger.error("error", e);
                  resultMap.put("code","500");
                  resultMap.put("msg","操作失敗"+e.getMessage());
              }
              return resultMap;
          }
      }

       
       注冊mapper,主要作用是掃包,不進行注冊會導致dao層bean無法注入。
        例如:
          使用

          @Autowired
          private UserMapper userMapper;

          報錯如下:

                   Field userMapper in cn.wj.example.ssmspringboot.service.impl.UserServiceImpl required a bean of type 'cn.wj.example.ssmspringboot.dao.UserMapper' that could not be found.

                   The injection point has the following annotations:
                   - @org.springframework.beans.factory.annotation.Autowired(required=true)

        注冊mapper方式,本例中使用方法1:

        方法1、在springboot啟動類中加@MapperScan("cn.wj.example.ssmspringboot.dao")
        方法2、在對應dao層加上@Mapper
             

        
        在application.yml中指定mybatis配置文件的路徑,否則會報錯,例如:Invalid bound statement (not found): cn.wj.example.ssmspringboot.dao.UserMapper.selectByPrimaryKey。
        

      #指定mybatis配置文件的路徑
      mybatis:
       mapper-locations: classpath:mybatis/mapper/*.xml

       
        測試

        新增了默認用戶,返回對應的主鍵id1
        

       

         查詢主鍵id=1的用戶信息
        

       

         啦啦啦?(^?^*),完結~
         
        代碼:有需要的親可以參考,github - ssmspringboot項目
             參考資料:MyBatis-Spring-Boot-Starter簡要說明


        

      posted on 2020-09-08 15:25  半紙情書  閱讀(1314)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 精品婷婷色一区二区三区| 国产精品揄拍100视频| 亚洲精品一区二区妖精| 国内自产少妇自拍区免费| 亚洲中文字幕久久精品码| 亚洲激情一区二区三区在线| 老师扒下内裤让我爽了一夜| 亚洲男人的天堂久久香蕉| 欧美亚洲日本国产综合在线美利坚| 精品福利视频一区二区三区| 中文字幕亚洲精品第一页| 99久久夜色精品国产亚洲| 色欲狠狠躁天天躁无码中文字幕 | AV喷水高潮喷水在线观看COM| 欧美性受xxxx白人性爽| 国产毛片精品av一区二区| 激情综合网激情综合网激情| 中文字幕 欧美日韩| 国产99在线 | 免费| 久久亚洲精品亚洲人av| 十九岁的日本电影免费观看| 亚洲国产午夜福利精品| 四虎在线永久免费看精品| 日本另类αv欧美另类aⅴ| 赞皇县| 国产熟女精品一区二区三区| 亚洲精品国模一区二区| 欧美日韩国产码高清| 国产精品久久久久7777| 国产精品普通话国语对白露脸| √天堂资源地址在线官网| 亚洲国产精品久久久天堂麻豆宅男| 69精品无人区国产一区| 国产高清在线A免费视频观看| 人妻精品动漫H无码中字| 日韩区中文字幕在线观看| 97在线观看视频免费| 一区二区不卡国产精品| 欧美高清一区三区在线专区| 动漫AV纯肉无码AV电影网| 岳池县|