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 中

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐