Oracle组合查询,MyBatis Generator生成代码程序,LOG4J日志,高级update【诗书画唱】



本期看点:
07_日志和代码生成工具.ppt
PPT的文字说明
LOG4J简介
要使用log4j来记录日志,需要做到以下两点
LOG4J代码的打印语句的说明
MyBatis Generator的简介,作用和优势等等(MYBATIS框架自动生成代码工具)
使用MyBatis Generator的方法
generator:生成程序
例子
1.把组合查询的结果进行分页查询(组合分页查询)
2.MyBatis Generator代码生成工具的使用
例子 START
1.把组合查询的结果进行分页查询
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 --是否循环
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,'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,'关注','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
【这个是条件查询后的分页查询,是常用的,如果组合查询后的数据很少,比如只有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;//当前显示第几页的数据
private Integer rows;//每页显示的记录条数
//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必须是唯一的 -->
<!-- 创建一个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中的配置信息,就是读取四大连接字符串的内容
Reader config;
try {
config = Resources.getResourceAsReader(path);
SqlSessionFactory factory =
new SqlSessionFactoryBuilder().build(config);
//数据库的操作对象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文件中的内容 -->
<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>
<!-- 配置数据源 -->
<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>
<!-- 配置数据源 -->
<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简介
绝大部分企业级项目都需要记录日志,这是因为项目在运行期间不可能日日夜夜安排一个专人守在控制台监控,观察程序是否报错。
因此,我们会使用特定的工具,来帮助我们记载程序在运行使用过程中所发生的事情,同时,也方便程序员在分析日志后,对程序进行修复和调整。
现今,几乎百分之九十的项目,都会在自己的代码中使用log4j来输出日志信息,因此,我们使用log4j还是非常便利的。
要使用log4j来记录日志,需要做到以下两点:
在项目的类路径下添加log4j.jar
在项目的src根目录下增加一个名为log4j.properties

LOG4J代码的打印语句的说明:
DEBUG / INFO这是用来设置每条日志信息的级别的,除开这两个还有warn(警告)、error(错误)、fatal(致命错误)等
Console表示输出在控制台
log4j.appender.Console.layout.ConversionPattern=%d[%t] %-5p [%c] - %m%n这条信息用来定制输出的格式

%m 输出代码中指定的消息
%p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL
%r 输出自应用启动到输出该log信息耗费的毫秒数
%c 输出所属的类目,通常就是所在类的全名
%t 输出产生该日志事件的线程名
%n 输出一个回车换行符,Windows平台为“/r/n”,Unix平台为“/n”
%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss , SSS},输出类似:2002年10月18日 22 : 10 : 28 , 921
%l 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。举例:Testlog4.main(TestLog4.java: 10 )
MyBatis Generator的简介,作用和优势等等:
在企业项目中,数据库表和实体类的数量是非常多的,有的时候甚至上千个。在这种情况下,针对每张数据库表来编写实体类和相应的映射文件是一件非常繁琐的事情。
我们在编写实体类映射文件的时候,应该也发现了,对于一个对象的操作,无非就是一些CRUD,这些SQL语句编写出来,实际上还是有很多固定的规律的。能否有一套工具能帮我们代劳呢?
MyBatis提供了一套代码生成工具,能够在已经建好数据库表的情况下,自动的创建好对应的实体类和映射文件信息。
使用MyBatis Generator的方法:
搭建Generator环境的第一步是需要在工程中引入一个jar包

在src目录下创建自动生成工具的配置文件generatorConfig.xml(为了方便阅读和维护,这个文件名不要修改)


创建需要映射的表所对应的javabean类,对generatorConfig.xml进行修改。例如:需要对数据库中的danmu表进行映射配置,则先在java项目中创建与之对应的Danmu实体类。配置文件修改如下:

创建生成工具java类,这个类带有一个main方法。这个程序大概意思就是根据前面的generatorConfig.xml文件生成对应的映射文件和接口。

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

通过这套工具生成的实体类和映射文件,并不一定完全满足当前项目的需要,可能会对以下内容进行调整:
实体类字段的类型
映射文件中并非包含所有需要使用的SQL,但是常规的CRUD都是具备的,我们只需要根据自己的需要继续在里面完善就行了