Mybatis连接Oracle数据库【增删改查】实例

文章目录

  • 前言
  • 一、Dependency
  • 二、实体类创建
  • 三、接口定义数据库操作方法
  • 四、创建全局配置文件
  • 五、创建mapper映射文件
  • 六、测试连接

前言

       MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

一、Dependency

  • 导入依赖jar包【oracle jar包导入方法:Oracle jar包导入】
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc6</artifactId>
      <version>11.2.0.1.0 </version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>

  • 包结构图:
    Mybatis连接Oracle数据库【增删改查】实例_第1张图片

二、实体类创建

       根据对应的数据库表创建对应的java实体类:

数据库表一览

Mybatis连接Oracle数据库【增删改查】实例_第2张图片
java实体类创建

package cn.wsj.myoracle.entity;

import java.util.Date;

public class Users {
    private int userid;
    private Date birthday;
    private String username;
    private float userhigh;

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setUserhigh(float userhigh) {
        this.userhigh = userhigh;
    }

    public int getUserid() {
        return userid;
    }

    public Date getBirthday() {
        return birthday;
    }

    public String getUsername() {
        return username;
    }

    public float getUserhigh() {
        return userhigh;
    }
}



三、接口定义数据库操作方法

接口创建:定义了操作数据库的方法

package cn.wsj.myoracle.dao;

import cn.wsj.myoracle.entity.Users;

import java.util.List;

public interface UserDAO {
    public void saveUser(Users user);
    public void delUser(int userid);
    public void batchInsertUser(List<Users> users);
    public void updateUser(Users user);
    public List<Users> findAll();
    public Users findById(int userid);
}


四、创建全局配置文件

       这里对应包图的mybatis.cfg.xml文件,该文件主要定义了数据库配置连接信息:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
    <typeAlias type="cn.wsj.myoracle.entity.Users" alias="user"></typeAlias>
</typeAliases>
    <environments default="wsj">
        <environment id="wsj">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
                <property name="url" value="jdbc:oracle:thin:@sole:1521:orcl"/>
                <property name="username" value="wsj"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/users.xml"></mapper>
    </mappers>
</configuration>

五、创建mapper映射文件

       这里对应包图的local.xml文件,该文件都是由SQL语句构成:

<?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="cn.wsj.myoracle.dao.UserDAO">
    <insert id="saveUser" parameterType="user">
        insert into userinfos values(seq_userid.nextval,#{username},#{birthday},#{userhigh})
    </insert>
    <insert id="batchInsertUser" parameterType="list">
        insert into userinfos select seq_userid.nextval,username,birthday,userhigh from
        <foreach collection="list" separator=" union" item="us" open="(" close=")">
            select #{us.username} username,#{us.birthday} birthday,#{us.userhigh} userhigh from dual
        </foreach>
    </insert>
    <delete id="delUser" parameterType="int">
        delete from userinfos where userid=#{userid}
    </delete>
    <update id="updateUser" parameterType="user">
        update userinfos set username=#{username},birthday=#{brithday},userhigh=#{userhigh} where userid=#{userid}
    </update>
    <select id="findAll" resultType="user">
        select userid,username,birthday,userhigh from userinfos
    </select>
    <select id="findById" parameterType="int" resultType="user">
        select userid,username,birthday,userhigh from userinfos where userid=#{userid}
    </select>
</mapper>

六、测试连接

       这里对应包图的Test.java:

package org.example;

import cn.wsj.myoracle.dao.UserDAO;
import cn.wsj.myoracle.entity.Users;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws Exception {
        Reader reader = Resources.getResourceAsReader("mybatis.cfg.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = factory.openSession();
        UserDAO udao = session.getMapper(UserDAO.class);
        //查看查询结果测试连接
//        System.out.println(udao.findAll());

        //插入数据
//        Users user = new Users();
//        user.setUsername("lisi");
//        user.setBirthday(new java.util.Date());
//        user.setUserhigh(182.6f);
//        udao.saveUser(user);


        //批量插入
        List<Users> us = new ArrayList<>();
        Users user1 = new Users();
        user1.setUsername("wangwu");
        user1.setBirthday(new java.util.Date());
        user1.setUserhigh(173.3f);
        Users user2 = new Users();
        user2.setUsername("zhaoliu");
        user2.setBirthday(new java.util.Date());
        user2.setUserhigh(176.9f);
        us.add(user1);
        us.add(user2);
        udao.batchInsertUser(us);
        session.commit();

        session.close();

    }
}



PS:如果有写错或者写的不好的地方,欢迎各位在评论区留下宝贵的意见或者建议,敬上!如果这篇博客对您有帮助,希望您可以顺手帮我点个赞!不胜感谢!

原创作者:wsjslient

作者主页:https://blog.csdn.net/wsjslient


你可能感兴趣的:(mybatis,mybatis,java,oracle)