Spring boot + mybatis + orcale实现步骤实例代码讲解
作者:弓长张&木子李 时间:2023-07-11 22:50:09
接着上次的实现, 添加 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>
第八步:浏览器输入http://localhost:8080/person/show?name=zhang
总结
以上所述是小编给大家介绍的Spring boot + mybatis + orcale实现步骤实例代码讲解网站的支持!
来源:http://www.cnblogs.com/gczmn/archive/2017/12/15/8043105.html
标签:Spring,boot,mybatis,orcale
0
投稿
猜你喜欢
Android4.4 WebAPI实现拍照上传功能
2023-08-17 00:46:21
Mockito mock Kotlin Object类方法报错解决方法
2022-03-10 15:23:37
Android消息机制Handler用法总结
2022-08-10 23:58:56
Android开发之资源文件用法实例总结
2023-05-13 06:02:39
Java ArrayList类的基础使用讲解
2021-11-14 10:22:18
详解java 三种调用机制(同步、回调、异步)
2023-11-25 07:59:57
SpringBoot在RequestBody中使用枚举参数案例详解
2022-12-15 05:16:30
Mybatis拦 截 器实现数据权限的示例代码
2023-11-19 20:15:04
解决springboot配置logback-spring.xml不起作用问题
2022-09-10 11:21:24
基于java构造方法Vector删除元素源码分析
2023-11-25 14:54:45
Springboot文件上传功能的实现
2021-09-20 12:48:47
java实现解析二进制文件的方法(字符串、图片)
2023-04-18 19:07:51
C#实现把科学计数法(E)转化为正常数字值
2022-09-06 23:41:47
Android底部菜单简单应用
2022-03-25 18:09:58
C#中DataTable和List互转的示例代码
2022-08-25 21:40:06
C#实现简易计算器功能(1)(窗体应用)
2022-09-30 16:59:16
C#取得Web程序和非Web程序的根目录的N种取法总结
2023-07-16 07:37:32
springboot 如何取消starter的自动注入
2023-02-06 21:31:05
Android UI使用HTML布局方法实例
2023-04-07 09:14:11
Java 实战项目锤炼之网上商城系统的实现流程
2022-06-26 17:21:47