Springboot-Mybatis-Oracle-H2-Properties demo
先说废话: 之前有一篇spring 框架的Mybatis 框架,这个是为将之改造为springboot而生
代码中有注释的地方刻意没有删, 心思比较细的话,可以了解下
先上代码结构:
pom文件:
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.adao</groupId> <artifactId>mybaits</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybaits</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>10.2.0.2.0</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>cn.easyproject</groupId> <artifactId>orai18n</artifactId> <version>12.1.0.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
MAIN 文件
包: com.adao.mybatis
PropertiesUtil 有注释的地方是
package com.adao.mybaits.common; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * @Classname PropertiesUtil * @Description TODO * @Date 2020/7/23 * @Created ADAO */ @Configuration @ConfigurationProperties(prefix = "config", ignoreUnknownFields = false) //@PropertySource("classpath:config.properties") @Component public class PropertiesUtil { @Value("${socket.server.address}") private String serverAddress; @Value("${socket.server.port}") private String serverPort; public String getServerAddress() { return serverAddress; } public void setServerAddress(String serverAddress) { this.serverAddress = serverAddress; } public String getServerPort() { return serverPort; } public void setServerPort(String serverPort) { this.serverPort = serverPort; } }
TerController
package com.adao.mybaits.controller; import com.adao.mybaits.common.PropertiesUtil; import com.adao.mybaits.entity.TerminalBean; import com.adao.mybaits.service.TerminalService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @Classname TerController * @Description TODO * @Date 2020/7/23 * @Created ADAO */ //@EnableConfigurationProperties(PropertiesUtil.class) @RestController @RequestMapping("/test") public class TerController { @Autowired PropertiesUtil propertiesUtil; @Autowired private TerminalService terminalService; @RequestMapping("getTer/{id}") public String GetUser(@PathVariable long id) { System.out.println("terminalService ##############: " + terminalService); String teaminalName = terminalService.getTerNameById(String.valueOf(id)); System.out.println(teaminalName); return teaminalName; } @RequestMapping("getTaskTer/{id}") public long getTaskTer(@PathVariable long id) { //"1433142404804l" List<TerminalBean> b = terminalService.getTerListByTaskId(id); System.out.println(b.get(0).getTerminalAddress()); return b.get(0).getTerminalAddress(); } @RequestMapping("getTer") public String GetUser() { System.out.println("terminalService ##############: " + terminalService); List<TerminalBean> list = terminalService.list(); System.out.println("数据库有" + list.size() + "条终端记录"); return "数据库有" + list.size() + "条终端记录"; } @RequestMapping("getProperties") public String testProperties() { String str = propertiesUtil.getServerAddress(); String i = propertiesUtil.getServerPort(); System.out.println(str); System.out.println(i); return str + i; } @RequestMapping("setProperties") public String setProperties() { propertiesUtil.setServerAddress("192.168.127.222"); propertiesUtil.setServerPort("8888"); String str2 = propertiesUtil.getServerAddress(); String i2 = propertiesUtil.getServerPort(); System.out.println(str2); System.out.println(i2); return str2 + i2; } }
TerminalMapper
package com.adao.mybaits.dao; import java.util.List; import java.util.Map; import com.adao.mybaits.entity.TerminalBean; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; /** * 终端mapper * */ //@Repository //@Transactional //@Mapper @Component public interface TerminalMapper { /** * 根据ID取终端名称 * * @param terId * @return */ public String getTerNameById(long terId); public List<TerminalBean> list(); /** * 根据taskId获取对应的终端信息 * @param taskId * @return */ public List<TerminalBean> getTerListByTaskId(long taskId); /** * @Author 瞿天赐 * @Description 根据终端ID查询表地址ID * @Date 2020/07/09 * @Param [terminalId] * @return java.util.List<java.lang.Long> **/ public List<Map<String,Object>> getMpointIdList(@Param("terminalId") long terminalId); }
TerminalBean.
package com.adao.mybaits.entity; /** * 终端档案 */ public class TerminalBean { private long terminalId; // 终端ID private long terminalAddress;// 终端地址 public long getTerminalId() { return terminalId; } public void setTerminalId(long terminalId) { this.terminalId = terminalId; } public long getTerminalAddress() { return terminalAddress; } public void setTerminalAddress(long terminalAddress) { this.terminalAddress = terminalAddress; } }
TerminalService
package com.adao.mybaits.service; import com.adao.mybaits.entity.TerminalBean; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; /** * 业务接口 * * @author ADAO * 2020-06-23 */ public interface TerminalService { /** * 测试方法 :获取列表 * * @return */ public List<TerminalBean> list(); /** * 测试方法 :获取终端名 * * @return */ public String getTerNameById(String id); /** * 根据任务id获取对应的终端信息 * @param taskId * @return */ public List<TerminalBean> getTerListByTaskId(long taskId); /** * @Author 瞿天赐 * @Description 根据终端ID查询表地址ID * @Date 2020/07/09 * @Param [terminalId] * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>> **/ public List<Map<String,Object>> getMpointIdList(long terminalId); }
MybaitsApplication
package com.adao.mybaits; import org.apache.ibatis.annotations.Mapper; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @MapperScan(basePackages = {"com.adao.mybaits.dao"}) @SpringBootApplication //@ComponentScan(basePackages = {"com.adao.mybaits"}) public class MybaitsApplication { public static void main(String[] args) { SpringApplication.run(MybaitsApplication.class, args); } }
TerminalServiceImpl
package com.adao.mybaits.service.impl; import com.adao.mybaits.entity.TerminalBean; import com.adao.mybaits.dao.TerminalMapper; import com.adao.mybaits.service.TerminalService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; /** * 实现类 * * @author ADAO * 2020-06-23 */ @Service public class TerminalServiceImpl implements TerminalService { @Autowired private TerminalMapper terminalMapper; @Override public List<TerminalBean> list() { System.out.println("terminalMapper####################" +terminalMapper); return terminalMapper.list(); } @Override public String getTerNameById(String id) { //long terId = Long.parseLong("1437640452317"); long terId = Long.parseLong(id); String teaminalName = terminalMapper.getTerNameById(terId); return teaminalName; } @Override public List<TerminalBean> getTerListByTaskId(long taskId) { List<TerminalBean> terminalBeans = terminalMapper.getTerListByTaskId(taskId); return terminalBeans; } @Override public List<Map<String, Object>> getMpointIdList(long terminalId) { return terminalMapper.getMpointIdList(terminalId); } }
RESOURCE 文件
mapping/ TerminalMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="com.adao.mybaits.dao.TerminalMapper"> <select id="getTerNameById" resultType="String"> select TERMINAL_NAME from T_TERMINAL where TERMINAL_ID=#{terId} </select> <select id="list" resultType="com.adao.mybaits.entity.TerminalBean"> select * from T_TERMINAL </select> <select id="getTerListByTaskId" resultType="com.adao.mybaits.entity.TerminalBean"> select t.TERMINAL_ID terminalId, t.TERMINAL_ADDRESS terminalAddress from T_TASK_DEVICE_RELATION tdr, T_TERMINAL t where TASK_ID=#{taskId} and t.TERMINAL_ID = tdr.DEVICE_ID </select> <select id="getMpointIdList" resultType="java.util.Map"> SELECT m.MPOINT_ID mpointId, m.MPOINT_INDEX mpointIndex FROM T_TERMINAL t LEFT JOIN T_MPOINT m ON t.TERMINAL_ID = m.TERMINAL_ID WHERE t.TERMINAL_ID = #{terminalId} </select> </mapper>
配置文件:
appliation.yml
#spring: # profiles: # active:dev ############################################################ # # 配置数据源信息 # ############################################################ spring: datasource: # 数据源的相关配置 type: com.zaxxer.hikari.HikariDataSource # 数据源类型:HikariCP driver-class-name: oracle.jdbc.OracleDriver # oracle驱动 url: jdbc:oracle:thin:@192.168.127.150:1521:ORCL username: coll_platform password: coll_platform hikari: connection-timeout: 30000 # 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 默认:30秒 minimum-idle: 5 # 最小连接数 maximum-pool-size: 20 # 最大连接数 auto-commit: true # 自动提交 idle-timeout: 600000 # 连接超时的最大时长(毫秒),超时则被释放(retired),默认:10分钟 pool-name: DateSourceHikariCP # 连接池名字 max-lifetime: 1800000 # 连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认:30分钟 1800000ms ############################################################ # # mybatis 配置 # ############################################################ mybatis: type-aliases-package: com.adao.mybaits.entity # 所有POJO类所在包路径 mapper-locations: classpath:mapping/TerminalMapper.xml # mapper映射文件 server: port: 8080 #showSql logging: level: com: example: mapper : debug
config.properties
#socket server config
socket.server.address=127.0.0.1
socket.server.port=8511
运行结果
控制台hikari日志
完美