SpringBoot-Mybatis-Oracle 全流程
只是简单记录一下流程 示例使用springboot版本为2.1.3.RELEASE
依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.oracle</groupId><artifactId>ojdbc7</artifactId><version>12.1.0.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- 集成mybatis --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.14</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.71</version></dependency>
由于Oracle的jar是不能从仓库直接下载的 所以需要手动加入到仓库当中
本次使用的jar是 ojdbc7-12.1.0.2.jar
手动添加的方法是:
使用bash切换到jar的所在目录然后执行以下命令:
mvn install:install-file -Dfile=ojdbc7-12.1.0.2.jar-DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.2 -Dpackaging=jar -DgeneratePom=true
即可将jar添加到个人仓库
配置文件
application.properties的配置为:
spring.datasource.jdbc-url=jdbc:oracle:thin:@192.168.178.131:1521:orclspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=oracle.jdbc.OracleDriver
SpringBoot 1 使用spring.datasource.url
SpringBoot 2 使用spring.datasource.jdbc-url
示例中的oracle数据库使用的虚拟机版本为 :
TNS for Linux: Version 11.2.0.4.0 – Production
配置类
import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.SqlSessionTemplate;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration//指定扫描的mapper接口所在的包@MapperScan(basePackages = \"com.mapper\", sqlSessionFactoryRef = \"DBDataSqlSessionFactory\")public class DataSourceConfig {@Bean(name = \"DBDataSource\")@ConfigurationProperties(prefix = \"spring.datasource\") //告诉自动加载配置的属性public DataSource dataSource() {return DataSourceBuilder.create().build();}@Bean(name = \"DBDataSqlSessionFactory\")public SqlSessionFactory sqlSessionFactory(@Qualifier(\"DBDataSource\") DataSource dataSource)throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(\"classpath:mapper/*.xml\"));return bean.getObject();}@Bean(name = \"DBDataTransactionManager\")public DataSourceTransactionManager transactionManager(@Qualifier(\"DBDataSource\") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = \"DBDataSqlSessionTemplate\")public SqlSessionTemplate sqlSessionTemplate(@Qualifier(\"DBDataSqlSessionFactory\") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}}
Controller
import com.service.DaService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;import java.util.Map;@Controller@RequestMapping(\"/info\")public class SyncTgInfoController {//注入service@Autowiredprivate DaService daServiceImpl;@RequestMapping(path = \"/getAll\", method = RequestMethod.GET)@ResponseBodypublic List<Map<String, Object>> getAll(){return daServiceImpl.queryAll();}}
Service
interface:
import java.util.List;import java.util.Map;public interface DaService {public List<Map<String, Object>> queryAll();}
impl:
import com.grid.aostar.tgmonitor.mapper.SyncInfoMapper;import com.grid.aostar.tgmonitor.service.DaService;import com.grid.aostar.tgmonitor.util.JsonUtil;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import org.springframework.util.CollectionUtils;import java.util.*;@Service@Slf4j@Transactionalpublic class DaServiceImpl implements DaService {@Autowiredprivate SyncInfoMapper syncInfoMapper;@Overridepublic List<Map<String, Object>> queryAll() {return syncTgInfoMapper.queryAll();}}
Mapper/Dao
interface:
import org.springframework.stereotype.Repository;import java.util.List;import java.util.Map;@Repositorypublic interface SyncTgInfoMapper {List<Map<String, Object>> queryAll();}
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\"><!-- namespace指向mapper所在位置--><mapper namespace=\"com.mapper.SyncInfoMapper\"><select id=\"queryAll\" resultType=\"map\">SELECT * FROM DA</select></mapper>
访问的请求路径为
http://localhost:port/info/getAll
结语
以上即为本次记录的内容 希望能对您有所帮助.谢谢!