iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >Oracle学习(七) --- MyBatis操作、JDBC操作
  • 393
分享到

Oracle学习(七) --- MyBatis操作、JDBC操作

Oracle学习(七)---MyBatis操作JDBC操作 2017-02-22 01:02:09 393人浏览 才女
摘要

1、mybatis操作 1.1、环境搭建 步骤一:创建项目 test_oracle 步骤二:修改pom.xml文件(MyBatis相关依赖、Oracle驱动、测试依赖) changGou3_paren

Oracle学习(七) --- MyBatis操作、JDBC操作

1、mybatis操作

1.1、环境搭建

  • 步骤二:修改pom.xml文件(MyBatis相关依赖、Oracle驱动、测试依赖)

    
    
        
            changGou3_parent_java78
            com.czxy.changgou3
            1.0-SNAPSHOT
        
        4.0.0
    
        test_oracle
    
        
            
            
                org.springframework.boot
                spring-boot-starter-WEB
            
            
            
                org.springframework.boot
                spring-boot-starter-test
            
    
            
            
                tk.mybatis
                mapper-spring-boot-starter
                2.0.4
            
            
            
                com.GitHub.pagehelper
                pagehelper-spring-boot-starter
                1.2.3
            
            
            
                org.projectlombok
                lombok
            
    
            
            
                com.oracle
                ojdbc6
                12.1.0.1-atlassian-hosted
            
        
    
    
    
    
  • 步骤三:创建yml文件(数据库基本4项 -- Oracle驱动+Oracle连接)

    spring:
      datasource:           #数据源配置
        driver-class-name: oracle.jdbc.driver.OracleDriver
        url: jdbc:oracle:thin:@localhost:1521:xe
        username: czxy002
        passWord: czxy002
    
  • 步骤四:启动类

    package com.czxy;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    
    @SpringBootApplication
    public class TestOracleApplication {
        public static void main(String[] args) {
            SpringApplication.run(TestOracleApplication.class, args);
        }
    }
    
    

1.2、MyBatis 基本操作 + 测试

  • 步骤一:编写 JavaBean,t_area --> Area

package com.czxy.domain;

import lombok.Data;

import javax.persistence.Id;
import javax.persistence.Table;


@Table(name="t_area")
@Data
public class Area {
    @Id
    private Integer id;
    private String name;
}

  • 步骤二:编写 Mapper

package com.czxy.mapper;

import com.czxy.domain.Area;
import tk.mybatis.mapper.common.Mapper;


@org.apache.ibatis.annotations.Mapper
public interface AreaMapper extends Mapper {
}

  • 步骤三:测试类

package com.czxy;

import com.czxy.mapper.AreaMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestOracleApplication.class)
public class AreaTest {
    @Resource
    private AreaMapper areaMapper;

    @Test
    public void testDemo01(){
        System.out.println(areaMapper);
    }
}

1.3、测试:增删改查

  • 使用通用Mapper,Oracle基本增删改查与Mysql相同的。
package com.czxy;

import com.czxy.domain.Area;
import com.czxy.mapper.AreaMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestOracleApplication.class)
public class AreaTest {
    @Resource
    private AreaMapper areaMapper;

    @Test
    public void testInsert(){
        //添加
        Area area = new Area();
        area.setName("米国");
        area.setId(7);

        areaMapper.insert( area );
    }

    @Test
    public void testUpdate(){
        Area area = new Area();
        area.setName("米小国");
        area.setId(7);

        areaMapper.updateByPrimaryKey( area );
    }

    @Test
    public void testDelete(){
        //作业
    }
}

  • 通过Mapper使用 Oracle 序列

    • 解决1:自定义添加方法,直接使用序列

      @org.apache.ibatis.annotations.Mapper
      public interface AreaMapper extends Mapper {
          @Insert("insert into t_area(id,name) values(seq_stuno.nextval ,#{name})")
          public void save(Area area);
      }
      
    • 解决2:通过网上查询资料,使用注解(存在问题,提交的id为null)

      @Table(name="t_area")
      @Data
      public class Area {
          @Id
          //@SequenceGenerator(name="any" ,sequenceName = "seq_stuno")
          //@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "select seq_stuno.nextval from dual")
          private Integer id;
          private String name;
      }
      

1.4、测试:储存过程

  • 没有返回值

        
        @Insert("call add_area(#{name})")
        //如果运行出错,添加下面注解,表示执行的是存储过程
        @Options(statementType = StatementType.CALLABLE)
        public void addArea(@Param("name") String name);
    
  • 有返回值

        
        @Insert("call add_area2(#{id, mode=OUT, jdbcType=INTEGER},#{name})")
        @Options(statementType = StatementType.CALLABLE)
        public void addArea2(Area area);
    
  • 测试程序

    package com.czxy;
    
    import com.czxy.domain.Area;
    import com.czxy.mapper.AreaMapper;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import javax.annotation.Resource;
    
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = TestOracleApplication.class)
    public class AreaProTest {
        @Resource
        private AreaMapper areaMapper;
    
        @Test
        public void testProAddArea(){
            //调用 add_area 存储过程
            areaMapper.addArea("米国1111");
        }
    
        @Test
        public void testProAddArea2(){
            //调用 add_area2 存储过程
            Area area = new Area();
            area.setName("鹰国111");
            areaMapper.addArea2(area);
            System.out.println(area.getId());
        }
    
    }
    
    

2、JDBC操作

2.1、数据库操作

2.2、JDBC PreparedStatement操作

2.2.1、添加

package com.czxy;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class TestJdbc {
    @Test
    public void testInsert() throws Exception {
        String url = "jdbc:oracle:thin:@localhost:1521:xe";
        String username = "czxy002";
        String password = "czxy002";

        //1 注册驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //2 获得连接
        Connection conn = DriverManager.getConnection(url, username, password);
        //3.1 处理sql语句--添加,将实际参数替换?
        String sql = "insert into t_area(id,name) values(seq_stuno.nextval,? )";
        //3.2 获得预处理对象 PreparedStatement
        PreparedStatement psmt = conn.prepareStatement(sql);
        //3.3 设置参数 -- 给?设置实际参数,有几个?问号,就需要设置几次
        psmt.setString(1, "凹国");
        //4 执行
        int result = psmt.executeUpdate();
        //5 处理结果
        System.out.println(result);
        //6 释放资源
        psmt.close();
        conn.close();
    }
}

2.2.2、查询

@Test
    public void testSelectAll() throws Exception {
        //查询所有
        //1 注册驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //2 获得连接
        Connection conn = DriverManager.getConnection(url, username, password);
        //3.1 处理sql语句
        String sql = "select * from t_area";
        //3.2 获得预处理对象
        PreparedStatement psmt = conn.prepareStatement(sql);
        //3.3 设置参数 -- 没有?
        //4 执行语句
        ResultSet rs = psmt.executeQuery();
        //5 处理结果
        while(rs.next()) {
            // 处理一行的数据  rs.get类型("列名");
            int id = rs.getInt("id");
            String name = rs.getString("name");
            System.out.println(id + "__" + name);
        }
        //6 释放资源
        rs.close();
        psmt.close();
        conn.close();

    }

3、JDBC Statement 操作

3.1、查询详情

@Test
    public void testFindById() throws Exception {
        //通过id查询
        int id = 999;
        //1 注册驱动
        Class.forName(driverName);
        //2 获得连接
        Connection conn = DriverManager.getConnection(url, username, password);
        //3 获得语句执行者 Statement
        Statement st = conn.createStatement();
        //4 执行sql语句 -- 没有结果、只有一条
        ResultSet rs = st.executeQuery("select * from t_area where id = " + id);
        //5 处理结果 -- 可以使用while,最多只有一条,if可以处理
        if(rs.next()){
            // 获得一行数据
            int _id = rs.getInt("id");
            String name = rs.getString("name");

            System.out.println(_id + "###" + name);
        } else {
            System.out.println("没有查询结果");
        }
        //6 释放资源
        rs.close();
        st.close();
        conn.close();

    }
您可能感兴趣的文档:

--结束END--

本文标题: Oracle学习(七) --- MyBatis操作、JDBC操作

本文链接: https://www.lsjlt.com/news/6484.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • sql怎么查看表的索引
    通过查询系统表,可以获取表的索引信息,包括索引名称、是否唯一、索引类型、索引列和行数。常用系统表有:mysql 的 information_schema.statistics、postg...
    99+
    2024-05-14
    mysql oracle
  • sql怎么查看索引
    您可以使用 sql 通过以下方法查看索引:show indexes 语句:显示表中定义的索引列表及其信息。explain 语句:显示查询计划,其中包含用于执行查询的索引。informat...
    99+
    2024-05-14
  • sql怎么查看存储过程
    如何查看 sql 存储过程的源代码:使用 show create procedure 语句直接获取创建脚本。查询 information_schema.routines 表的 routi...
    99+
    2024-05-14
  • sql怎么查看视图表
    要查看视图表,可以使用以下步骤:使用 select 语句获取视图中的数据。使用 desc 语句查看视图的架构。使用 explain 语句分析视图的执行计划。使用 dbms 提供...
    99+
    2024-05-14
    oracle python
  • sql怎么查看创建的视图
    可以通过sql查询查看已创建的视图,具体步骤包括:连接到数据库并执行查询select * from information_schema.views;查询结果将显示视图的名称、...
    99+
    2024-05-14
    mysql
  • sql怎么用循环语句实现查询
    可以通过 do 和 while 语句创建循环,并在循环内执行查询,详细步骤包括:定义循环变量设置循环初始值循环执行查询更新循环变量执行查询循环退出条件 SQL 中使用循环语句实现查询 ...
    99+
    2024-05-14
  • sql怎么用代码修改表中数据
    通过 sql 代码修改表中数据的方法包括:修改单个记录:使用 update 语句设置列值并指定条件。修改多条记录:在 update 语句中指定多个条件来修改满足条件的所有记录。增加新列:...
    99+
    2024-05-14
  • sql怎么用命令创建数据库
    在 sql 中使用 create database 命令创建新数据库,其语法包含以下步骤:指定数据库名称。指定数据库文件和日志文件的位置(可选)。指定数据库大小、最大大小和文件增长(可选...
    99+
    2024-05-14
  • sql怎么用身份证提取年龄
    sql 中提取身份证号码中的年龄的方法:提取出生日期部分(身份证号码中第 7-14 位);使用 to_date 函数转换为日期格式;使用 extract 函数计算与当前日期之间的年差。 ...
    99+
    2024-05-14
  • sql怎么看字段长度
    有两种方法可查看 sql 中的字段长度:使用 information_schema 架构,其中包含元数据信息,可用于查询字段长度。使用内建函数,如 length(),其适用于字符串数据类...
    99+
    2024-05-14
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作