Apache Mybatis集成
Posted java_wxid
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache Mybatis集成相关的知识,希望对你有一定的参考价值。
文章目录
创建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可以正常工作
以上是关于Apache Mybatis集成的主要内容,如果未能解决你的问题,请参考以下文章