Oracle和Mybatis作業(yè):范圍,組合分頁查詢,解決項目紅色的感嘆號等問題【詩書畫唱】
內(nèi)容概括:
解決導(dǎo)入項目時報了紅色的感嘆號等的問題
(關(guān)于Mybatis框架代碼生成程序工具的使用,見上篇的專欄,有介紹)
作業(yè)和自己給的答案(含個人的詳細(xì)理解)
創(chuàng)建一個商品表,包含id、名稱、價格和上架日期四個屬性。
1、實現(xiàn)商品組合查詢功能,要求根據(jù)名稱進(jìn)行模糊查詢,根據(jù)價格和日期進(jìn)行范圍查詢。
2、實現(xiàn)商品分頁查詢。
3、實現(xiàn)商品的高級update功能。
4、請實現(xiàn)組合分頁查詢
??/*范圍查詢屬性
? ? ?* (這些和分頁屬性都是類似的,
? ? ?* 在數(shù)據(jù)庫中是沒有其實際存在的列名的
? ? ?* ,但卻在bean包的實體類中
? ? ?* 可以聲明使用):*/
?/*
--rows=5,page=2
--end = rows * page=10
--start = (page - 1) * rows + 1=6
--start表示>=號后面的值,end表示<=號后面的值
?【我這里命名的pstart就是上面公式中的start,pend就是end。
?p是page的縮寫】
? ? ?* oracle分頁屬性
? ? ?* (將用page或 rows計算來得出pstart或pend的值):*/
作業(yè)和自己給的答案(含個人的詳細(xì)理解) START
創(chuàng)建一個商品表,包含id、名稱、價格和上架日期四個屬性。
1、實現(xiàn)商品組合查詢功能,要求根據(jù)名稱進(jìn)行模糊查詢,根據(jù)價格和日期進(jìn)行范圍查詢。
2、實現(xiàn)商品分頁查詢。
3、實現(xiàn)商品的高級update功能。
4、請實現(xiàn)組合分頁查詢
?--drop table goods? ? ? ? ? ? ? ??
create table goods(
? ? id number primary key,
? ? pname varchar2(30) not null,
? ?price? number(10,2),
? shengChanRiQi? date
);
--drop sequence seq_goods
create sequence seq_goods
start with 1? ? ? ?--起始值是1
increment by 1? ? ?--增長的值? ?
maxvalue 999999999 --序列號的最大值
minvalue 1? ? ? ? ?--序列號的最小值
nocycle? ? ? ? ? ? --是否循環(huán)
cache 10;? ? ? ? ? --預(yù)存
insert into goods values(seq_goods.nextval,'黑筆',1.5,to_date('2020-06-06','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'紅書',2.0,to_date('2020-06-07','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'掛面',3.0,to_date('2020-06-08','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'藍(lán)筆',1.5,to_date('2020-06-09','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'黃書',2.0,to_date('2020-06-10','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'拉面',3.0,to_date('2020-06-11','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'拉面1',3.0,to_date('2020-06-11','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'拉面2',3.0,to_date('2020-06-11','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'拉面3',3.0,to_date('2020-06-11','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'拉面4',3.0,to_date('2020-06-11','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'拉面5',3.0,to_date('2020-06-11','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'拉面6',3.0,to_date('2020-06-11','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'拉面7',4.0,to_date('2020-06-12','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'拉面8',5.0,to_date('2020-06-13','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'拉面9',6.0,to_date('2020-06-14','yyyy-mm-dd') );
insert into goods values(seq_goods.nextval,'拉面10',7.0,to_date('2020-06-15','yyyy-mm-dd') );
select * from goods where pname like '%拉面%'?
and? ?price >=4 and price <=7?
and? shengChanRiQi >=to_date('2020-06-12','yyyy-mm-dd')
?and shengChanRiQi <=to_date('2020-06-15','yyyy-mm-dd')


/*CTRL+F:范圍查詢屬性
?* */
package com.SSHC.bean;
public class Goods {
? ? private Integer id;
? ? private String pname;
? ? private Double price;
? ? private String shengChanRiQi;
? ? /*范圍查詢屬性
? ? ?* (這些和分頁屬性都是類似的,
? ? ?* 在數(shù)據(jù)庫中是沒有其實際存在的列名的
? ? ?* ,但卻在bean包的實體類中
? ? ?* 可以聲明使用):*/
? ? private String begin;//開始日期
private String end;//截至日期
private Double PriceMin;//價格的范圍查詢中的最小值
private Double PriceMax;//價格的范圍查詢中的最大值
? ??
? ? //通用的分頁屬性:
? ? private Integer page;//當(dāng)前顯示第幾頁的數(shù)據(jù)
? ? private Integer rows;//每頁顯示的記錄條數(shù)
? ??
? ? /*
--rows=5,page=2
--end = rows * page=10
--start = (page - 1) * rows + 1=6
--start表示>=號后面的值,end表示<=號后面的值
?【我這里命名的pstart就是上面公式中的start,pend就是end。
?p是page的縮寫】
? ? ?* oracle分頁屬性
? ? ?* (將用page或 rows計算來得出pstart或pend的值):*/
? ? private Integer pstart;
? ? private Integer pend;
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 String getShengChanRiQi() {
return shengChanRiQi;
}
public void setShengChanRiQi(String shengChanRiQi) {
this.shengChanRiQi = shengChanRiQi;
}
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 Double getPriceMin() {
return PriceMin;
}
public void setPriceMin(Double priceMin) {
PriceMin = priceMin;
}
public Double getPriceMax() {
return PriceMax;
}
public void setPriceMax(Double priceMax) {
PriceMax = priceMax;
}
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">
<!-- CTRL+F:
selectByCond組合查詢,
? ? 稱進(jìn)行模糊查詢,根據(jù)價格和日期進(jìn)行范圍查詢
? ??
?價格進(jìn)行范圍查詢
? ??
?分頁查詢部分? ?
? ??
組合分頁查詢部分
?-->
<mapper namespace="com.SSHC.dao.GoodsDao">
? ? <resultMap type="Goods" id="rmGoods">
? ? ? ?
? ? ? ? <id property="id" column="ID" />
? ? ? ? <!-- u.setpname(rs.getInt("pname")) -->
? ? <result property="pname" column="pname"/>
? ? <result property="price" column="price"/>
? ? <result property="shengChanRiQi" column="shengChanRiQi"/>
? ? </resultMap>
? ? ? ?
? <!-- selectByCond組合查詢,
? ? 稱進(jìn)行模糊查詢,根據(jù)價格和日期進(jìn)行范圍查詢 START -->??
? <select id="selectByCond" resultMap="rmGoods"
? ? ? ? parameterType="Goods">
select * from
Goods
? ? <where>
? ? ? ? <if test="pname != null and pname.length() > 0">
? ? ? ? ? ? ? ? and pname like #{pname}
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="price != null and price.length() > 0">
? ? ? ? ? ? ? ? and price = #{price}
? ? ? ? ? ? </if>
? ? ? ? ? ? <!-- 日期進(jìn)行范圍查詢 START
? ? ? ? ? ? 自己聲明begin和end分別為日期的范圍查詢的最小值和最大值 -->
? ? ? ? ? ? <if test="begin != null and begin.length() > 0">
and shengChanRiQi >= to_date(#{begin},'yyyy-mm-dd')
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="end != null and end.length() > 0">
?and shengChanRiQi <= to_date(#{end},'yyyy-mm-dd')
? ? ? ? ? ? </if>?
? ? ? ? ? ??
? ? ? ? ? ??
? ? ? ? ? ? ? ?<!-- 日期進(jìn)行范圍查詢 END -->
? ? ? ? ? ? ? ?
?<!-- 價格進(jìn)行范圍查詢 START
? ? ? ? ? ? 自己聲明PriceMin和PriceMax
? ? ? ? ? ? 分別為價格的范圍查詢的最小值和最大值 -->
? ? ? ? ? ? <if test="PriceMin != null">
and Price >=#{PriceMin}
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="PriceMax != null">
?and Price <=#{PriceMax}
? ? ? ? ? ? </if>?
? ? ? ? ? ??
? ? ? ? ? ??
? ? ? ? ? ? ? ?<!-- 價格進(jìn)行范圍查詢 END --> ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?
? ? </where>?
? ? </select>?
? ??
? ??
? ? ?
? ? <!-- selectByCond組合查詢,
? ? 稱進(jìn)行模糊查詢,根據(jù)價格和日期進(jìn)行范圍查詢 END -->? ?
? ??
? ??
? ? <!-- 分頁查詢部分 START -->? ??
??
? ? <select id="selectByPage" resultMap="rmGoods"
? ? ? ? parameterType="Goods">
select * from
(select t.*,rownum rn from?
(select * from Goods
? ?
) t
where rownum <= #{pend})
where rn >= #{pstart}
? ? </select>
? ? ? ?<!-- 分頁查詢部分 END -->
? ??
? ??
? ??
? ??
? ? <!-- 組合分頁查詢部分 START -->? ??
? ? <!--public List<Goods>selectByCondAndPage(Goods u) -->
? ? <select id="selectByCondAndPage" resultMap="rmGoods"
? ? ? ? parameterType="Goods">
select * from
(select t.*,rownum rn from?
(select * from Goods
? ? <where>
? ? ? ? <if test="pname != null and pname.length() > 0">
? ? ? ? ? ? ? ? and pname like #{pname}
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="price> 0.0">
? ? ? ? ? ? ? ? and price = #{price}
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="begin != null and begin.length() > 0">
? ? ? ? ? ? ? ? and shengChanRiQi >= to_date(#{begin},'yyyy-mm-dd')
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="end != null and end.length() > 0">
? ? ? ? ? ? ? ? and shengChanRiQi <= to_date(#{end},'yyyy-mm-dd')
? ? ? ? ? ? </if>?
? ? </where>?
) t
where rownum <= #{pend})
where rn >= #{pstart}
? ? </select>
? ? ? ?<!-- 組合分頁查詢部分 END -->??
? ?
? ?
? ?
? ?
? ?<!-- 高級update功能部分 START -->
? ? <!-- public Integer updateById(Goods u) -->
? ? <update id="updateById" parameterType="Goods">
? ? ? ? update Goods
? ? ? ? <set>
? ? ? ? ? ? <if test="pname != null and pname.length() > 0">
? ? ? ? ? ? ? ? pname = #{pname},
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="price > 0">
? ? ? ? ? ? ? ? price = #{price},
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="shengChanRiQi != null and shengChanRiQi.length() > 0">
? ? ? ? ? ? ? ? shengChanRiQi = to_date(#{shengChanRiQi},'yyyy-mm-dd'),
? ? ? ? ? ? </if>
? ? ? ? </set>
where id = #{id}
? ? </update>
? ? ? <!-- 高級update功能部分 END -->?
? ??
? ??
</mapper>


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.Goods;
public class GoodsCondPageUpdate {
public static void main(String[] args) {
// TODO Auto-generated method stub
//獲取主配置文件的路徑
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();
String exePath = null;
while(true){
System.out.print("請選擇操作:"
+ "\n 1.實現(xiàn)商品組合查詢功能,"
+ "\n 要求根據(jù)名稱進(jìn)行模糊查詢,"
+ "\n 根據(jù)價格和日期進(jìn)行范圍查詢。"
+ "\n 2.實現(xiàn)商品分頁查詢。"
+ "\n 3.實現(xiàn)商品的高級update功能。"
+ "\n 4.請實現(xiàn)組合分頁查詢。");
? ?Scanner input = new Scanner(System.in);
? ?int num = input.nextInt();
? ?if(num==1){
Goods u = new Goods();
/*select * from goods where pname like '%拉面%'?
and? ?price >=4 and price <=7?
and? shengChanRiQi >=to_date('2020-06-12','yyyy-mm-dd')
?and shengChanRiQi <=to_date('2020-06-15','yyyy-mm-dd')*/
u.setPname("%拉面%");
u.setBegin("2020-06-12");
u.setEnd("2020-06-15");
u.setPriceMin(4.0);
u.setPriceMax(7.0);
exePath = "com.SSHC.dao.GoodsDao.selectByCond";
List<Goods>list = session.selectList(exePath,u);
for(Goods user : list) {
System.out.println(user.getPname()+" "
+user.getShengChanRiQi()+" "+user.getPrice());
}
}
if(num==2){
Goods u = new Goods();
Integer rows = 5;
Integer page = 2;
/*
*?
*?
* --rows=5,page=2
--end = rows * page=10
--start = (page - 1) * rows + 1=6
--start表示>=號后面的值,end表示<=號后面的值
*/
Integer start = (page - 1) * rows + 1;
Integer end = page * rows;
u.setPage(page);
u.setRows(rows);
u.setPstart(start);
u.setPend(end);
exePath = "com.SSHC.dao.GoodsDao.selectByPage";
List<Goods>list = session.selectList(exePath,u);
for(Goods user : list) {
System.out.println(user.getPname());
}
}
if(num==3){
exePath = "com.SSHC.dao.GoodsDao.updateById";
Goods u = new Goods();
u.setId(1);
u.setPname("黑筆");
u.setPrice(1.5);
u.setShengChanRiQi("2020-06-06");
Integer count = session.update(exePath,u);
System.out.println(count);
session.commit();}
if(num==4){
Goods u = new Goods();
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);
u.setPname("%拉面%");
exePath = "com.SSHC.dao.GoodsDao.selectByCondAndPage";
List<Goods>list = session.selectList(exePath,u);
for(Goods user : list) {
System.out.println(user.getPname());
}
} }}
?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文件中的內(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">
? ? ? ? ? ? <!-- 事務(wù)配置 -->
? ? ? ? ? ? <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/GoodsSqlMap.xml"/>
? ? ? ??
? ? </mappers>
</configuration>


作業(yè)和自己給的答案(含個人的詳細(xì)理解) END
解決導(dǎo)入項目時報了紅色的感嘆號等的問題 START
如果導(dǎo)入項目時報了紅色的感嘆號時就時jar包導(dǎo)入錯誤的原因 。


