springBoot+mybatis+oracle

目录

目录

一、前提

二、操作

1、项目结构

2、pom配置

3、po类

4、xml

5、mapper及service层

6、controller层

7、页面测试

三、注意



一、前提

1、本文章主要写一个springBoot+mybatis+oracle的一个模板;

2、数据源,oracle

create table SYSTEM.STUDENT
(
  name VARCHAR2(12),
  age  NUMBER,
  sex  VARCHAR2(2)
);
 
insert into student(name,age,sex) values('lisi',20,'男');
insert into student(name,age,sex) values('wangli',34,'女');
CREATE TABLE `lesson`(
  `id` VARCHAR(12) PRIMARY KEY,
  `name` VARCHAR(36) NOT NULL ,
  `credit` FLOAT  DEFAULT 0,
  `teacher` VARCHAR(36) DEFAULT 0
);
 
INSERT INTO lesson (id, name, credit, teacher) VALUES ('1', 'chiness', 5, 'zhangsan');
INSERT INTO lesson (id, name, credit, teacher) VALUES ('2', 'math', 5, 'lisi');

二、操作

1、项目结构

2、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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springboot</groupId>
    <artifactId>mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- orcale驱动 -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>12.1.0.1-atlassian-hosted</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3、po类

@Data
@Accessors(chain = true)
public class Student {
    private String name;
    private int age;
    private String sex;
}

4、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.spb.mybatis.mapper.StudentMapper">

    <select id="getStudentInfo" resultType="com.spb.mybatis.po.Student">
      select * from student
    </select>

</mapper>

5、mapper及service层

import java.util.List;
@Mapper
public interface StudentMapper {
    List<Student> getStudentInfo();
}
@Service
public class StudentService{
    @Autowired
    StudentMapper studentMapper;

    public List<Student> getStudentInfo() {
        return studentMapper.getStudentInfo();
    }
}

6、controller层

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentService studentService;
    @RequestMapping("/getInfo")
    @ResponseBody
    public List<Student> getStudentInfo(){
        List<Student> studentInfo = studentService.getStudentInfo();
        return studentInfo;
    }
}

7、页面测试

三、注意

xml文件要放到resources文件夹下,再从配置文件配置其位置:

mybatis.mapper-locations=classpath:mapping/*.xml

如果将xml放在src下,即使在配置文件配置对其位置,target下也不会出现对应的xml,即程序会报错;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郝少

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值