欢迎光临散文网 会员登陆 & 注册

Oracle作业:JDBC连接数据库增删改查,搭建一个Mybatis框架,创建一个表【诗书画唱】

2021-03-09 19:52 作者:诗书画唱  | 我要投稿

概括:

搭建一个Mybatis框架

在数据库中创建一个宠物表(id,name,color)

在数据库中创建一个科目表(id,sname,point)

在数据库中创建一个车类表(cid,cname,cp车牌号,describe描述)

在mybatis框架中,对上面的三个表进行下面的操作:

selectAll

selectById

add

update

deleteById

要写三个bean,三个sqlMap.xml文件和一个测试类


作业 START


1、搭建一个Mybatis框架

(eclipse中进行,先创建一个简单的Java项目,暂时不创建Java Web项目)




2、在数据库中创建一个宠物表(id,name,color)

--drop table pet

                       

create table pet(

    id number primary key,

    name varchar2(30) not null,

   color varchar2(30) not null

);



--drop sequence seq_pet

create sequence seq_pet

start with 1       --起始值是1

increment by 1     --增长的值   

maxvalue 999999999 --序列号的最大值

minvalue 1         --序列号的最小值

nocycle            --是否循环

cache 10;          --预存



insert into pet values(seq_pet.nextval,'小黑猫','黑色');

insert into pet values(seq_pet.nextval,'小红鱼','红色');

insert into pet values(seq_pet.nextval,'小蓝狗','蓝色');


--update pet set name='诗书画唱的猫',color='红色' where id=1


--select * from pet

--delete from pet where id=4





3、在数据库中创建一个科目表(id,sname,point)


--科目:subject  

 --drop table subject                  

create table subject(

    id number primary key,

    name varchar2(30) not null,

   point number(4,1)

);

--【number(4,3)是表示 这个数 一共有4位是有效位,后面的3 表示有3个是小数

--也就是这个数 只能是1.234,这样格式的 最大只能是9.999】


--drop sequence seq_subject

create sequence seq_subject

start with 1       --起始值是1

increment by 1     --增长的值   

maxvalue 999999999 --序列号的最大值

minvalue 1         --序列号的最小值

nocycle            --是否循环

cache 10;          --预存



insert into subject values(seq_subject.nextval,'Java',99.5);

insert into subject values(seq_subject.nextval,'Python',100.0);

insert into subject values(seq_subject.nextval,'C#',97.5);


--select * from subject





4、在数据库中创建一个车类表(cid,cname,cp车牌号,describe描述)

--describe:描述

--drop table car

create table car(

    cid number primary key,

    cname varchar2(30) not null,

   cp varchar2(30) not null,

   describe varchar2(30) not null

);



--drop sequence seq_car

create sequence seq_car

start with 1       --起始值是1

increment by 1     --增长的值   

maxvalue 999999999 --序列号的最大值

minvalue 1         --序列号的最小值

nocycle            --是否循环

cache 10;          --预存



insert into car values(seq_car.nextval,'Java牌汽车','琼A17052','酷车');

insert into car values(seq_car.nextval,'Python牌汽车','赣B17053','帅车');

insert into car values(seq_car.nextval,'C#牌汽车','沪C17054','快车');


--select * from car



5、在mybatis框架中,对上面的三个表进行下面的操作:

selectAll

selectById

add

update

deleteById

要写三个bean,三个sqlMap.xml文件和一个测试类


package com.SSHC.bean;

/*id number primary key,

    name varchar2(30) not null,

   color varchar2(30) not null*/

public class Pet {

    private Integer id;

    private String name;

    private String color;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

   


}

<?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">

<!-- namespace就是空间名,它必须在整个项目中都是唯一的 -->

<mapper namespace="com.SSHC.dao.PetDao">

    <!-- id必须是唯一的 -->

    <!-- 创建一个List<Pet>集合,变量名叫rmPet -->

    <resultMap type="com.SSHC.bean.Pet" id="rmPet">

        <!-- Pet表的主键是id -->

        <!-- property指的是Pet类的属性名,

             column指的是Pet表的列名 -->

        <!-- u.setId(rs.getInt("ID")) -->

        <id property="id" column="ID" />

        <!-- u.setAct(rs.getInt("ACT")) -->

    <result property="name" column="NAMET"/>

    <result property="color" column="COLOR"/>

   

    </resultMap>

    <!-- public List<Pet>selectAll() -->

    <select id="selectAll" resultMap="rmPet">

        select * from Pet

    </select>

    

    <!-- public Pet selectById(Integer id) -->

    <!-- mybatis框架中,占位符?使用#{}来代替 -->

    <select id="selectById" resultMap="rmPet">

        select * from Pet where id = #{id}

    </select>

    

    <!-- public Integer add(Pet u) -->

    <!-- 因为参数的类型是Pet,所以占位符中的字符串就必须是对应的属性名

         (属性名区分大小写)

         

         com.SSHC.bean.Pet传的是Text文件中赋值给Pet类后传

         过来的类,比较常用和方便 -->

    <insert id="add" parameterType="com.SSHC.bean.Pet">

        insert into Pet values(#{id},#{name},#{color})

    </insert>

    <update id="update" parameterType="com.SSHC.bean.Pet">

        update pet set name=#{name},color=#{color} where id=#{id}

    </update>

     <delete id="deleteById" parameterType="com.SSHC.bean.Pet">

        delete from pet where id=#{id}

    </delete>

</mapper>




package Text;

import java.io.IOException;

import java.io.Reader;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;


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 com.SSHC.bean.Pet;



public class PetAddTest {


public static void main(String[] args) {


//获取主配置文件的路径

String path = "mybatis.xml";

//读取mybatis.xml中的配置信息,就是读取四大连接字符串的内容

Reader config;

try {

config = Resources.getResourceAsReader(path);

SqlSessionFactory factory = 

new SqlSessionFactoryBuilder().build(config);

//数据库的操作对象session

SqlSession session = factory.openSession();

//调用selectAll

//执行路径就是映射文件的namespace属性+'.'+id

String exePath = null;








// exePath = "com.SSHC.dao.PetDao.selectAll";

// List<Pet>list = session.selectList(exePath);

// for(Pet u : list) {

// System.out.println(u.getAct());

// }


// exePath = "com.SSHC.dao.PetDao.selectById";

// Pet u = session.selectOne(exePath,8);

// System.out.println(u.getAct());


exePath = "com.SSHC.dao.PetDao.add";

Pet u = new Pet();

u.setId(4);

u.setName("帅哥猫");

u.setColor("红色");


Integer count = session.insert(exePath,u);

//新增修改和删除一定记得提交事务

session.commit();

System.out.println(count);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}


package Text;

import java.io.IOException;

import java.io.Reader;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;


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 com.SSHC.bean.Pet;



public class PetDeleteByIdTest {


public static void main(String[] args) {


//获取主配置文件的路径

String path = "mybatis.xml";

//读取mybatis.xml中的配置信息,就是读取四大连接字符串的内容

Reader config;

try {

config = Resources.getResourceAsReader(path);

SqlSessionFactory factory = 

new SqlSessionFactoryBuilder().build(config);

//数据库的操作对象session

SqlSession session = factory.openSession();

//调用selectAll

//执行路径就是映射文件的namespace属性+'.'+id

String exePath = null;

// exePath = "com.SSHC.dao.PetDao.selectAll";

// List<Pet>list = session.selectList(exePath);

// for(Pet u : list) {

// System.out.println("编号是"+u.getId()

// +";名字是"+u.getName()

// +";颜色是 "+u.getColor());

// }


// exePath = "com.SSHC.dao.PetDao.selectById";

// Pet u = session.selectOne(exePath,1);

// System.out.println("编号是"+u.getId()

// +";名字是"+u.getName()

// +";颜色是 "+u.getColor());


exePath = "com.SSHC.dao.PetDao.deleteById";

Pet u = new Pet();

u.setId(7);



Integer count = session.update(exePath,u);

// 新增修改和删除一定记得提交事务


session.commit();

System.out.println(count);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}

package Text;

import java.io.IOException;

import java.io.Reader;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;


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 com.SSHC.bean.Pet;



public class PetSelectAllTest {


public static void main(String[] args) {


//获取主配置文件的路径

String path = "mybatis.xml";

//读取mybatis.xml中的配置信息,就是读取四大连接字符串的内容

Reader config;

try {

config = Resources.getResourceAsReader(path);

SqlSessionFactory factory = 

new SqlSessionFactoryBuilder().build(config);

//数据库的操作对象session

SqlSession session = factory.openSession();

//调用selectAll

//执行路径就是映射文件的namespace属性+'.'+id

String exePath = null;

exePath = "com.SSHC.dao.PetDao.selectAll";

List<Pet>list = session.selectList(exePath);

for(Pet u : list) {

System.out.println("编号是"+u.getId()

+";名字是"+u.getName()

+";颜色是 "+u.getColor());

}


// exePath = "com.SSHC.dao.PetDao.selectById";

// Pet u = session.selectOne(exePath,8);

// System.out.println(u.getAct());


// exePath = "com.SSHC.dao.PetDao.add";

// Pet u = new Pet();

// u.setId(4);

// u.setName("帅哥猫");

// u.setColor("红色");

//

// Integer count = session.insert(exePath,u);

//新增修改和删除一定记得提交事务


// session.commit();

// System.out.println(count);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}

package Text;

import java.io.IOException;

import java.io.Reader;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;


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 com.SSHC.bean.Pet;



public class PetSelectByIdTest {


public static void main(String[] args) {


//获取主配置文件的路径

String path = "mybatis.xml";

//读取mybatis.xml中的配置信息,就是读取四大连接字符串的内容

Reader config;

try {

config = Resources.getResourceAsReader(path);

SqlSessionFactory factory = 

new SqlSessionFactoryBuilder().build(config);

//数据库的操作对象session

SqlSession session = factory.openSession();

//调用selectAll

//执行路径就是映射文件的namespace属性+'.'+id

String exePath = null;

// exePath = "com.SSHC.dao.PetDao.selectAll";

// List<Pet>list = session.selectList(exePath);

// for(Pet u : list) {

// System.out.println("编号是"+u.getId()

// +";名字是"+u.getName()

// +";颜色是 "+u.getColor());

// }


exePath = "com.SSHC.dao.PetDao.selectById";

Pet u = session.selectOne(exePath,1);

System.out.println("编号是"+u.getId()

+";名字是"+u.getName()

+";颜色是 "+u.getColor());


// exePath = "com.SSHC.dao.PetDao.add";

// Pet u = new Pet();

// u.setId(4);

// u.setName("帅哥猫");

// u.setColor("红色");

//

// Integer count = session.insert(exePath,u);

//新增修改和删除一定记得提交事务


// session.commit();

// System.out.println(count);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}

package Text;

import java.io.IOException;

import java.io.Reader;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;


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 com.SSHC.bean.Pet;



public class PetUpdateTest {


public static void main(String[] args) {


//获取主配置文件的路径

String path = "mybatis.xml";

//读取mybatis.xml中的配置信息,就是读取四大连接字符串的内容

Reader config;

try {

config = Resources.getResourceAsReader(path);

SqlSessionFactory factory = 

new SqlSessionFactoryBuilder().build(config);

//数据库的操作对象session

SqlSession session = factory.openSession();

//调用selectAll

//执行路径就是映射文件的namespace属性+'.'+id

String exePath = null;

while(true){


System.out.print("请选择操作:1.selectAll "

+ ",2.selectById,"

+ "3.add,"

+ "4.update,"

+ "5.deleteById");

   Scanner input = new Scanner(System.in);

   int num = input.nextInt();

   System.out.println(num);

String IdNow="";



if(num==1){

exePath = "com.SSHC.dao.PetDao.selectAll";

List<Pet>list = session.selectList(exePath);

for(Pet u : list) {

System.out.println("编号是"+u.getId()

+";名字是"+u.getName()

+";颜色是 "+u.getColor());

IdNow=IdNow+"  "+u.getId();

}

}

if(num==2){


//查询所有 START

exePath = "com.SSHC.dao.PetDao.selectAll";

List<Pet>list = session.selectList(exePath);

for(Pet u1 : list) {

// System.out.println("编号是"+u1.getId()

// +";名字是"+u1.getName()

// +";颜色是 "+u1.getColor());

IdNow=IdNow+"  "+u1.getId();

}

System.out.print("请选择现有id:"+IdNow);

   Scanner input1 = new Scanner(System.in);

   int num1 = input1.nextInt();

//    System.out.println(num1);


//查询所有 END



exePath = "com.SSHC.dao.PetDao.selectById";

Pet u = session.selectOne(exePath,num1);

System.out.println("编号是"+u.getId()

+";名字是"+u.getName()

+";颜色是 "+u.getColor());

}

if(num==3){

//查询所有 START

exePath = "com.SSHC.dao.PetDao.selectAll";

List<Pet>list = session.selectList(exePath);

for(Pet u1 : list) {

System.out.println("编号是"+u1.getId()

+";名字是"+u1.getName()

+";颜色是 "+u1.getColor());

IdNow=IdNow+"  "+u1.getId();

}



System.out.print("请选择没出现的id");

   Scanner input1 = new Scanner(System.in);

   int num1 = input1.nextInt();

//    System.out.println(num1);

//查询所有 END


exePath = "com.SSHC.dao.PetDao.add";

Pet u = new Pet();






u.setId(num1);

u.setName("帅哥好猫");

u.setColor("红色");

Integer count = session.insert(exePath,u);

session.commit();

System.out.println(count);

if(count==1){System.out.println("添加成功!");}

else{System.out.println("添加失败!");}

//查询所有 START

exePath = "com.SSHC.dao.PetDao.selectAll";

List<Pet>list1 = session.selectList(exePath);

for(Pet u1 : list1) {

System.out.println("编号是"+u1.getId()

+";名字是"+u1.getName()

+";颜色是 "+u1.getColor());

IdNow=IdNow+"  "+u1.getId();

}

//查询所有 END


}


if(num==4){

exePath = "com.SSHC.dao.PetDao.update";

Pet u = new Pet();

u.setId(1);

u.setName("帅哥好猫");

u.setColor("红色");

Integer count = session.update(exePath,u);

session.commit();

System.out.println(count);

}

if(num==5){


//查询所有 START

exePath = "com.SSHC.dao.PetDao.selectAll";

List<Pet>list = session.selectList(exePath);

for(Pet u1 : list) {

System.out.println("编号是"+u1.getId()

+";名字是"+u1.getName()

+";颜色是 "+u1.getColor());

IdNow=IdNow+"  "+u1.getId();

}

System.out.print("请选择现有id:"+IdNow);

   Scanner input1 = new Scanner(System.in);

   int num1 = input1.nextInt();

//    System.out.println(num1);


//查询所有 END



exePath = "com.SSHC.dao.PetDao.deleteById";

Pet u = new Pet();

u.setId(num1);


Integer count = session.update(exePath,u);

session.commit();

System.out.println(count);


if(count==1){System.out.println("删除成功!");}

else{System.out.println("删除失败!");}

//查询所有 START

exePath = "com.SSHC.dao.PetDao.selectAll";

List<Pet>list1 = session.selectList(exePath);

for(Pet u1 : list1) {

System.out.println("编号是"+u1.getId()

+";名字是"+u1.getName()

+";颜色是 "+u1.getColor());

IdNow=IdNow+"  "+u1.getId();

}

//查询所有 END


}

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}

package ZSGCText;

import java.io.IOException;

import java.io.Reader;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;


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 com.SSHC.bean.Pet;



public class PeZSGCTest {


public static void main(String[] args) {


//获取主配置文件的路径

String path = "mybatis.xml";

//读取mybatis.xml中的配置信息,就是读取四大连接字符串的内容

Reader config;

try {

config = Resources.getResourceAsReader(path);

SqlSessionFactory factory = 

new SqlSessionFactoryBuilder().build(config);

//数据库的操作对象session

SqlSession session = factory.openSession();

//调用selectAll

//执行路径就是映射文件的namespace属性+'.'+id

String exePath = null;

// exePath = "com.SSHC.dao.PetDao.selectAll";

// List<Pet>list = session.selectList(exePath);

// for(Pet u : list) {

// System.out.println("编号是"+u.getId()

// +";名字是"+u.getName()

// +";颜色是 "+u.getColor());

// }


// exePath = "com.SSHC.dao.PetDao.selectById";

// Pet u = session.selectOne(exePath,1);

// System.out.println("编号是"+u.getId()

// +";名字是"+u.getName()

// +";颜色是 "+u.getColor());


exePath = "com.SSHC.dao.PetDao.update";

Pet u = new Pet();

u.setId(1);

u.setName("帅哥好猫");

u.setColor("红色");


Integer count = session.update(exePath,u);

// 新增修改和删除一定记得提交事务


session.commit();

System.out.println(count);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}


<?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>   

    <environments default="oracleConf">                 

        <!-- oracle配置 --> 

        <environment id="oracleConf">  

            <transactionManager type="JDBC"> 

                <property name="closeConnection" value="false"/>

            </transactionManager> 

            <!-- 配置数据源 -->       

            <dataSource type="POOLED">

                <property name="driver" value="oracle.jdbc.driver.OracleDriver"/>   

                <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/> 

                <property name="username" value="X"/> 

                <property name="password" value="sshcPwd"/>  

            </dataSource>    

        </environment>

    </environments> 

    <!-- 实体映射文件集合 --> 

    <mappers>

        <mapper resource="com/SSHC/bean/PetSqlMap.xml"/>

    </mappers>

</configuration>




作业 END


Oracle作业:JDBC连接数据库增删改查,搭建一个Mybatis框架,创建一个表【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律