Then in the last implementation, add mybatis query orcale database
Step 1: Create a few new required packages, the results are as follows
Step 2: Create a new personService.java under the service package to check the person method interface according to the name
package com.example.first.service;import com.example.first.entity.Person;public interface personService { Person queryPersonByName(String name);}Step 3: Create a new personServiceImpl.java under the serviceImpl package to implement the personService.java interface
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@Transactionalpublic class personServiceImpl implements personService { @Autowired personMapperDao personMapperDao; @Override public Person queryPersonByName(String name) { Person person = personMapperDao.findByName(name); return person; }}Step 4: Create a new personMapperDao.java under personDao. There is a method to query person
package com.example.first.personDao;import com.example.first.entity.Person;import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface personMapperDao { Person findByName(String name);}Step 5: Create a new personMapper.xml under resource
<?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>Step 6: Add data source, mapper file path and entity path in application.properties
spring.jpa.database=oraclespring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriverspring.datasource.url=jdbc:oracle:thin:@//192.168.3.177:1521/orclspring.datasource.username=liguang_devspring.datasource.password=123456spring.jpa.hibernate.ddl-auto=updatemybatis.mapperLocations=classpath:/mapper/*.xmlmybatis.typeAliasesPackag= com.example.first.entityspring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=html5
Step 7: Add dependencies in the pom file
<?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 database dependencies--> <dependency> <groupId>oracle</groupId> <artifactId>ojdbc7</artifactId> <version>1.0.0.1</version> </dependency> <!--mybatis dependencies--> <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> </dependency> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
Step 8: Enter http://localhost:8080/person/show?name=zhang in the browser
Summarize
The above is the example code explanation of Spring boot + mybatis + orcale implementation steps introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!