Spring boot + mybatis + orcale實(shí)現(xiàn)步驟實(shí)例代碼講解
接著上次的實(shí)現(xiàn), 添加 mybatis 查詢 orcale 數(shù)據(jù)庫(kù)
第一步: 新建幾個(gè)必須的包, 結(jié)果如下
第二步: 在service包下新建personService.java 根據(jù)名字查person方法接口
package com.example.first.service; import com.example.first.entity.Person; public interface personService { Person queryPersonByName(String name); }
第三步: 在serviceImpl包下新建personServiceImpl.java 實(shí)現(xiàn)personService.java接口
package com.example.first.serviceImpl; import com.example.first.personDao.personMapperDao; import com.example.first.entity.Person; import com.example.first.service.personService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class personServiceImpl implements personService { @Autowired personMapperDao personMapperDao; @Override public Person queryPersonByName(String name) { Person person = personMapperDao.findByName(name); return person; } }
第四步: personDao下新建personMapperDao.java 有一個(gè)查詢person的方法
package com.example.first.personDao; import com.example.first.entity.Person; import org.apache.ibatis.annotations.Mapper; @Mapper public interface personMapperDao { Person findByName(String name); }
第五步: 在resource下新建personMapper.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.first.personDao.personMapperDao"> <resultMap id="findPerson" type="com.example.first.entity.Person"> <result property="name" column="name"/> <result property="age" column="age"/> </resultMap> <select id="findByName" resultMap="findPerson"> select name,age from person where name = #{name} </select> </mapper>
第六步: 在application.properties 中添加數(shù)據(jù)源 , mapper文件路徑 和實(shí)體路徑
spring.jpa.database=oracle spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.url=jdbc:oracle:thin:@//192.168.3.177:1521/orcl spring.datasource.username=liguang_dev spring.datasource.password=123456 spring.jpa.hibernate.ddl-auto=update mybatis.mapperLocations=classpath:/mapper/*.xml mybatis.typeAliasesPackag= com.example.first.entity spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode = HTML5
第七步: 在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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.first</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--orcale數(shù)據(jù)庫(kù)依賴--> <dependency> <groupId>oracle</groupId> <artifactId>ojdbc7</artifactId> <version>1.0.0.1</version> </dependency> <!--mybatis依賴--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
第八步:瀏覽器輸入http://localhost:8080/person/show?name=zhang
總結(jié)
以上所述是小編給大家介紹的Spring boot + mybatis + orcale實(shí)現(xiàn)步驟實(shí)例代碼講解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
redis實(shí)現(xiàn)隊(duì)列的阻塞、延時(shí)、發(fā)布和訂閱
本文主要介紹了redis實(shí)現(xiàn)隊(duì)列的阻塞、延時(shí)、發(fā)布和訂閱,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Spring Cloud 整合Apache-SkyWalking實(shí)現(xiàn)鏈路跟蹤的方法
這篇文章主要介紹了Spring Cloud 整合Apache-SkyWalking鏈路跟蹤的示例代碼,代碼簡(jiǎn)單易懂,通過(guò)圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Java讀寫(xiě)Windows共享文件夾的方法實(shí)例
本篇文章主要介紹了Java讀寫(xiě)Windows共享文件夾的方法實(shí)例,具有一定的參考價(jià)值,有興趣的同學(xué)可以了解一下。2016-11-11Java項(xiàng)目Guava包?HashMultimap使用及注意事項(xiàng)
guava基本上可以說(shuō)是java開(kāi)發(fā)項(xiàng)目中,大概率會(huì)引入的包,今天介紹的主角是一個(gè)特殊的容器HashMultmap,可以簡(jiǎn)單的將它的數(shù)據(jù)結(jié)構(gòu)理解為Map<K,?Set<V>>,今天主要介紹下基礎(chǔ)的知識(shí)點(diǎn)?HashMultmap級(jí)使用,感興趣的朋友一起看看吧2022-05-05springboot 使用zookeeper實(shí)現(xiàn)分布式隊(duì)列的基本步驟
這篇文章主要介紹了springboot 使用zookeeper實(shí)現(xiàn)分布式隊(duì)列,通過(guò)ZooKeeper的協(xié)調(diào)和同步機(jī)制,多個(gè)應(yīng)用程序可以共享一個(gè)隊(duì)列,并按照先進(jìn)先出的順序處理隊(duì)列中的消息,需要的朋友可以參考下2023-08-08聊聊在獲取方法參數(shù)名方面,Spring真的就比Mybatis強(qiáng)?
在獲取方法參數(shù)名方面,Spring真的就比Mybatis強(qiáng)嗎?今天就帶大家聊聊這個(gè)話題,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot 多線程事務(wù)回滾的實(shí)現(xiàn)
本文是基于springboot的@Async注解開(kāi)啟多線程,并通過(guò)自定義注解和AOP實(shí)現(xiàn)的多線程事務(wù),避免繁瑣的手動(dòng)提交/回滾事務(wù),感興趣的可以了解一下2024-02-02