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

时间:2021-05-20

接着上次的实现, 添加 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@Transactionalpublic 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;@Mapperpublic 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=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

第七步: 在pom文件中添加依赖

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://.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实现步骤实例代码讲解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章