接着上次的实现, 添加 mybatis 查询 orcale 数据库
第一步: 新建几个必须的包, 结果如下
第二步: 在service包下新建personservice.java 根据名字查person方法接口
1
2
3
4
5
|
package com.example.first.service;
import com.example.first.entity.person;
public interface personservice {
person querypersonbyname(string name);
}
|
第三步: 在serviceimpl包下新建personserviceimpl.java 实现personservice.java接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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的方法
1
2
3
4
5
6
7
|
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
1
2
3
4
5
6
7
8
9
10
11
|
<?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文件路径 和实体路径
1
2
3
4
5
6
7
8
9
10
11
|
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文件中添加依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<?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