第一周岗前培训

进行了一周的岗前培训,特到这来总结一下这周学的内容。
本周主要学了iBatis,SQL和TA3(公司自己封装的框架)的jsp页面组件。我在想是分开写还是写在一起,鉴于是总结本周培训的相关知识,所以还是写在一起吧~

iBatis

iBatis是一款使用方便的数据访问工具,也可作为数据持久层的框架。和ORM框架(如Hibernate)将数据库表直接映射为Java对象相比,iBatis是将SQL语句映射为Java对象。相对于全自动SQL的Hibernate,iBatis允许你对SQL有完全控制权,可以视为半自动的数据访问工具。

iBatis在idea配置普通项目的过程就略过了,遇到问题最大的还是老师最后留给我们的将ibatis配置到Web项目中去,那么就大概描述以下,这中间遇到的一系列的坑。

  • 在idea中创建web项目,配置过程略过
  • 导入iBatis必要的jar包。主要的jar包主要如下:
    主要的jar包

    在这里注意ojdbc6这个jar包,我在这里踩了坑,具体的错误显示如下:
    ORA-28040: No matching authentication protocol(没有匹配的身份验证协议)
    具体的解决办法见下。
  • 创建与数据库表相对应的实体类,比如我创建的account类,其中包含了表中的所需的字段,代码如下:
package com.yinhai.domain;

public class Account {
    private int id;
    private String firstName;
    private String lastName;
    private String emailAddress;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    @Override
    public String toString() {
        return "Account [id=" + id + ", FirstName=" + firstName + ", LastName=" + lastName + "Email" +
                emailAddress + "]";
    }
}
  • 创建iBatis的配置文件
    SqlMapConfig.xml:顾名思义,就是数据库映射配置文件,即配置数据库连接环境,iBatis使用type="SIMPLE"来获取数据库环境
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

  <!-- Configure a built-in transaction manager.  If you're using an 
       app server, you probably want to use its transaction manager 
       and a managed datasource -->
  <transactionManager type="JDBC" commitRequired="false">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="oracle.jdbc.OracleDriver"/>
      <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
      <property name="JDBC.Username" value="c##scott"/>
      <property name="JDBC.Password" value="tiger"/>
    </dataSource>
  </transactionManager>

<!--还没完!这将在后续配置的过程中完善SqlMapConfig.xml的代码-->

</sqlMapConfig>

  • 创建iBatis的实体与映射关系文件
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="Account">

  <!-- Use type aliases to avoid typing the full classname every time. -->
  <typeAlias alias="Account" type="com.yinhai.domain.Account"/>

  <!-- Result maps describe the mapping between the columns returned
       from a query, and the class properties.  A result map isn't
       necessary if the columns (or aliases) match to the properties 
       exactly. -->
  <resultMap id="AccountResult" class="Account">
    <result property="id" column="ACC_ID"/>
    <result property="firstName" column="ACC_FIRST_NAME"/>
    <result property="lastName" column="ACC_LAST_NAME"/>
    <result property="emailAddress" column="ACC_EMAIL"/>
  </resultMap>

  <!-- Select with no parameters using the result map for Account class. -->
  <select id="selectAllAccounts" resultMap="AccountResult">
    select * from ACCOUNT
  </select>

  <!-- A simpler select example without the result map.  Note the 
       aliases to match the properties of the target result class. -->
  <select id="selectAccountById" parameterClass="int" resultClass="Account">
    select
      ACC_ID as id,
      ACC_FIRST_NAME as firstName,
      ACC_LAST_NAME as lastName,
      ACC_EMAIL as emailAddress
    from ACCOUNT
    where ACC_ID = #id#
  </select>
   
  <!-- Insert example, using the Account parameter class -->
  <insert id="insertAccount" parameterClass="Account">
    insert into ACCOUNT (
                         ACC_ID,
                         ACC_FIRST_NAME,
                         ACC_LAST_NAME,
                         ACC_EMAIL
                )
         values (
                         #id#,
                         #firstName#,
                         #lastName#,
                         #emailAddress#
    )
  </insert>

  <!-- Update example, using the Account parameter class -->
  <update id="updateAccount" parameterClass="Account">
    update ACCOUNT set
      ACC_FIRST_NAME = #firstName#,
      ACC_LAST_NAME = #lastName#,
      ACC_EMAIL = #emailAddress#
    where
      ACC_ID = #id#
  </update>

  <!-- Delete example, using an integer as the parameter class -->
  <delete id="deleteAccountById" parameterClass="int">
    delete from ACCOUNT where ACC_ID = #id#
  </delete>

  <select id="queryById" parameterClass="java.lang.Integer" resultClass="java.util.HashMap">
    select ACC_ID,
           ACC_FIRST_NAME,
           ACC_LAST_NAME,
           ACC_EMAIL
    where ACC_ID = #id#
  </select>

</sqlMap>
  • 现在我们已经有了Mybatis的配置文件和表与实体之前的映射文件了,因此我们要将配置文件和映射文件关联起来
    下面这个代码将添加至SqlMapConfig.xml文件中
<sqlMap resource="resources/Account.xml"/>
  • 在test包中创建工具测试类SimpleExample,static静态语句块通过配置文件和数据库的信息,获取sqlMapper,通过sqlMapper中的数据操纵函数来对数据库进行操作。
package com.yinhai.test;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
import com.yinhai.domain.Account;

import java.io.Reader;
import java.io.IOException;
import java.util.List;
import java.sql.SQLException;

/**
 * This is not a best practices class.  It's just an example
 * to give you an idea of how iBATIS works.  For a more complete
 * example, see JPetStore 5.0 at http://www.ibatis.com.
 */
public class SimpleExample {

  /**
   * SqlMapClient instances are thread safe, so you only need one.
   * In this case, we'll use a static singleton.  So sue me.  ;-)
   */
  private static SqlMapClient sqlMapper;

  /**
   * It's not a good idea to put code that can fail in a class initializer,
   * but for sake of argument, here's how you configure an SQL Map.
   */
  static {
    try{   
      Reader reader = Resources.getResourceAsReader("resources/SqlMapConfig.xml");
      sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
      reader.close(); 
    } catch (IOException e) {
      // Fail fast.
      throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
    }
  }

  public static List selectAllAccounts () throws SQLException {
    return sqlMapper.queryForList("selectAllAccounts");
  }

  public static Account selectAccountById  (int id) throws SQLException {
    return (Account) sqlMapper.queryForObject("selectAccountById", id);
  }

  public static void insertAccount (Account account) throws SQLException {
    sqlMapper.insert("insertAccount", account);
  }

  public static void updateAccount (Account account) throws SQLException {
    sqlMapper.update("updateAccount", account);
  }

  public static void deleteAccount (int id) throws SQLException {
    sqlMapper.delete("deleteAccount", id);
  }

}

至此,我们就把iBatis的环境配置完毕,下面我们就可以将其与web结合,将数据显示在网页上,用到的方法就是简单的servlet

  • 创建ServletDemo类来将数据呈现在web页面上即可,代码如下:
package com.yinhai.test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/example")
public class ServletDemo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        try{
            List list = SimpleExample.selectAllAccounts();
            out.println(list.toString());
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}

运行效果如图:


运行效果

这个是对上文的ORA-28040: No matching authentication protocol(没有匹配的身份验证协议)进行解释:
问题描述:尝试利用ojdbc6来连接oracle12c数据库时发生该问题
网络上的解决方法,参见网址:
http://logic.edchen.org/how-to-resolve-ora-28040-no-matching-authentication-protocol/

发生该错误的原因:
There was no acceptable authentication protocol for either client or server.

版本之间可能存在一些互操作性问题,特别是当版本差距很大时。管理员应将客户端和服务器上的SQLNET.ALLOWED_LOGON_VERSION_SERVERSQLNET. ALLOWED_LOGON_VERSION_CLIENT参数的值设置为与系统中支持的最低版本软件匹配的值。

当客户端对没有适用于客户端软件版本的验证器创建的用户帐户进行身份验证时,也会引发此错误ORA-28040。在这种情况下,必须重置该帐户的密码,以便生成所需的验证程序并允许验证成功进行。

应该使用SQLNET.ALLOWED_LOGON_VERSION_SERVER与身份验证协议的两端兼容。

使用该命令的目的在于设置连接到Oracle数据库实例时允许的最低身份验证协议。

使用说明:参数名称中的version指的是身份验证协议的版本,不是Oracle数据库版本

如果客户端版本不满足或者超过此参数定义的值,就会报出ORA-28040: No matching authentication protocol error 或者 ORA-03134: Connections to this server version are no longer supported error.

1.试图将ojdbc6换成ojdbc8 ---> 调试失败

2.在sqlnet.ora配置文件中加入SQLNET.ALLOWED_LOGON_VERSION_SERVER=8

----> 成功

Oracle相关

培训老师只需让我掌握具体的sql的查询语句,即数据操纵语言(DML)。关于DDL等其他语言不重点掌握,想来也是,公司部门分工明确,我的岗位是java开发工程师,并非数据库管理员,建立用户,表信息等工作并非我等需要掌握的技能。但是个人觉得还是不能荒废触发器,试图,索引等相关知识,所以必须有空将其掌握熟悉,以便于整个职业生涯的发展。
由于主要学习的是SQL查询语句,所以将比较难的查询语句贴上来,以便后续的复习

题目1:查询scott用户下的所有表
答案:

select * from tabs

题目6:查询所有雇员编号,姓名,工作.按以下格式显示:编号:7369,姓名:SMITH,工作:CLERK
答案:

select '编号:'||empno || ',姓名:'||ename ||  ',工作:'||job from emp

题目14:查询在1981年雇用的员工信息
答案:

除了利用extract(year from hiredate)也可以用to_char(hiredate,’YYYY’)
select * from emp where extract(year from hiredate)=1981

题目43:找出早于12年前受雇的员工信息
答案:months_between(sysdate-hiredate)/12

select * from emp where (sysdate - hiredate)/365 >=12  

题目44:以首字母大写的方式显示所有员工的姓名
答案:

select upper(substr(ename,1,1)) || lower (substr(ename,2,length(ename)-1)) from emp

题目65:创建报告,显示员工名和奖金系数,如果奖金系数为空,则显示"无奖金"
答案:

select ename,decode(comm,'','无奖金',comm) from emp;

解释:如果comm是null,则返回无奖金,不是null就返回奖金的数目

  • decode函数

decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值),这行代码就是decode全部的精髓以及用法。
该函数的含义如下:
IF 条件=值1 THEN RETURN(返回值1)
ELS IF 条件=值2 THEN RETURN(返回值2)
......
ELSIF 条件=值n THEN RETURN(返回值n)
ELSE RETURN(缺省值)
END IF

decode(字段或字段的运算,值1,值2,值3)
这个函数运行的结果是,当字段或字段的运算的值等于值1时,该函数返回值2,否则返回值3
当然值1,值2,值3也可以是表达式,这个函数使得某些sql语句简单了许多
下面举两个栗子:
栗子1:
你要统计students表中,男生女生的数量:
传统写法如下:
select count() from students where 性别 = 男;
select count(
) from students where 性别 = 女;
然后使用Union连接,得到最终的统计结果。
现在你只需要这样来写:
select decode(性别,男,1,0),decode(性别,女,1,0) from students

题目67:请使用decode语句,查询员工的job_id和级别.例如:
Job Grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
None of the above 0
答案:

select job,decode(job,
                  'CLERK','E',
                  'SALESMAN','D',
                  'ANALYST','C',
                  'MANAGER','B',
                  'PRESIDENT','A',
                  '0')
from emp;

题目68:请使用case语句,查询员工的job_id和级别.例如:
Job Grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
None of the above 0
答案:

SELECT job_id, CASE job_id 
                WHEN 'ST_CLERK' THEN 'E' 
                WHEN 'SA_REP' THEN 'D' 
                WHEN 'IT_PROG' THEN 'C' 
                WHEN 'ST_MAN' THEN 'B' 
                WHEN 'AD_PRES' THEN 'A' 
                ELSE '0' END GRADE 
from employees;

题目78:查询每个部门中工资最高的雇员姓名,工作,工资,部门名称,最后按工资从高到低排序,工资相同的情况下按姓名排升序

答案:

select e.ename,e.job,e.sal,d.dname 
from emp e,dept d,(select max(e.sal) sal 
                     from emp e,dept d 
                     where e.deptno=d.deptno 
                     group by d.deptno) temp 
                     where e.deptno=d.deptno and e.sal=temp.sal  
                     order by e.sal desc;   

题目81:按部门分组,并显示部门的名称,以及每个部门的员工数
答案:

select d.dname,count(e.empno)
from dept d left join emp e
on e.deptno=d.deptno
group by e.deptno,d.dname;  

TA3Cloud具体学习

今天刚开头,仅仅学了jsp页面组件,后续在更新

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,734评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,931评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,133评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,532评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,585评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,462评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,262评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,153评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,587评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,792评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,919评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,635评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,237评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,855评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,983评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,048评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,864评论 2 354

推荐阅读更多精彩内容