国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

SpringCloud(第 044 篇)鏈接Mysql數(shù)據(jù)庫簡單的集成Mybatis框架采用Mapp

smartlion / 1256人閱讀

摘要:添加用戶接口類實(shí)現(xiàn)類簡單用戶鏈接數(shù)據(jù)庫微服務(wù)通過注解標(biāo)注該類為持久化操作對象。添加一個(gè)使用接收請求添加微服務(wù)啟動(dòng)類鏈接數(shù)據(jù)庫簡單的集成框架采用訪問數(shù)據(jù)庫。

SpringCloud(第 044 篇)鏈接Mysql數(shù)據(jù)庫簡單的集成Mybatis框架采用MapperXml訪問數(shù)據(jù)庫

-

一、大致介紹
1、前面章節(jié)講解的是在方法上面添加sql語句操作,雖然說僅僅只是一種簡單的操作,在測試期間可以多試試;
2、但是對于復(fù)雜的操作,那種簡單也僅僅只是簡單的自己用用而已,復(fù)雜的還是得采用xml配置;
3、因此本章節(jié)就試試采用Mybatis框架通過mapperxml進(jìn)行對數(shù)據(jù)的操作;
二、實(shí)現(xiàn)步驟 2.1 添加 maven 引用包


    4.0.0

    springms-provider-user-mysql-mybatis-mapper
    1.0-SNAPSHOT
    jar
    
    
        com.springms.cloud
        springms-spring-cloud
        1.0-SNAPSHOT
    
    
    
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            mysql
            mysql-connector-java
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.1
        

        
        
            
            
        
    

2.2 添加應(yīng)用配置文件(springms-provider-user-mysql-mybatis-mappersrcmainresourcesapplication.yml)
server:
  port: 8330
spring:
  application:
    name: springms-provider-user-mysql-mybatis-mapper  #全部小寫


#####################################################################################################
# mysql 屬性配置
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://ip:3306/hmilyylimh
    username: username
    password: password
#  jpa:
#    hibernate:
#      #ddl-auto: create #ddl-auto:設(shè)為create表示每次都重新建表
#      ddl-auto: update #ddl-auto:設(shè)為update表示每次都不會(huì)重新建表
#    show-sql: true
#####################################################################################################

#####################################################################################################
# mybatis mapper xml 配置
mybatis:
  # mybatis.type-aliases-package:指定domain類的基包,即指定其在*Mapper.xml文件中可以使用簡名來代替全類名(看后邊的UserMapper.xml介紹)
  type-aliases-package:
  mapper-locations: classpath:mybatis/mapper/*.xml
  config-location: classpath:mybatis/mybatis-config.xml
#####################################################################################################

#####################################################################################################
# 打印日志
logging:
  level:
    root: INFO
    org.hibernate: INFO
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
    com.springms: DEBUG
#####################################################################################################
2.3 添加mybatis配置文件(springms-provider-user-mysql-mybatis-mapper/src/main/java/com/springms/cloud/entity/User.java)




    
        

        

        

        

        

        

        

        

        

        

        

        

    

    
        
        
        
        
        
        

        
    

2.4 添加用戶mapperxml映射文件(springms-provider-user-mysql-mybatis-mapper/src/main/java/com/springms/cloud/entity/User.java)





    
        
        
        
        
        
    


    
    
        
    

    

    
2.5 添加實(shí)體用戶類User(springms-provider-user-mysql-mybatis-mapper/src/main/java/com/springms/cloud/entity/User.java)
package com.springms.cloud.entity;

public class User {

  private Long id;

  private String username;

  private String name;

  private Integer age;

  private String balance;

  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getUsername() {
    return this.username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return this.age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  public String getBalance() {
    return this.balance;
  }

  public void setBalance(String balance) {
    this.balance = balance;
  }
}
2.6 添加用戶mapper接口(springms-provider-user-mysql-mybatis-mapper/src/main/java/com/springms/cloud/mapper/IUserMapper.java)
package com.springms.cloud.mapper;

import com.springms.cloud.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 用戶 mybatis 接口文件。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017-10-19
 *
 */
public interface IUserMapper {

    User findUserById(Long id);

    List findAllUsers();

    int insertUser(User user);
}
2.7 添加用戶DAO接口類(springms-provider-user-mysql-mybatis-mapper/src/main/java/com/springms/cloud/dao/IUserDao.java)
package com.springms.cloud.dao;

import com.springms.cloud.entity.User;

import java.util.List;

/**
 * 簡單用戶鏈接Mysql數(shù)據(jù)庫微服務(wù)(通過@Repository注解標(biāo)注該類為持久化操作對象)。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017-10-19
 *
 */
public interface IUserDao {

    User findUserById(Long id);

    List findAllUsers();

    int insertUser(User user);
}
2.8 添加用戶DAO接口類實(shí)現(xiàn)類(springms-provider-user-mysql-mybatis-mapper/src/main/java/com/springms/cloud/dao/impl/UserDaoImpl.java)
package com.springms.cloud.dao.impl;

import com.springms.cloud.dao.IUserDao;
import com.springms.cloud.entity.User;
import com.springms.cloud.mapper.IUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 簡單用戶鏈接Mysql數(shù)據(jù)庫微服務(wù)(通過@Repository注解標(biāo)注該類為持久化操作對象)。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017-10-19
 *
 */
@Repository
public class UserDaoImpl implements IUserDao {

    @Autowired
    private IUserMapper iUserMapper;

    @Override
    public User findUserById(Long id) {
        return iUserMapper.findUserById(id);
    }

    @Override
    public List findAllUsers() {
        return iUserMapper.findAllUsers();
    }

    @Override
    public int insertUser(User user) {
        return iUserMapper.insertUser(user);
    }
}
2.9 添加用戶Service接口類(springms-provider-user-mysql-mybatis-mapper/src/main/java/com/springms/cloud/service/IUserService.java)
package com.springms.cloud.service;

import com.springms.cloud.entity.User;

import java.util.List;

/**
 * 簡單用戶鏈接Mysql數(shù)據(jù)庫微服務(wù)(通過@Service注解標(biāo)注該類為持久化操作對象)。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017-10-19
 *
 */
public interface IUserService {

    User findUserById(Long id);

    List findAllUsers();

    int insertUser(User user);
}
2.10 添加用戶Service接口實(shí)現(xiàn)類(springms-provider-user-mysql-mybatis-mapper/src/main/java/com/springms/cloud/service/impl/UserServiceImpl.java)
package com.springms.cloud.service.impl;

import com.springms.cloud.dao.IUserDao;
import com.springms.cloud.entity.User;
import com.springms.cloud.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 簡單用戶鏈接Mysql數(shù)據(jù)庫微服務(wù)(通過@Service注解標(biāo)注該類為持久化操作對象)。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017-10-19
 *
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    IUserDao iUserDao;

    @Override
    public User findUserById(Long id) {
        return iUserDao.findUserById(id);
    }

    @Override
    public List findAllUsers() {
        return iUserDao.findAllUsers();
    }

    @Override
    public int insertUser(User user) {
        return iUserDao.insertUser(user);
    }
}
2.11 添加用戶Web訪問層Controller(springms-provider-user-mysql-mybatis-mapper/src/main/java/com/springms/cloud/controller/ProviderUserMysqlMybatisMapperController.java)
package com.springms.cloud.controller;

import com.springms.cloud.entity.User;
import com.springms.cloud.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用戶微服務(wù)Controller。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017-10-19
 *
 */
@RestController
public class ProviderUserMysqlMybatisMapperController {

    @Autowired
    private IUserService iUserService;

    @GetMapping("/simple/{id}")
    public User findUserById(@PathVariable Long id) {
        return this.iUserService.findUserById(id);
    }

    @GetMapping("/simple/list")
    public List findUserList() {
        return this.iUserService.findAllUsers();
    }

    /**
     * 添加一個(gè)student,使用postMapping接收post請求
     *
     * http://localhost:8330/simple/addUser?username=user11&age=11&balance=11
     *
     * @return
     */
    @PostMapping("/simple/addUser")
    public User addUser(@RequestParam(value = "username", required=false) String username, @RequestParam(value = "age", required=false) Integer age, @RequestParam(value = "balance", required=false) String balance){
        User user=new User();

        user.setUsername(username);
        user.setName(username);
        user.setAge(age);
        user.setBalance(balance);

        int result = iUserService.insertUser(user);
        if(result > 0){
            return user;
        }

        user.setId(0L);
        user.setName(null);
        user.setUsername(null);
        user.setBalance(null);
        return user;
    }
}
2.12 添加微服務(wù)啟動(dòng)類(springms-provider-user-mysql-mybatis-mapper/src/main/java/com/springms/cloud/MsProviderUserMysqlMybatisMapperApplication.java)
package com.springms.cloud;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 鏈接Mysql數(shù)據(jù)庫簡單的集成Mybatis框架采用MapperXml訪問數(shù)據(jù)庫。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017-10-19
 *
 */
@SpringBootApplication
@MapperScan("com.springms.cloud.mapper.**")
public class MsProviderUserMysqlMybatisMapperApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsProviderUserMysqlMybatisMapperApplication.class, args);
        System.out.println("【【【【【【 鏈接MysqlMybatisMapperXml數(shù)據(jù)庫微服務(wù) 】】】】】】已啟動(dòng).");
    }
}
三、測試
/****************************************************************************************
 注意:Mybatis 需要加上 entity 等注解才可以使用,不然啟動(dòng)都會(huì)報(bào)錯(cuò);
 @MapperScan("com.springms.cloud.mapper.**") 或者在每個(gè) Mapper 接口文件上添加 @Mapper 也可以;

 一、簡單用戶鏈接Mysql數(shù)據(jù)庫微服務(wù)(通過 mybatis 鏈接 mysql 并用 MapperXml 編寫數(shù)據(jù)訪問):

 1、啟動(dòng) springms-provider-user-mysql-mybatis-mapper 模塊服務(wù),啟動(dòng)1個(gè)端口;
 2、在瀏覽器輸入地址 http://localhost:8330/simple/10 可以看到用戶ID=10的信息成功的被打印出來;

 3、使用 IDEA 自帶工具 Test Restful WebService 發(fā)送 HTTP POST 請求,并添加 username、age、balance三個(gè)參數(shù),然后執(zhí)行請求,并去 mysql 數(shù)據(jù)庫查看數(shù)據(jù)是否存在,正常情況下 mysql 數(shù)據(jù)庫剛剛插入的數(shù)據(jù)成功了:
 4、使用 REST Client 執(zhí)行 "/simple/list" 接口,也正常將 mysql 數(shù)據(jù)庫中所有的用戶信息打印出來了;
 ****************************************************************************************/
四、下載地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接

歡迎關(guān)注,您的肯定是對我最大的支持!!!

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/67840.html

相關(guān)文章

  • SpringCloud 043 鏈接Mysql數(shù)據(jù)庫簡單集成Mybatis框架訪問數(shù)據(jù)庫

    摘要:添加用戶接口實(shí)現(xiàn)類簡單用戶鏈接數(shù)據(jù)庫微服務(wù)通過注解標(biāo)注該類為持久化操作對象。添加一個(gè)使用接收請求添加微服務(wù)啟動(dòng)類鏈接數(shù)據(jù)庫簡單的集成框架訪問數(shù)據(jù)庫。 SpringCloud(第 043 篇)鏈接Mysql數(shù)據(jù)庫簡單的集成Mybatis框架訪問數(shù)據(jù)庫 - 一、大致介紹 1、訪問數(shù)據(jù)庫,自然少不了一些持久化框架,而我本身也是Mybatis框架的支持者; 2、Mybatis是那種專注于sql...

    187J3X1 評論0 收藏0
  • SpringCloud 045 鏈接Mysql數(shù)據(jù)庫集成Mybatis、ehcache采用M

    摘要:添加用戶接口類簡單用戶鏈接數(shù)據(jù)庫微服務(wù)通過注解標(biāo)注該類為持久化操作對象。查找用戶數(shù)據(jù)保存用戶數(shù)據(jù)更新用戶數(shù)據(jù)刪除用戶數(shù)據(jù)這是清除緩存添加緩存配置緩存配置。對象是否永久有效,一但設(shè)置了,將不起作用。設(shè)置對象在失效前允許存活時(shí)間單位秒。 SpringCloud(第 045 篇)鏈接Mysql數(shù)據(jù)庫簡單的集成Mybatis、ehcache框架采用MapperXml訪問數(shù)據(jù)庫 - 一、大致介紹...

    chenjiang3 評論0 收藏0

發(fā)表評論

0條評論

最新活動(dòng)
閱讀需要支付1元查看
<