欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring boot + mybatis + orcale实现步骤实例代码讲解

程序员文章站 2023-12-12 13:50:10
接着上次的实现, 添加 mybatis 查询 orcale 数据库 第一步: 新建几个必须的包, 结果如下 第二步: 在service包下新建personservi...

接着上次的实现, 添加 mybatis 查询 orcale 数据库

第一步: 新建几个必须的包, 结果如下

Spring boot + mybatis + orcale实现步骤实例代码讲解

第二步: 在service包下新建personservice.java 根据名字查person方法接口

package com.example.first.service;
import com.example.first.entity.person;
public interface personservice {
 person querypersonbyname(string name);
}

第三步: 在serviceimpl包下新建personserviceimpl.java 实现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  有一个查询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 中添加数据源 , mapper文件路径 和实体路径

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数据库依赖-->
  <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>

第八步:浏览器输入

Spring boot + mybatis + orcale实现步骤实例代码讲解

总结

以上所述是小编给大家介绍的spring boot + mybatis + orcale实现步骤实例代码讲解,希望对大家有所帮助

上一篇:

下一篇: