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

这篇文章主要介绍了Spring boot + mybatis + orcale的实现步骤实例代码讲解,需要的朋友可以参考下

接着上次的实现, 添加 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

select name,age from person where name = #{name}

第六步: 在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文件添加依赖

4.0.0com.example.firstspringboot0.0.1-SNAPSHOTjarspringbootDemo project for Spring Bootorg.springframework.bootspring-boot-starter-parent1.5.6.RELEASEUTF-8UTF-81.8org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-starter-thymeleaforacleojdbc71.0.0.1org.mybatis.spring.bootmybatis-spring-boot-starter1.1.1org.springframework.bootspring-boot-starter-jdbcorg.springframework.bootspring-boot-maven-plugin

第八步:浏览器输入

总结

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...