五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Oracle,Mybatis框架例子,對正向映射和反向映射,反射和映射關系的理解【詩書畫唱】

2021-03-12 19:15 作者:詩書畫唱  | 我要投稿

概括:

實現(xiàn)Mybatis框架的嵌套效果的關鍵

1、在mybatis項目中引入Log4j

2、實現(xiàn)客戶(id,name,Sex)和地址(id,addr,cid)的正向映射和反向映射。

3、實現(xiàn)商品(id,name,price,typeId)和類型(id,tname)的正向映射和反向映射。




實現(xiàn)Mybatis框架的嵌套效果的關鍵 START


第一步:創(chuàng)建數(shù)據(jù)庫的表,插入數(shù)據(jù)

第一步:創(chuàng)建數(shù)據(jù)庫的表,插入數(shù)據(jù)
property:性質

其實映射就是代替了JDBC的功能,嵌套查詢的話就是按自己的需求設置配置文件等等就可以。





實現(xiàn)Mybatis框架的嵌套效果的關鍵 END




作業(yè) START

1、在mybatis項目中引入Log4j

2、實現(xiàn)客戶(id,name,Sex)和地址(id,addr,cid)的正向映射和反向映射。


?--drop table customer? ? ? ? ? ? ? ??

create table customer(

? ? id number primary key,

? ? name varchar2(30) not null,

Sex varchar2(30) not null

);


--drop sequence seq_customer

create sequence seq_customer

start with 1? ? ? ?--起始值是1

increment by 1? ? ?--增長的值? ?

maxvalue 999999999 --序列號的最大值

minvalue 1? ? ? ? ?--序列號的最小值

nocycle? ? ? ? ? ? --是否循環(huán)

cache 10;? ? ? ? ? --預存



insert into customer values(seq_customer.nextval,'A名字','男');

insert into customer values(seq_customer.nextval,'B名字','女');

insert into customer values(seq_customer.nextval,'C名字','男');


--select * from customer




?--drop table address? ? ? ? ? ? ? ??

create table address(

? ? id number primary key,

? ?addr varchar2(30) not null,

cid varchar2(30) not null

);


--drop sequence seq_address

create sequence seq_address

start with 1? ? ? ?--起始值是1

increment by 1? ? ?--增長的值? ?

maxvalue 999999999 --序列號的最大值

minvalue 1? ? ? ? ?--序列號的最小值

nocycle? ? ? ? ? ? --是否循環(huán)

cache 10;? ? ? ? ? --預存



insert into address values(seq_address.nextval,'江西的收貨地址',1);

insert into address values(seq_address.nextval,'廣東的收貨地址',2);

insert into address values(seq_address.nextval,'浙江的收貨地址',3);

insert into address values(seq_address.nextval,'湖南的收貨地址',1);

insert into address values(seq_address.nextval,'北京的收貨地址',2);

insert into address values(seq_address.nextval,'上海的收貨地址',3);

--select * from address

package com.SSHC.bean;


import java.util.List;


public class Address {

/*id,addr,cid*/

private Integer id;

private String addr;

private Integer cid;

/*下面的就是關聯(lián)元素:

?* (本來我是要命名為更有明確的意思的CustomerClass,

?* 但是為了提高書寫的效率的話,我就是會命名為簡介的

?* c(取頭字母或2個字母的組合)*/

private Customer c;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getAddr() {

return addr;

}

public void setAddr(String addr) {

this.addr = addr;

}

public Integer getCid() {

return cid;

}

public void setCid(Integer cid) {

this.cid = cid;

}

public Customer getC() {

return c;

}

public void setC(Customer c) {

this.c = c;

}


}

package com.SSHC.bean;


import java.util.ArrayList;

import java.util.List;

/*擁有可能多個所屬“一”的“多”的“一”,這個部分就是要

?*聲明一個裝著 所屬“一”的“多”的集合。

?*個人理解:"一”對“多”的關系就是主從關系。

?*"一"就是"主","多"是"從"。*/

public class Customer {

private Integer id;

private String name;

private String Sex;

/*下面的元素(list)就是反向的關聯(lián)元素:*/

private List<Address>list;



/*id number primary key,

name varchar2(30) not null,

Sex varchar2(30) not null*/

public Integer getId() {

return id;

}





public List<Address> getList() {

return list;

}





public void setList(List<Address> list) {

this.list = list;

}





public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return Sex;

}

public void setSex(String Sex) {

this.Sex = Sex;

}


}

<?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.SSHC.dao.AddressDao">

? ?

? <resultMap type="Address" id="rmAddress">

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

? ? <result property="addr" column="ADDR"/>

? ? <result property="cid" column="CID"/>

? ? <!-- 個人理解:習慣上property和column的值是要一樣的,

? ? 前者是小寫的Address中的要遍歷出來的內(nèi)容的列名,

? ? 后者是大寫的。

? ? 要讀取遍歷出的列名就是要使用result標簽遍歷出來。

? ?

? association中一般是property=

? "當前xml文件對應的實體類中命名的屬性名"

? ?column="當前xml文件對應的實體類中命名的編號的命名"

? ? -->

? ? <association property="c" column="CID"

? ? ? ? select="com.SSHC.dao.CustomerDao.selectById">

? ? ? ? <id property="cid" column="cID" />

? ? <result property="name" column="NAME"/>

? ? <result property="Sex" column="Sex"/>

? ? </association>

? ? <!-- 個人的理解:association就是關聯(lián)的元素,

? ? 所以這個部分的內(nèi)容是必不可少的。

? ?

property:性質。

大家發(fā)現(xiàn)了嗎?其實

property的值都是和bean包下的Address中的屬性名對應的,

column為大寫的屬性名。

result標簽的屬性取值都是當前xml文件相關的bean包下的實體類

的屬性名。


? ? ?-->

? ?

? ?

? ? </resultMap>

? ??

? ??

? ? <!-- public Address selectById(Integer id) -->

? ? <select id="selectById" resultMap="rmAddress">

? ? ? ? select * from Address where cid = #{cid}

? ? </select>

? ?

? ? <select id="selectAll" resultMap="rmAddress">

? ? ? ? select * from Address

? ? </select>

</mapper>





<?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.SSHC.dao.CustomerDao">

?<!-- 映射文件部分的配置 START

?

? property指的就是Customer屬性中的關系屬性名?

? ? javaType指的就是plist的類型是一個ArrayList

? ? column指的就是商品類型id取自Customer表中的哪個列

? ? ofType指的就是plist中存放的對象類型

? ? select指的就是需要執(zhí)行的查詢語句-->

? ? <resultMap type="Customer" id="rmCustomer">

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

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

? ??

? ? <result property="Sex" column="Sex"/>

? ? <!-- 其實column就是對應的當前的表的列名,

? ? 如果你在Oracle中查看表的數(shù)據(jù)的話就是會發(fā)現(xiàn)

? ? ,你建表是小寫的列名,但是

? ? oracl查詢出來時的列名全部是大寫的。

? ? property和column是對應的關系,

? ? 就是Java的實體類中的屬性和數(shù)據(jù)庫中對應的列名的關系。

? ? ? ofType="Address"其實我有時理解為

? ? ? 要數(shù)據(jù)庫中查詢的類型是Address表中的內(nèi)容

? ? ? -->

? ? <collection property="list" javaType="ArrayList"?

? ? column="ID"

? ? ? ? ofType="Address"?

? ? ? ? select="com.SSHC.dao.AddressDao.selectById">

? ? </collection>

? ? </resultMap>

?<!--? 映射文件部分的配置 END -->

? ??

? ? <!-- public List<Customer>selectAll() -->

? ? <select id="selectAll" resultMap="rmCustomer">

? ? ? ? select * from Customer

? ? </select>

? ? <!-- public List<Customer>selectByTid(Integer tid) -->

? ? <select id="selectById" resultMap="rmCustomer">

? ? ? ? select * from Customer where id= #{id}

? ? </select>

</mapper>


/** CTRL+F:

?* 在使用Mybatis框架前要用的JDBC的代碼,使用Mybatis框架后

就不必使用


下面的內(nèi)容是通過for的嵌套的遍歷打印出

2個表的內(nèi)容

?* */



package ZSGC;




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.Address;

import com.SSHC.bean.Customer;

import com.SSHC.bean.Userinfo;



public class CustomerAddressTest {


public static void main(String[] args) {

// TODO Auto-generated method stub


//為了獲取主配置文件的路徑而去聲明一個path的變量:

String path = "mybatis.xml";

//讀取mybatis.xml中的配置信息,就是讀取四大連接字符串的內(nèi)容:

Reader config;

try {

config = Resources.getResourceAsReader(path);

SqlSessionFactory factory =?

new SqlSessionFactoryBuilder().build(config);

//數(shù)據(jù)庫的操作對象session(這個是自己命名的):

SqlSession session = factory.openSession();

//調用selectAll

//執(zhí)行路徑就是映射文件的namespace屬性+'.'+id





/**下面的內(nèi)容是通過for的嵌套的遍歷打印出

2個表的內(nèi)容 START */

String exePath = null;

/*一個人可以對應多個地址(比如快遞中的運用,

就像是一個商品類型可以

?對應多個商品。進行一些類比的運用。*/

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

List<Customer>ls = session.selectList(exePath);

for(Customer c : ls){

System.out.println(c.getName());

/*先打印出“一”的屬性,但其實按自己的需求來。*/



List<Address>list = c.getList();

for(Address p : list) {

System.out.println(p.getAddr());

/*后面的話就是打印出“多”的屬性。

* */

}

}

/**下面的內(nèi)容是通過for的嵌套的遍歷打印出

2個表的內(nèi)容 END */



} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}


oracle_drivername=oracle.jdbc.driver.OracleDriver

oracle_url=jdbc:oracle:thin:@localhost:1521:orcl

oracle_username=X

oracle_password=sshcPwd


mysql_drivername=com.mysql.jdbc.Driver

mysql_url=jdbc:mysql://localhost:3306/j190802?useUnicode=true&amp;characterEncoding=GBK2312

mysql_username=root

mysql_password=1


sqlserver_drivername=com.microsoft.sqlserver.jdbc.SQLServerDriver

sqlserver_url=jdbc:sqlserver://localhost:1433;databaseName=cervs

sqlserver_username=sa

sqlserver_password=

log4j.rootLogger=DEBUG,Console

#Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.layout=org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=%d[%t] %-5p [%c] - %m%n

log4j.logger.java.sql.ResultSet=INFO

log4j.logger.org.apache=INFO

log4j.logger.java.sql.Connection=DEBUG

log4j.logger.java.sql.Statement=DEBUG

log4j.logger.java.sql.PreparedStatement=DEBUG


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

? ? <!-- 讀取指定的properties文件中的內(nèi)容 -->

? ? <properties resource="db.properties"></properties>?

? ? <!-- 給類取一個簡短的別名 -->

? ? <typeAliases>

? ? ? ? <package name="com.SSHC.bean"/>

? ? </typeAliases>

? ? <environments default="oracleConf">? ? ? ? ? ? ? ? ?

? ? ? ? <!-- oracle配置 -->?

? ? ? ? <environment id="oracleConf">??

? ? ? ? ? ? <transactionManager type="JDBC">?

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

? ? ? ? ? ? </transactionManager>?

? ? ? ? ? ? <!-- 配置數(shù)據(jù)源 -->? ? ? ?

? ? ? ? ? ? <dataSource type="POOLED">

? ? ? ? ? ? ? ? <property name="driver" value="${oracle_drivername}"/>? ?

? ? ? ? ? ? ? ? <property name="url" value="${oracle_url}"/>?

? ? ? ? ? ? ? ? <property name="username" value="${oracle_username}"/>?

? ? ? ? ? ? ? ? <property name="password" value="${oracle_password}"/>??

? ? ? ? ? ? </dataSource>? ??

? ? ? ? </environment>

? ? ? ? <!-- mysql配置 -->

? ? ? ? <environment id="mysqlConf">

? ? ? ? ? ? <!-- 事務配置 -->

? ? ? ? ? ? <transactionManager type="JDBC">?

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

? ? ? ? ? ? </transactionManager>?

? ? ? ? ? ? <!-- 配置數(shù)據(jù)源 -->? ? ? ?

? ? ? ? ? ? <dataSource type="POOLED">

? ? ? ? ? ? ? ? <property name="driver" value="${mysql_drivername}"/>? ?

? ? ? ? ? ? ? ? <property name="url" value="${mysql_url}"/>?

? ? ? ? ? ? ? ? <property name="username" value="${mysql_username}"/>?

? ? ? ? ? ? ? ? <property name="password" value="${mysql_password}"/>??

? ? ? ? ? ? </dataSource>

? ? ? ? </environment>

? ? </environments>?

? ? <!-- 實體映射文件集合 -->?

? ? <mappers>

? ? ? ? <!-- 告訴mybatis框架,映射文件放在什么地方 -->

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

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

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

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

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

? ? </mappers>

</configuration>



3、實現(xiàn)商品(id,name,price,typeId)和類型(id,tname)的正向映射和反向映射。




?--drop table Product? ? ? ? ? ? ? ??

create table Product(

? ? id number primary key,

? ? pname varchar2(30) not null,

? ?price? number(10,2),

? ptype? number

);


--drop sequence seq_Product

create sequence seq_Product

start with 1? ? ? ?--起始值是1

increment by 1? ? ?--增長的值? ?

maxvalue 999999999 --序列號的最大值

minvalue 1? ? ? ? ?--序列號的最小值

nocycle? ? ? ? ? ? --是否循環(huán)

cache 10;? ? ? ? ? --預存



insert into Product values(seq_Product.nextval,'黑筆',1.5,1);

insert into Product values(seq_Product.nextval,'紅書',2.0,2);

insert into Product values(seq_Product.nextval,'掛面',3.0,3);

insert into Product values(seq_Product.nextval,'藍筆',1.5,1);

insert into Product values(seq_Product.nextval,'黃書',2.0,2);

insert into Product values(seq_Product.nextval,'拉面',3.0,3);

--select * from Product





?--drop table Protype? ? ? ? ? ? ? ??

create table Protype(

? ? id number primary key,

? ? tname varchar2(30) not null

?

);


--drop sequence seq_Protype

create sequence seq_Protype

start with 1? ? ? ?--起始值是1

increment by 1? ? ?--增長的值? ?

maxvalue 999999999 --序列號的最大值

minvalue 1? ? ? ? ?--序列號的最小值

nocycle? ? ? ? ? ? --是否循環(huán)

cache 10;? ? ? ? ? --預存



insert into Protype values(seq_Protype.nextval,'筆');

insert into Protype values(seq_Protype.nextval,'書');

insert into Protype values(seq_Protype.nextval,'面');


--select * from Protype

package com.SSHC.bean;

//一對多中的一:

public class Product {

? ? private Integer id;

? ? private String pname;

? ? private Double price;

? ? private Integer ptype;

? ? /*pt就是關聯(lián)屬性(數(shù)據(jù)庫沒pt這一列):

?也就是聲明了另一個類,一般是在作為“一”的對象中,聲明出

?作為“多”的對象這個類。pt就是ptype的縮寫,如果不熟的話就是

?命名具體一些,如果很熟了就簡短有意義的命名。*/

? ? private Protype pt;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getPname() {

return pname;

}

public void setPname(String pname) {

this.pname = pname;

}

public Double getPrice() {

return price;

}

public void setPrice(Double price) {

this.price = price;

}

public Integer getPtype() {

return ptype;

}

public void setPtype(Integer ptype) {

this.ptype = ptype;

}

public Protype getPt() {

return pt;

}

public void setPt(Protype pt) {

this.pt = pt;

}

}

package com.SSHC.bean;


import java.util.List;


public class Protype {

? ? private Integer id;

? ? private String tname;

? ??

? ? //反向關聯(lián)屬性

? ? private List<Product>plist;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getTname() {

return tname;

}

public void setTname(String tname) {

this.tname = tname;

}

public List<Product> getPlist() {

return plist;

}

public void setPlist(List<Product> plist) {

this.plist = plist;

}

}



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

? ? <!-- id必須是唯一的 -->

? ? <resultMap type="Product" id="rmProduct">

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

? ? <result property="pname" column="PNAME"/>

? ? <result property="price" column="PRICE"/>

? ? <result property="ptype" column="PTYPE"/>

? ? <!-- 配置一對多的關系映射 -->

? ? <!--

? ? ? ?property就是指的Product對應的唯一的商品類型對象

? ? ? ?column就是指從Product表中查詢的商品類型id取自Product表的哪個列

? ? ? ?select就是指需要執(zhí)行的商品類型的查詢語句

? ? -->

? ? <association property="pt" column="PTYPE"

? ? ? ? select="com.SSHC.dao.ProtypeDao.selectById" >

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

? ? ? ? <result property="tname" column="TNAME" />

? ? </association>

? ? </resultMap>

? ? <!-- public List<Product>selectAll() -->

? ? <select id="selectAll" resultMap="rmProduct">

? ? ? ? select * from product

? ? </select>

? ? <!-- public List<Product>selectByTid(Integer tid) -->

? ? <select id="selectByTid" resultMap="rmProduct">

? ? ? ? select * from product where ptype = #{tid}

? ? </select>

</mapper>




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

? ? <!-- id必須是唯一的 -->

? ? <resultMap type="Product" id="rmProduct">

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

? ? <result property="pname" column="PNAME"/>

? ? <result property="price" column="PRICE"/>

? ? <result property="ptype" column="PTYPE"/>

? ? <!-- 配置一對多的關系映射 -->

? ? <!--

? ? ? ?property就是指的Product對應的唯一的商品類型對象

? ? ? ?column就是指從Product表中查詢的商品類型id取自Product表的哪個列

? ? ? ?select就是指需要執(zhí)行的商品類型的查詢語句

? ? -->

? ? <association property="pt" column="PTYPE"

? ? ? ? select="com.SSHC.dao.ProtypeDao.selectById" >

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

? ? ? ? <result property="tname" column="TNAME" />

? ? </association>

? ? </resultMap>

? ? <!-- public List<Product>selectAll() -->

? ? <select id="selectAll" resultMap="rmProduct">

? ? ? ? select * from product

? ? </select>

? ? <!-- public List<Product>selectByTid(Integer tid) -->

? ? <select id="selectByTid" resultMap="rmProduct">

? ? ? ? select * from product where ptype = #{tid}

? ? </select>

</mapper>

/** CTRL+F:

?* 在使用Mybatis框架前要用的JDBC的代碼,使用Mybatis框架后

就不必使用


下面的內(nèi)容是通過for的嵌套的遍歷打印出

2個表的內(nèi)容

?* */



package ZSGC;




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.Product;

import com.SSHC.bean.Protype;

import com.SSHC.bean.Userinfo;



public class Test {


public static void main(String[] args) {

// TODO Auto-generated method stub

/** 在使用Mybatis框架前要用的JDBC的代碼,使用Mybatis框架后

就不必使用 START*/

//? ? ? ? String drivername = "oracle.jdbc.driver.OracleDriver";

//? ? ? ? //oracle數(shù)據(jù)庫的默認端口號1521

//? ? ? ? //連接的數(shù)據(jù)庫名字是orcl數(shù)據(jù)庫

//? ? ? ? String url = "jdbc:oracle:thin:@localhost:1521:orcl";

//? ? ? ? String username = "j190802";

//? ? ? ? String pwd = "orcl";

//? ? ? ??

//? ? ? ? Connection conn = null;

//? ? ? ? PreparedStatement pstm = null;

//? ? ? ? ResultSet rs = null;

//? ? ? ??

//? ? ? ? try {

//? ? ? ? Class.forName(drivername);

// conn = DriverManager.getConnection(url,username,pwd);

// pstm = conn.prepareStatement("select * from userinfo where id = ?");

// pstm.setInt(1, 8);

// rs = pstm.executeQuery();

// List<Userinfo>rmUserinfo = new ArrayList<Userinfo>();

// while(rs.next()) {

// Userinfo u = new Userinfo();

// //ID是userinfo表的主鍵

// u.setId(rs.getInt("ID"));

// u.setAct(rs.getString("ACT"));

// System.out.println(rs.getString("ACT"));

// u.setPwd(rs.getString("PWD"));

// u.setBirth(rs.getString("BIRTH"));

// rmUserinfo.add(u);

// }

//

// String sql = "insert into userinfo values(?,?,?,to_date(?,'yyyy-mm-dd'))";

// pstm = conn.prepareStatement(sql);

// Userinfo u = new Userinfo();

// u.setId(11);

// u.setAct("haha");

// u.setPwd("09876");

// u.setBirth("2000-7-3");

// pstm.setInt(1, u.getId());

// pstm.setString(2, u.getAct());

// pstm.setString(3, u.getPwd());

// pstm.setString(4, u.getBirth());

// int count = pstm.executeUpdate();

// System.out.println(count);

// } catch (Exception e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// } finally {

// try {

// if(rs != null) {

// rs.close();

// }

// if(pstm != null) {

// pstm.close();

// }

// if(conn != null) {

// conn.close();

// }

// } catch(Exception e) {

// e.printStackTrace();

// }

// }

/** 在使用Mybatis框架前要用的JDBC的代碼,使用Mybatis框架后

就不必使用 END*/

//為了獲取主配置文件的路徑而去聲明一個path的變量:

String path = "mybatis.xml";

//讀取mybatis.xml中的配置信息,就是讀取四大連接字符串的內(nèi)容:

Reader config;

try {

config = Resources.getResourceAsReader(path);

SqlSessionFactory factory =?

new SqlSessionFactoryBuilder().build(config);

//數(shù)據(jù)庫的操作對象session(這個是自己命名的):

SqlSession session = factory.openSession();

//調用selectAll

//執(zhí)行路徑就是映射文件的namespace屬性+'.'+id

while(true){

System.out.print("請選擇操作:1.查詢"

+ "Userinfo表的所有內(nèi)容 "

+ ",2.查詢ProductDao表的所有內(nèi)容,"

+ "\n 3.嵌套查詢,"

+ "4.UserinfoDao的id查詢,"

+ "\n"

+ "5.Oracle中的UserinfoDao表的運用序列的新增,"

+ "\n 6.MySQL中的UserinfoDao表的運用自增的新增 ,"

+ "\n 7.UserinfoDao表的修改,"

+ "\n8.根據(jù)ID刪除UserinfoDao表的內(nèi)容");

? ?Scanner input = new Scanner(System.in);

? ?int num = input.nextInt();

? ?System.out.println(num);

? ?String exePath = null;

if(num==1){

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

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

for(Userinfo u : list) {

System.out.println(u.getId()+" "+u.getAct()+" "+u.getBirth()

+" "+u.getPwd());

}

}

if(num==2){

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

List<Product>lt = session.selectList(exePath);

for(Product p : lt) {

System.out.println(p.getPname());

Integer ptype = p.getPtype();

//還要進行一次查詢才可以將商品類型中文名稱查詢出來

// Protype pt = session

// .selectOne("com.SSHC.dao.ProtypeDao.selectById",ptype);

// System.out.println(pt.getTname());

System.out.println(p.getPt().getTname());

}

}

if(num==3){

/**下面的內(nèi)容是通過for的嵌套的遍歷打印出

2個表的內(nèi)容 START */

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

List<Protype>ls = session.selectList(exePath);

for(Protype pt : ls){

System.out.println(pt.getTname());

/*先打印出“多”的關系的部分*/

List<Product>plist = pt.getPlist();

/*再打印出“多”的關系的部分中聲明的裝著

* 所屬的“一”的部分:*/

for(Product p : plist) {

System.out.println(p.getPname());

}

}

/**下面的內(nèi)容是通過for的嵌套的遍歷打印出

2個表的內(nèi)容 END */

}

if(num==4){

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

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

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

}

if(num==5){

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

Userinfo u = new Userinfo();

u.setAct("詩書畫唱");

u.setPwd("999");

u.setBirth("2009-9-20");

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

//新增修改和刪除一定記得提交事務

session.commit();

System.out.println(count);

}

if(num==6){

exePath = "com.SSHC.dao.UserinfoDao.addMySQL";

Userinfo u = new Userinfo();

u.setAct("詩書畫唱");

u.setPwd("666");

u.setBirth("1999-7-7");

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

//新增修改和刪除一定記得提交事務

session.commit();

System.out.println(count);

}

if(num==7){

Userinfo u = new Userinfo();

u.setId(11);

u.setAct("測試賬號");

u.setPwd("555555");

u.setBirth("1998-11-29");

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

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

//新增修改和刪除一定記得提交事務

session.commit();

System.out.println(count);

}

if(num==8){

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

Integer count = session.delete(exePath,8);

//新增修改和刪除一定記得提交事務

session.commit();

System.out.println(count);

}

}} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}

(代碼見上)







作業(yè) END



個人對正向映射和反向映射,反射和映射的關系的理解 START

在一實體類A中聲明另一個實體類B就是正向映射。

在B實體類中聲明A實體類為集合的泛型的ArrayList等集合就是向映射。

怎么區(qū)分正向映射和反向映射?其實2者是相對的關系。行業(yè)習慣上規(guī)定在一實體類A中聲明另一個實體類B就是正向映射,相對的,在B實體類中聲明A實體類為集合的泛型的ArrayList等集合就是向映射。

反射和映射的關系很大,其實映射是Mybatis框架中使用的技術,而映射技術是在反射技術的基礎上產(chǎn)生的(這些內(nèi)容是我目前個人的理解,可能有不準確的地方)。

映射可以減少JDBC代碼和Dao的部分。




個人對正向映射和反向映射,反射和映射的關系的理解 END





Oracle,Mybatis框架例子,對正向映射和反向映射,反射和映射關系的理解【詩書畫唱】的評論 (共 條)

分享到微博請遵守國家法律
密云县| 依兰县| 镇江市| 九江市| 木里| 平罗县| 巨鹿县| 平谷区| 渝中区| 德化县| 六盘水市| 增城市| 上杭县| 富顺县| 兰考县| 普陀区| 乃东县| 济源市| 潜山县| 林州市| 河北省| 磐安县| 永安市| 吉水县| 罗平县| 来安县| 阿城市| 新宁县| 静乐县| 岳阳市| 水城县| 海林市| 侯马市| 登封市| 鄱阳县| 伊春市| 土默特左旗| 井陉县| 喀什市| 阿荣旗| 余干县|