结合 springboot 在 mybatis 中使用枚举常量

13 篇文章 1 订阅
4 篇文章 0 订阅

1. application.properties 配置文件

#oracle 配置
spring.datasource.url=jdbc:oracle:thin:@192.168.1.100:1521/ORCL

spring.datasource.username=TEST
spring.datasource.password=111111
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

#jpa 配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true

#mybatis 配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.type-aliases-package=com.example.d3.mapper
mybatis.type-handlers-package=org.apache.ibatis.type.LocalDateTypeHandler

spring.messages.basename=i18n/messages

2. TestMapper.java

package com.example.d3.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface TestMapper {
	String[] getLevel();
}

3. TestMapper.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.d3.mapper.TestMapper">
	<select id="getLevel" resultType="java.lang.String">
		select
		       decode(
		           T_LEVEL,
		           '${@com.example.d3.enums.Level@LOW.getValue()}',
					'${@com.example.d3.enums.Level@LOW.getDescription()}',

				   '${@com.example.d3.enums.Level@MEDIUM.getValue()}',
				   '${@com.example.d3.enums.Level@MEDIUM.getDescription()}',

				   '${@com.example.d3.enums.Level@HIGH.getValue()}',
				   '${@com.example.d3.enums.Level@HIGH.getDescription()}'
				   )
		from T_level
	</select>
</mapper>

4. 测试类

package com.example.d3;

import com.example.d3.mapper.TestMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class MybatisTest {
	private static final Logger logger = LoggerFactory.getLogger(MybatisTest.class);

	@Autowired
	public TestMapper testMapper;

	@Test
	public void test() {
		String[] s = testMapper.getLevel();
		for (String s1 : s) {
			logger.info(s1);
		}
	}
}

5. 输出结果

2020-01-16 23:09:43.545  INFO 46110 --- [           main] com.example.d3.MybatisTest               : 低
2020-01-16 23:09:43.545  INFO 46110 --- [           main] com.example.d3.MybatisTest               : 中
2020-01-16 23:09:43.545  INFO 46110 --- [           main] com.example.d3.MybatisTest               : 高

附 :springboot 中通过配置国际化文件( messages.properties )给枚举类( enum )赋值

https://blog.csdn.net/qq_42557844/article/details/104011106

结合 springboot 中通过配置国际化文件( messages.properties )给枚举类( enum )赋值 可以给枚举类赋值,并将枚举类的值应用到 mybatis 中

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值