Mybatis框架映射,一對多或一對一地打印數(shù)據(jù),Oracle動態(tài)SQL處理,個人詳細注釋和單詞
本期的重點:
單詞意思
包含個人詳細注釋的例子(幾乎無障礙閱讀)
1.Mybatis框架實現(xiàn)組合查詢和分頁查詢
2.一對多地打印數(shù)據(jù),一對一地打印數(shù)據(jù)
留給讀者的作業(yè)(很容易和我提供詳細注釋的例子相似)?
--查詢userinfo表,根據(jù)act進行模糊查詢
--如果沒有輸入賬號,就查詢所有
--如果輸入了賬號,就進行模糊查詢
--查詢userinfo表,根據(jù)act和日期進行查詢
--每次查詢只使用一個條件,要么根據(jù)賬號進行查詢,
--要么根據(jù)日期進行查詢,這兩個條件是互相排斥的
--組合查詢
--act模糊查詢
--pwd精確匹配
--brith范圍查詢
--分頁查詢
--page當前頁碼
--rows每頁顯示記錄條數(shù)
--end = page * rows【end代表<=后面的數(shù)值】
--start = (page - 1) * rows + 1【start代表>=前面的數(shù)值】
包含個人詳細注釋的例子(幾乎無障礙閱讀)START

1.Mybatis框架實現(xiàn)組合查詢和分頁查詢
Mybatis框架建表示例 START
?--drop table Userinfo? ? ? ? ? ? ? ??
create table Userinfo(
? ? id number primary key,
? ? act varchar2(30) not null,
? ?pwd varchar2(30) not null,
? ?birth date
);
--drop sequence seq_Userinfo
create sequence seq_Userinfo
start with 1? ? ? ?--起始值是1
increment by 1? ? ?--增長的值? ?
maxvalue 999999999 --序列號的最大值
minvalue 1? ? ? ? ?--序列號的最小值
nocycle? ? ? ? ? ? --是否循環(huán)
cache 10;? ? ? ? ? --預存
insert into Userinfo values(seq_Userinfo.nextval,'黑黑','pwd1',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'紅紅','pwd2',to_date('2020-06-07','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'藍藍','pwd3',to_date('2020-06-08','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'詩書畫唱','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'三連','pwd5',to_date('2020-06-10','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'關注','pwd6',to_date('2020-06-11','yyyy-mm-dd'));
--select * from Userinfo
--rows=5,page=2
--end = rows * page=10
--start = (page - 1) * rows + 1=6
--start表示>=好后面的值,end表示<=號后面的值
?select * from
(select p1.*,rownum r1 from Userinfo p1
where rownum <= 10)
where r1 >=6
Mybatis框架建表示例 END



package com.SSHC.bean;
public class Userinfo {
? ? private Integer id;
? ? private String act;
? ? private String pwd;
? ? private String birth;
? ? //查詢屬性
? ? private String begin;//開始日期
? ? private String end;//截至日期
? ??
? ? //通用的分頁屬性
? ? private Integer page;//當前顯示第幾頁的數(shù)據(jù)
? ? private Integer rows;//每頁顯示的記錄條數(shù)
? ??
? ? //oracle分頁屬性
? ? private Integer pstart;
? ? private Integer pend;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAct() {
return act;
}
public void setAct(String act) {
this.act = act;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getBegin() {
return begin;
}
public void setBegin(String begin) {
this.begin = begin;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getRows() {
return rows;
}
public void setRows(Integer rows) {
this.rows = rows;
}
public Integer getPstart() {
return pstart;
}
public void setPstart(Integer pstart) {
this.pstart = pstart;
}
public Integer getPend() {
return pend;
}
public void setPend(Integer pend) {
this.pend = pend;
}
}

<?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.UserinfoDao">
? ? <!-- id必須是唯一的 -->
? ? <!-- 創(chuàng)建一個List<Userinfo>集合,變量名叫rmUserinfo -->
? ? <resultMap type="Userinfo" id="rmUserinfo">
? ? ? ? <!-- userinfo表的主鍵是id -->
? ? ? ? <!-- property指的是Userinfo類的屬性名,
? ? ? ? ? ? ?column指的是userinfo表的列名 -->
? ? ? ? <!-- u.setId(rs.getInt("ID")) -->
? ? ? ? <id property="id" column="ID" />
? ? ? ? <!-- u.setAct(rs.getInt("ACT")) -->
? ? <result property="act" column="ACT"/>
? ? <result property="pwd" column="PWD"/>
? ? <result property="birth" column="BIRTH"/>
? ? </resultMap>? ?
? ? <!-- public List<Userinfo>selectByAct(Userinfo u) -->
? ? <select id="selectByAct" resultMap="rmUserinfo"
? ? ? ? parameterType="Userinfo">
? ? ? ? select * from userinfo
? ? ? ? <if test="act != null and act.length() > 0">
? ? ? ? ? ? where act like #{act}
? ? ? ? </if>
? ? </select>
? ??
? ? <!-- public List<Userinfo> selectByOne(Userinfo u) -->
? ? <select id="selectByOne" resultMap="rmUserinfo"
? ? ? ? parameterType="Userinfo">
? ? ? ? select * from userinfo where 1 = 1
? ? ? ? <choose>
? ? ? ? ? ? <when test="act != null and act.length() > 0">
? ? ? ? ? ? ? ? and act like #{act}
? ? ? ? ? ? </when>
? ? ? ? ? ? <when test="birth != null and birth.length() > 0">
? ? ? ? ? ? ? ? and birth = to_date(#{birth},'yyyy-mm-dd')
? ? ? ? ? ? </when>
? ? ? ? ? ? <otherwise>
? ? ? ? ? ? </otherwise>
? ? ? ? </choose>
? ? </select>
? ??
? ? <!-- public List<Userinfo>selectByCond(Userinfo u) -->
? ? <select id="selectByCond" resultMap="rmUserinfo"
? ? ? ? parameterType="Userinfo">
? ? ? ? select * from userinfo
? ? ? ? <where>
? ? ? ? ? ? <if test="act != null and act.length() > 0">
? ? ? ? ? ? ? ? and act like #{act}
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="pwd != null and pwd.length() > 0">
? ? ? ? ? ? ? ? and pwd = #{pwd}
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="begin != null and begin.length() > 0">
? ? ? ? ? ? ? ? and birth >= to_date(#{begin},'yyyy-mm-dd')
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="end != null and end.length() > 0">
? ? ? ? ? ? ? ? and birth <= to_date(#{end},'yyyy-mm-dd')
? ? ? ? ? ? </if>
? ? ? ? </where>
? ? </select>
? ??
? ? <!-- public List<Userinfo>selectByPage(Userinfo u) -->
? ? <select id="selectByPage" resultMap="rmUserinfo"
? ? ? ? parameterType="Userinfo">
? ? ? ? select * from
(select t.*,rownum rn from userinfo t
where rownum <= #{pend})
where rn >= #{pstart}
? ? </select>
</mapper>

/*CTRL+F搜索詞條:config:配置
resources:資源
factory:工廠
session“會話控制”,會議,會話
分頁查詢 */
package Text;
import java.io.IOException;
import java.io.Reader;
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.Userinfo;
public class UserinfoSelect {
public static void main(String[] args) {
// TODO Auto-generated method stub
//設置主配置文件的路徑path:
String path = "mybatis.xml";
/*
讀取mybatis.xml中的配置信息,就是讀取四大連接字符串的內容
*/
Reader config;
/*config:配置*/
/*resources:資源*/
/*factory:工廠
* session“會話控制”,會議,會話*/
try {
config = Resources.getResourceAsReader(path);
SqlSessionFactory factory =?
new SqlSessionFactoryBuilder().build(config);
/*數(shù)據(jù)庫的操作對象session(session
* 就是給factory.openSession()起
的別名,我理解的意思是:“工廠開放會話”*/
SqlSession session = factory.openSession();
String exePath = null;
while(true){
System.out.print("請選擇操作:1.selectByAct"
+ ",2.selectByOne,"
+ "\n 3.selectByCond,"
+ "4.selectByPage"
+ "\n");
? ?Scanner input = new Scanner(System.in);
? ?int num = input.nextInt();
? ?System.out.println(num);
?
if(num==1){
exePath = "com.SSHC.dao.UserinfoDao.selectByAct";
Userinfo u = new Userinfo();
u.setAct("%詩書畫唱%");
List<Userinfo>list = session.selectList(exePath,u);
for(Userinfo user : list) {
System.out.println(user.getId()+" "
+user.getAct()+" "+user.getPwd()+" "
+user.getBirth());
}
}
if(num==2){
exePath = "com.SSHC.dao.UserinfoDao.selectByOne";
Userinfo u = new Userinfo();
u.setAct("%詩書畫唱%");
u.setBirth("2020-06-06");
List<Userinfo>list = session.selectList(exePath,u);
for(Userinfo user : list) {
System.out.println(user.getId()+" "
+user.getAct()+" "+user.getPwd()+" "
+user.getBirth());
}
}
if(num==3){
exePath = "com.SSHC.dao.UserinfoDao.selectByCond";
Userinfo u = new Userinfo();
//u.setAct("%詩書畫唱%");
//u.setPwd("pwd4");
u.setBegin("2020-06-01");
u.setEnd("2020-06-08");
List<Userinfo>list = session.selectList(exePath,u);
for(Userinfo user : list) {
System.out.println(user.getId()+" "
+user.getAct()+" "+user.getPwd()+" "
+user.getBirth());
}
}
if(num==4){
exePath = "com.SSHC.dao.UserinfoDao.selectByPage";
Userinfo u = new Userinfo();
/*rows=5,page=2
end = rows * page=10
start = (page - 1) * rows + 1=6
start表示>=好后面的值,end表示<=號后面的值*/
/**分頁查詢 START*/
Integer rows = 5;
Integer page = 2;
Integer start = (page - 1) * rows + 1;
Integer end = page * rows;
u.setPage(page);
u.setRows(rows);
u.setPstart(start);
u.setPend(end);
List<Userinfo>list = session.selectList(exePath,u);
for(Userinfo user : list) {
System.out.println(user.getId()+" "
+user.getAct()+" "+user.getPwd()+" "
+user.getBirth());
}
/**分頁查詢 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&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文件中的內容 -->
? ? <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/CustomSqlMap.xml"/>
? ? ? ? <mapper resource="com/SSHC/bean/AddrSqlMap.xml"/>
? ? </mappers>
</configuration>

2.一對多地打印數(shù)據(jù),一對一地打印數(shù)據(jù)
?--drop table custom? ? ? ? ? ? ? ??
create table custom(
? ? cid number primary key,
? ? name varchar2(30) not null,
SEX varchar2(30) not null
);
--drop sequence seq_custom
create sequence seq_custom
start with 1? ? ? ?--起始值是1
increment by 1? ? ?--增長的值? ?
maxvalue 999999999 --序列號的最大值
minvalue 1? ? ? ? ?--序列號的最小值
nocycle? ? ? ? ? ? --是否循環(huán)
cache 10;? ? ? ? ? --預存
insert into custom values(seq_custom.nextval,'A名字','男');
insert into custom values(seq_custom.nextval,'B名字','女');
insert into custom values(seq_custom.nextval,'C名字','男');
insert into custom values(seq_custom.nextval,'D名字','男');
--select * from custom
?--drop table addr? ? ? ? ? ? ? ??
create table addr(
? ? aid number primary key,
? ?text varchar2(30) not null,
cid varchar2(30) not null
);
--drop sequence seq_addr
create sequence seq_addr
start with 1? ? ? ?--起始值是1
increment by 1? ? ?--增長的值? ?
maxvalue 999999999 --序列號的最大值
minvalue 1? ? ? ? ?--序列號的最小值
nocycle? ? ? ? ? ? --是否循環(huán)
cache 10;? ? ? ? ? --預存
insert into addr values(seq_addr.nextval,'江西的收貨地址',1);
insert into addr values(seq_addr.nextval,'廣東的收貨地址',2);
insert into addr values(seq_addr.nextval,'浙江的收貨地址',3);
insert into addr values(seq_addr.nextval,'江西的收貨地址',1);
insert into addr values(seq_addr.nextval,'廣東的收貨地址',2);
insert into addr values(seq_addr.nextval,'浙江的收貨地址',3);
insert into addr values(seq_addr.nextval,'浙江的收貨地址',4);
--select * from addr


package com.SSHC.bean;
public class Addr {
? ? private Integer aid;
? ? private String text;
? ? private Integer cid;
? ? //關聯(lián)屬性
? ? private Custom c;
public Integer getAid() {
return aid;
}
public void setAid(Integer aid) {
this.aid = aid;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public Custom getC() {
return c;
}
public void setC(Custom c) {
this.c = c;
}
}

package com.SSHC.bean;
import java.util.List;
public class Custom {
? ? private Integer cid;
? ? private String name;
? ? private String SEX;
? ? //關聯(lián)屬性
? ? private List<Addr>al;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
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;
}
public List<Addr> getAl() {
return al;
}
public void setAl(List<Addr> al) {
this.al = al;
}
}

<?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.AddrDao">
? ? <resultMap type="Addr" id="rmAddr">
? ? ? ? <id property="aid" column="AID" />
? ? <result property="text" column="TEXT"/>
? ? <result property="cid" column="CID"/>
? ? <association property="c" column="CID"
? ? ? ? select="com.SSHC.dao.CustomDao.selectById">
? ? ? ? <id property="cid" column="CID" />
? ? <result property="name" column="NAME"/>
? ? <result property="SEX" column="SEX"/>
? ? </association>
? ? </resultMap>?
? ? <!-- public List<Addr>selectByCid(Integer cid) -->
? ? <select id="selectByCid" resultMap="rmAddr">
? ? ? ? select * from addr where cid = #{cid}
? ? </select>
? ? <!-- public List<Addr>selectAll() -->
? ? <select id="selectAll" resultMap="rmAddr">
? ? ? ? select * from addr
? ? </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.CustomDao">
? ? <resultMap type="Custom" id="rmCustom">
? ? ? ? <id property="cid" column="CID" />
? ? <result property="name" column="NAME"/>
? ? <result property="SEX" column="SEX"/>
? ? <collection property="al" javaType="ArrayList" ofType="Addr"
? ? ? ? column="CID" select="com.SSHC.dao.AddrDao.selectByCid">
? ? </collection>
? ? </resultMap>? ??
? ? <!-- public List<Custom>selectAll() -->
? ? <select id="selectAll" resultMap="rmCustom">
? ? ? ? select * from custom
? ? </select>
? ? <!-- public Custom selectById(Integer cid) -->
? ? <select id="selectById" resultMap="rmCustom">
? ? ? ? select * from custom where cid = #{cid}
? ? </select>
</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.Addr;
import com.SSHC.bean.Custom;
import com.SSHC.bean.Product;
import com.SSHC.bean.Protype;
import com.SSHC.bean.Userinfo;
public class YingShe {
public static void main(String[] args) {
// TODO Auto-generated method stub
//設置主配置文件的路徑path:
String path = "mybatis.xml";
//讀取mybatis.xml中的配置信息,就是讀取四大連接字符串的內容
Reader config;
try {
config = Resources.getResourceAsReader(path);
SqlSessionFactory factory =?
new SqlSessionFactoryBuilder().build(config);
//數(shù)據(jù)庫的操作對象session
SqlSession session = factory.openSession();
//調用selectAll
//執(zhí)行路徑就是映射文件的namespace屬性+'.'+id
String exePath = null;
while(true){
System.out.print("請選擇操作:1.一對多地打印數(shù)據(jù)"
+ ",2.一對一地打印數(shù)據(jù)");
? ?Scanner input = new Scanner(System.in);
? ?int num = input.nextInt();
? ?System.out.println(num);
?
if(num==1){
/**下面是一對多地打印出映射(逐組打印出名字和對應
的多個地址),
*/
? ? ? ? ? ? exePath = "com.SSHC.dao.CustomDao.selectAll";
? ? ? ? ? ? List<Custom>list = session.selectList(exePath);
? ? ? ? ? ? for(Custom c : list) {
? ? ? ? ? ? System.out.println(c.getName());
? ? ? ? ? ? List<Addr>al = c.getAl();
? ? ? ? ? ? for(Addr a : al) {
? ? ? ? ? ? System.out.println(a.getText());
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println();
? ? ? ? ? ? }
}
/**下面是一對一地打印出映射(逐組打印出地址和對應
? 的一個名字)
* 其實也可以一對多地打印出映射,比如上面就是
* 一對多的打印出數(shù)據(jù)(逐組打印出名字和對應
的多個地址),
* */
if(num==2){
exePath = "com.SSHC.dao.AddrDao.selectAll";
List<Addr>list = session.selectList(exePath);
for(Addr a : list) {
System.out.println(a.getText());
Custom c = a.getC();
if(c != null) {
System.out.println(c.getName());
}
}
// //新增修改和刪除一定記得提交事務
// session.commit();
// System.out.println(count);
} }} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


包含個人詳細注釋的例子(幾乎無障礙閱讀) END
視頻教程筆記 START
我對<;和&rt;的理解:分別是<>的left左邊(<)和right部分(>)。


視頻教程筆記 END
單詞意思 START

單詞意思 END
示例SQL START

create table custom(
? ? cid number primary key,
? ? name varchar2(30) not null,
? ? SEX char(2)
);
insert into custom values(1,'小詩','男');
insert into custom values(4,'小書','女');
insert into custom values(8,'樂畫','男');
create table addr(
? ? aid number primary key,
? ? text varchar2(50) not null,
? ? cid number
);
insert into addr values(1,'詩書畫唱學院',4);
insert into addr values(2,'詩書畫唱創(chuàng)新創(chuàng)業(yè)大樓',4);
insert into addr values(3,'五一大道7UP大廈三樓',1);
insert into addr values(4,'書院路冶金大廈一樓',8);
insert into addr values(6,'德雅路48號',8);
insert into addr values(7,'芙蓉廣場貿(mào)易大廈',8);
select * from addr where cid = 8;

--查詢userinfo表,根據(jù)act進行模糊查詢
--如果沒有輸入賬號,就查詢所有
--如果輸入了賬號,就進行模糊查詢
--查詢userinfo表,根據(jù)act和日期進行查詢
--每次查詢只使用一個條件,要么根據(jù)賬號進行查詢,
--要么根據(jù)日期進行查詢,這兩個條件是互相排斥的
--組合查詢
--act模糊查詢
--pwd精確匹配
--brith范圍查詢
--分頁查詢
--page當前頁碼
--rows每頁顯示記錄條數(shù)
--end = page * rows【end代表<=后面的數(shù)值】
--start = (page - 1) * rows + 1【start代表>=前面的數(shù)值】
select * from
(select t.*,rownum rn from userinfo t
where rownum <= end)
where rn >= start
示例SQL END
留給讀者的作業(yè)(很容易和我提供詳細注釋的例子相似)?START
一、創(chuàng)建商品信息表proinfo,包含id、商品名稱和價格兩個屬性,對proinfo表進行Mybatis配置實現(xiàn)以下的功能:
1、如果輸入了價格,就根據(jù)價格進行查詢,如果沒有輸入價格,就查詢所有。<if>標簽實現(xiàn)
2、如果輸入了商品名稱,就根據(jù)商品名稱進行模糊查詢,否則就查詢所有。<if>標簽實現(xiàn)
3、同時輸入價格和商品名稱,優(yōu)先根據(jù)商品名稱進行模糊查詢,商品名稱和價格兩個條件只能二選一。<choose><when>標簽實現(xiàn)
二、創(chuàng)建寵物信息表,包含id,寵物名字和毛色以及年齡,實現(xiàn)以下功能:
1、如果輸入了名字,就根據(jù)名字進行查詢,如果沒有輸入名字,就查詢所有。
2、如果輸入了毛色,就根據(jù)毛色進行查詢,如果么有輸入毛色,就查詢所有。
3、同時輸入名字、毛色和年齡時,查詢條件的優(yōu)先級別是名字>年齡>毛色,三個條件只能選擇一個進行查詢。
三、實現(xiàn)一個商品信息表和寵物信息表的分頁查詢和組合查詢功能