Oracle組合查詢,MyBatis Generator生成代碼程序,LOG4J日志,高級update【詩書畫唱】



本期看點:
07_日志和代碼生成工具.ppt?
PPT的文字說明
LOG4J簡介
要使用log4j來記錄日志,需要做到以下兩點
LOG4J代碼的打印語句的說明
MyBatis Generator的簡介,作用和優(yōu)勢等等(MYBATIS框架自動生成代碼工具)
使用MyBatis Generator的方法
generator:生成程序
例子
1.把組合查詢的結(jié)果進行分頁查詢(組合分頁查詢)
2.MyBatis Generator代碼生成工具的使用
例子 START
1.把組合查詢的結(jié)果進行分頁查詢
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;? ? ? ? ? --預(yù)存
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,'666','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'999','pwd5',to_date('2020-06-10','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'888','pwd6',to_date('2020-06-11','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,'關(guān)注','pwd6',to_date('2020-06-11','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'詩書畫唱1','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'詩書畫唱2','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'詩書畫唱3','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'詩書畫唱4','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
insert into Userinfo values(seq_Userinfo.nextval,'詩書畫唱5','pwd4',to_date('2020-06-06','yyyy-mm-dd'));
--select * from Userinfo?
select * from userinfo WHERE act like '%詩%'
--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? and act like '%詩%'
【這個不是條件查詢后的分頁查詢,而是分頁查詢后的條件查詢】
select * from (select t.*,rownum rn from
?(select * from userinfo WHERE act like '%詩%' ) t where rownum <= 10) where rn >= 6?
【這個是條件查詢后的分頁查詢,是常用的,如果組合查詢后的數(shù)據(jù)很少,比如只有1條,
就難看出效果】
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>selectByCondAndPage(Userinfo u) -->
? ? <select id="selectByCondAndPage" resultMap="rmUserinfo"
? ? ? ? parameterType="Userinfo">
? ? <!--? ? ?select * from
(select t.*,rownum rn from?
(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>?
) t
where rownum <= #{pend})
where rn >= #{pstart} -->?
select * from
(select t.*,rownum rn from?
(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>?
) t
where rownum <= #{pend})
where rn >= #{pstart}
? ? </select>
? ?
? ? <!-- public Integer updateById(Userinfo u) -->
? ? <update id="updateById" parameterType="Userinfo">
? ? ? ? update userinfo
? ? ? ? <set>
? ? ? ? ? ? <if test="act != null and act.length() > 0">
? ? ? ? ? ? ? ? act = #{act},
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="pwd != null and pwd.length() > 0">
? ? ? ? ? ? ? ? pwd = #{pwd},
? ? ? ? ? ? </if>
? ? ? ? ? ? <if test="birth != null and birth.length() > 0">
? ? ? ? ? ? ? ? birth = to_date(#{birth},'yyyy-mm-dd'),
? ? ? ? ? ? </if>
? ? ? ? </set>
where id = #{id}
? ? </update>
</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.Userinfo;
public class CondPageUpdate {
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("請選擇操作:1.組合查詢后,進行分頁查詢"
+ ",\n 2.類似萬能Dao的高級修改(就是可以setXXX()"
+ "\n 少寫幾個都不會報空指針異常的錯"
+ ",\n 而且還可以修改成功)");
? ?Scanner input = new Scanner(System.in);
? ?int num = input.nextInt();
if(num==1){
Userinfo u = new Userinfo();
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.setAct("%詩%");
exePath = "com.SSHC.dao.UserinfoDao.selectByCondAndPage";
List<Userinfo>list = session.selectList(exePath,u);
for(Userinfo user : list) {
System.out.println(user.getAct());
}
}
if(num==2){ exePath = "com.SSHC.dao.UserinfoDao.updateById";
Userinfo u = new Userinfo();
u.setId(3);
u.setAct("藍藍");
u.setPwd("pwd3");
u.setBirth("2020-06-08");
Integer count = session.update(exePath,u);
System.out.println(count);
session.commit();}
} } 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/UserinfoSqlMap.xml"/>
? ? ? ??
? ? </mappers>
</configuration>

2.MyBatis Generator代碼生成工具的使用















例子 END
07_日志和代碼生成工具.ppt? START
generator:生成程序?















PPT的文字說明:
LOG4J簡介
絕大部分企業(yè)級項目都需要記錄日志,這是因為項目在運行期間不可能日日夜夜安排一個專人守在控制臺監(jiān)控,觀察程序是否報錯。
因此,我們會使用特定的工具,來幫助我們記載程序在運行使用過程中所發(fā)生的事情,同時,也方便程序員在分析日志后,對程序進行修復(fù)和調(diào)整。
現(xiàn)今,幾乎百分之九十的項目,都會在自己的代碼中使用log4j來輸出日志信息,因此,我們使用log4j還是非常便利的。
要使用log4j來記錄日志,需要做到以下兩點:
在項目的類路徑下添加log4j.jar
在項目的src根目錄下增加一個名為log4j.properties

LOG4J代碼的打印語句的說明:
DEBUG / INFO這是用來設(shè)置每條日志信息的級別的,除開這兩個還有warn(警告)、error(錯誤)、fatal(致命錯誤)等
Console表示輸出在控制臺
log4j.appender.Console.layout.ConversionPattern=%d[%t] %-5p [%c] - %m%n這條信息用來定制輸出的格式

%m? ?輸出代碼中指定的消息
%p? ?輸出優(yōu)先級,即DEBUG,INFO,WARN,ERROR,F(xiàn)ATAL?
%r? ?輸出自應(yīng)用啟動到輸出該log信息耗費的毫秒數(shù)?
%c? ?輸出所屬的類目,通常就是所在類的全名?
%t? ?輸出產(chǎn)生該日志事件的線程名?
%n? ?輸出一個回車換行符,Windows平臺為“/r/n”,Unix平臺為“/n”?
%d? ?輸出日志時間點的日期或時間,默認格式為ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss , SSS},輸出類似:2002年10月18日? 22 : 10 : 28 , 921??
%l? ?輸出日志事件的發(fā)生位置,包括類目名、發(fā)生的線程,以及在代碼中的行數(shù)。舉例:Testlog4.main(TestLog4.java: 10 )?
MyBatis Generator的簡介,作用和優(yōu)勢等等:
在企業(yè)項目中,數(shù)據(jù)庫表和實體類的數(shù)量是非常多的,有的時候甚至上千個。在這種情況下,針對每張數(shù)據(jù)庫表來編寫實體類和相應(yīng)的映射文件是一件非常繁瑣的事情。
我們在編寫實體類映射文件的時候,應(yīng)該也發(fā)現(xiàn)了,對于一個對象的操作,無非就是一些CRUD,這些SQL語句編寫出來,實際上還是有很多固定的規(guī)律的。能否有一套工具能幫我們代勞呢?
MyBatis提供了一套代碼生成工具,能夠在已經(jīng)建好數(shù)據(jù)庫表的情況下,自動的創(chuàng)建好對應(yīng)的實體類和映射文件信息。
使用MyBatis Generator的方法:
搭建Generator環(huán)境的第一步是需要在工程中引入一個jar包

在src目錄下創(chuàng)建自動生成工具的配置文件generatorConfig.xml(為了方便閱讀和維護,這個文件名不要修改)


創(chuàng)建需要映射的表所對應(yīng)的javabean類,對generatorConfig.xml進行修改。例如:需要對數(shù)據(jù)庫中的danmu表進行映射配置,則先在java項目中創(chuàng)建與之對應(yīng)的Danmu實體類。配置文件修改如下:

創(chuàng)建生成工具java類,這個類帶有一個main方法。這個程序大概意思就是根據(jù)前面的generatorConfig.xml文件生成對應(yīng)的映射文件和接口。

運行main方法后,觀察控制臺。
刷新工程后可即看到新生成的映射文件和接口。

通過這套工具生成的實體類和映射文件,并不一定完全滿足當前項目的需要,可能會對以下內(nèi)容進行調(diào)整:
實體類字段的類型
映射文件中并非包含所有需要使用的SQL,但是常規(guī)的CRUD都是具備的,我們只需要根據(jù)自己的需要繼續(xù)在里面完善就行了