架構實戰項目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建後臺項目框架(二)

接下來我們將整合mybatisplus+Oracle,實現一個簡單的查詢。(期間踩了很多坑,遇到一些問題,還好慢慢解決了。現在是通過配置文件的方式來進行dubbo服務的註冊和發佈,希望以後能在學習和實踐中使用springboot註解方式(也有可能是因爲知識還沒到那個層面,無法弄懂其中的奧義大笑))

一、SpringBoot整合mybatisplus

    1 衆所周知,mybatisplus作爲mybatis的一個升級版,大大地簡化了大家配置mybatis的xml文件的時間,並且已經整合了很多通用的方法,包括分頁的方法等,本項目不細講mybatisplus,有興趣的同學可以自己去學一下。本次項目使用mybatisplus作爲後臺數據庫訪問的框架。

    2 mybatisplus和mybatisplus的SpringBoot依賴:

<!--mybatisplus 依賴 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>${mybatisplus.version}</version>
</dependency>
<!-- Mybatisplus SpringBoot-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatisplus-spring-boot-starter</artifactId>
    <version>${mybatisplus-spring-boot-starter.version}</version>
</dependency>

    具體的版本根據你的需求來定。

    3 對應的實體類UserEntity:

@TableName("USER_INFO")
public class UserEntity extends Model<UserEntity> {
    @TableId(value = "USER_ID",type = IdType.AUTO)
    private Integer userId;
    private String username;
    private String password;
    private Integer age;
    protected Serializable pkVal() {
        return userId;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "UserEntity{" +
                "userId=" + userId +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }

    public UserEntity(Integer userId, String username, String password, Integer age) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.age = age;
    }

    public UserEntity() {
    }

    4 Mapper層UserMapper:

public interface UserMapper extends BaseMapper<UserEntity>{
    Integer getCount();
}

    5 UserMapper.xml:

    

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.laowang.mapper.UserMapper">
    <resultMap id="userMap" type="com.laowang.entity.UserEntity">
        <result column="USER_ID" property="userId"></result>
        <result column="USERNAME" property="username"></result>
        <result column="PASSWORD" property="password"></result>
        <result column="AGE" property="age"></result>
    </resultMap>
    <select id="getCount" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM USER_INFO
    </select>
</mapper>

    6 服務接口層IUserService:

public interface IUserService extends IService<UserEntity> {
    UserEntity getUserById(int id);
    UserEntity findUser();
    /**
     * 查詢總數量
     * @return
     */
    Integer getCount();
}

    7 服務接口實現層UserServiceImpl:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,UserEntity> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    public UserEntity getUserById(int id) {
        return userMapper.selectById(id);
    }

    public UserEntity findUser() {
        return new UserEntity(20,"laowang","123456789",24);
    }

    public Integer getCount() {
        return userMapper.getCount();
    }
}

    8 控制層UserController:

@RestController
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/getUser/{id}")
    public UserEntity getUser(@PathVariable int id) {
        return userService.getUserById(id);
    }

    @GetMapping("/test")
    public UserEntity findUser() {
        return userService.findUser();
    }

    @GetMapping("/getCount")
    public Integer getCount() {
        return userService.getCount();
    }
}

9 application.yml:

# Dubbo 服務提供者配置
server:
  port: 8070
spring:
  application:
    name: provider
  datasource:
    url: jdbc:oracle:thin:@127.0.0.1:1521:hadoop
    username: sys as sysdba
    password: 123456789
    driver-class-name: oracle.jdbc.driver.OracleDriver
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    filters: stat,wall,logback
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    useGlobalDataSourceStat: true
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  #實體掃描,多個package用逗號或者分號分隔
  type-aliases-package: com.laowang.entity

10 運行項目進行測試,即可得到一個簡單的查詢結果(由於我的項目已經分割了,所以這裏暫時看不到效果,希望大家按照我的步驟去嘗試,能夠得到一個簡答的查詢)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章