CSDN博主:【java_wxid】
CSDN博主:点击【Java廖志伟】
CSDN社区:点击【幕后大佬】
码云:点击【互联网Java工程师知识扫盲】
随笔 - 882,  文章 - 0,  评论 - 1,  阅读 - 51602

主项目链接:https://gitee.com/java_wxid/java_wxid
项目架构及博文总结:

项目模块:
前期规划,实现部分

java_wxid   
├── demo                                                            // 演示模块
│     └── 模块名称:apache-mybatis-demo模块                            //Apache Mybatis集成(已实现并有博文总结)
│     └── 模块名称:apache-shardingsphere-demo模块                     //Apache ShardingSphere集成(已实现并有博文总结)
│     └── 模块名称:design-demo模块                                    //设计模式实战落地(已实现并有博文总结)
│     └── 模块名称:elasticsearch-demo模块                             //ElasticSearch集成(已实现并有博文总结)
│     └── 模块名称:mongodb-demo模块                                   //MongoDB集成(已实现并有博文总结)
│     └── 模块名称:redis-demo模块                                     //Redis集成(已实现并有博文总结)
│     └── 模块名称:spring-boot-demo模块                               //Spring Boot快速构建应用(已实现并有博文总结)
│     └── 模块名称:spring-cloud-alibaba-nacos-demo模块                //Spring Cloud Alibaba Nacos集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-alibaba-seata-demo模块                //Spring Cloud Alibaba Seata集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-alibaba-sentinel-demo模块             //Spring Cloud Alibaba Sentinel集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-gateway-demo模块                      //Spring Cloud Gateway集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-hystrix-demo模块                      //Spring Cloud Hystrix集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-open-feign-demo模块                   //Spring Cloud Open Feign集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-ribbon-demo模块                       //Spring Cloud Ribbon集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-security-oauth2-demo模块              //Spring Cloud Security Oauth2集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-security-oauth2-sso-client-demo模块   //Spring Cloud Security Oauth2集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-skywalking-demo模块                   //Spring Cloud Skywalking集成(已实现并有博文总结)
│     └── 模块名称:spring-cloud-stream-demo模块                       //Spring Cloud Stream集成(已实现并有博文总结)
│     └── 模块名称:swagger-demo模块                                   //springfox-swagger2集成(已实现并有博文总结)
│     └── 模块名称:xxl-job模块                                        //xxl-job集成(已实现并有博文总结)
│     └── 模块名称:apache-spark-demo模块                              //Apache Spark集成
│     └── 模块名称:etl-hdfs-hive-hbase-demo模块                       //ETL、HDFS、Hive、Hbase集成
│     └── 模块名称:ddd-mode-demo模块                                  //DDD领域设计
│     └── 模块名称:netty-demo模块                                     //Netty集成
│     └── 模块名称:vue-demo模块                                       //前端vue集成
├── document                                                        // 文档
│     └── JavaKnowledgeDocument                                     //java知识点
│           └── java基础知识点.md                     
│           └── mq知识点.md
│           └── mysql知识点.md
│           └── redis知识点.md
│           └── springcould知识点.md
│           └── spring知识点.md
│     └── FounderDocument                                           //创始人
│           └── 创始人.md

系列文章:快速集成各种微服务相关的技术,帮助大家可以快速集成到自己的项目中,节约开发时间。
提示:系列文章还未全部完成,后续的文章,会慢慢补充进去的。

创建apache-mybatis-demo项目

项目代码:https://gitee.com/java_wxid/java_wxid/tree/master/demo/apache-mybatis-demo
项目结构如下(示例):
在这里插入图片描述

修改pom.xml

代码如下(示例):

<?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>

    <groupId>com.example</groupId>
    <artifactId>apache-mybatis-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>apache-mybatis-demo</name>
    <description>Demo project for Spring Boot</description>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.23</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
    </dependencies>


</project>

修改application.properties

代码如下(示例):

server.port=8092
#mysql数据库
spring.datasource.url= jdbc:mysql://139.224.137.74:3306/syncdemo?characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8
spring.datasource.username= root
spring.datasource.password= ca0a997ee4770063
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#mybatis
#开启驼峰
mybatis.configuration.map-underscore-to-camel-case=true
#打印日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#mybatis-plus配置
mybatis-plus.mapper-locations=classpath*:/mapper/*.xml
#主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
mybatis-plus.global-config.id-type=0
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
mybatis-plus.global-config.field-strategy= 1
#驼峰下划线转换
mybatis-plus.global-config.db-column-underline=true

修改ApacheMybatisDemoApplication

代码如下(示例):

package com.example.apachemybatisdemo;

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

@MapperScan("com.example.apachemybatisdemo.mapper")
@SpringBootApplication
public class ApacheMybatisDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApacheMybatisDemoApplication.class, args);
    }

}

创建DictMapper.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.example.apachemybatisdemo.mapper.DictMapper" >

    <resultMap id="BaseResultMap" type="com.example.apachemybatisdemo.entity.Dict" >
        <id column="dict_id" property="dictId" jdbcType="INTEGER" />
        <result column="ustatus" property="ustatus" jdbcType="VARCHAR" />
        <result column="uvalue" property="uvalue" jdbcType="VARCHAR" />
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="Base_Column_List">
        t.dict_id,t.ustatus,t.uvalue
    </sql>

    <!-- 查询数据 -->
    <select id="getDictByDictId" resultType="com.example.apachemybatisdemo.entity.Dict">
        select
        <include refid="Base_Column_List" />
        from t_dict t
        where t.dict_id = #{dictId}
    </select>


</mapper>

创建DictMapper

代码如下(示例):

package com.example.apachemybatisdemo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.apachemybatisdemo.entity.Dict;
import org.apache.ibatis.annotations.Param;

/**
 * @Author: liaozhiwei
 * @Description: TODO
 * @Date: Created in 21:16 2022/9/2
 */
public interface DictMapper extends BaseMapper<Dict> {

    Dict getDictByDictId(@Param("dictId") String dictId);
}

创建Dict

代码如下(示例):

package com.example.apachemybatisdemo.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

/**
 * @Author: liaozhiwei
 * @Description: TODO
 * @Date: Created in 21:16 2022/9/2
 */
@TableName("t_dict")
public class Dict {

    @TableId(value = "dict_id",type = IdType.AUTO)
    private Long dictId;
    private String ustatus;
    private String uvalue;

    public Long getDictId() {
        return dictId;
    }

    public void setDictId(Long dictId) {
        this.dictId = dictId;
    }

    public String getUstatus() {
        return ustatus;
    }

    public void setUstatus(String ustatus) {
        this.ustatus = ustatus;
    }

    public String getUvalue() {
        return uvalue;
    }

    public void setUvalue(String uvalue) {
        this.uvalue = uvalue;
    }

    @Override
    public String toString() {
        return "Dict{" +
                "dictId=" + dictId +
                ", ustatus='" + ustatus + '\'' +
                ", uvalue='" + uvalue + '\'' +
                '}';
    }
}

创建MyBatisTestController

代码如下(示例):

package com.example.apachemybatisdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.apachemybatisdemo.entity.Dict;
import com.example.apachemybatisdemo.mapper.DictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @author zhiweiLiao<zhiwei.liao @ sgs.com>
 * @Description 这里我直接在controller层直接拿数据返回了,没有再参加service层,实战开发不要像我这样写
 * @Date create in 2022/9/11 0011 20:32
 */

@RestController
public class MyBatisTestController {

    @Autowired
    private DictMapper dictMapper;

    /**
     * 使用通用方法获取所有字典数据
     * @return
     */
    @GetMapping("/getDictAll")
    public List<Dict> getDictAll(){
        return dictMapper.selectList(new LambdaQueryWrapper<Dict>().eq(Dict::getUstatus,1));
    }

    /**
     * 使用XML方式根据字典id获取字典数据
     * @return
     */
    @GetMapping("/getDictByDictId")
    public Dict getDictByDictId(String DictId){
        return dictMapper.getDictByDictId(DictId);
    }

}

校验Apache Mybatis是否正常工作

启动项目

如下图(示例):
在这里插入图片描述

访问服务接口http://localhost:8092/getDictAll

如下图(示例):
在这里插入图片描述

访问服务接口http://localhost:8092/getDictByDictId?DictId=0

如下图(示例):
在这里插入图片描述Apache Mybatis可以正常工作

posted on   我是廖志伟  阅读(6)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

南北踏尘
点击右上角即可分享
微信分享提示