------------------------------------------------------------
school - oracle.txt
-- Create the school of oracle
-- Create student
create table student
(
sId int primary key,
sName varchar(20) not null,
sAge smallint not null
);
-- Create increment.
CREATE SEQUENCE s_increment start with 1 increment by 1;
-- Insert infos.
insert into student values (s_increment.nextval, '王晓兰', 25);
insert into student values (s_increment.nextval, '赵学刚', 28);
insert into student values (s_increment.nextval, '赵新', 25);
-- Create department.
create table department
(
dId int primary key,
dName varchar(20) not null
);
CREATE SEQUENCE s_department start with 1 increment by 1;
insert into department values (s_department.nextval, '后勤部');
insert into department values (s_department.nextval, '行政部');
insert into department values (s_department.nextval, '学员部');
insert into department values (s_department.nextval, '教师部');
-- Create person.
create table person
(
pId int primary key,
pName varchar(20) not null,
pAge smallint not null,
dptId int not null
);
comment on column person.pAge is '人的年龄';
comment on table person is '人员表';
CREATE SEQUENCE s_person start with 1 increment by 1;
insert into person values (s_person.nextval, '张琪', 26, 1);
insert into person values (s_person.nextval, '王蒙', 24, 2);
insert into person values (s_person.nextval, '左青', 23, 3);
insert into person values (s_person.nextval, '郝佳', 28, 4);
-- Select remark.
SELECT a1.column_name AS 字段名称
,CASE
WHEN a1.data_precision IS NULL THEN a1.data_type||'('||a1.data_length||')'
ELSE a1.data_type||'('||a1.data_length||','||a1.data_precision||')'
END AS 字段属性
,CASE
WHEN a2.comments IS NULL THEN '未知'
ELSE a2.comments
END AS 字段描述
FROM USER_TAB_COLUMNS a1
LEFT JOIN USER_COL_COMMENTS a2
ON a1.table_name = a2.table_name
AND a1.column_name = a2.column_name
WHERE a1.table_name = upper(
'person' -- 填写表名
)
ORDER BY a1.column_id
------------------------------------------------------------
mybatis-config__Oracle.xml
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!-- <property name="driver" value="org.gjt.mm.mysql.Driver" /> <property
name="url" value="jdbc:mysql://localhost:3306/db_school?useUnicode=true&characterEncoding=utf-8"
/> <property name="username" value="root" /> <property name="password" value="111"
/> -->
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:school" />
<property name="username" value="scott" />
<property name="password" value="School-2013" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mybatis/prj/oracle/vo/Student.xml" />
</mappers>
</configuration>
------------------------------------------------------------
StudentEntity.java
package mybatis.prj.oracle.vo;
import java.io.Serializable;
public class StudentEntity implements Serializable {
private static final long serialVersionUID = 1L;
private int sId;
private String sName;
private short sAge;
public int getsId() {
return sId;
}
public void setsId(int sId) {
this.sId = sId;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public short getsAge() {
return sAge;
}
public void setsAge(short sAge) {
this.sAge = sAge;
}
}
------------------------------------------------------------
Student.xml
<?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="student">
<select id="querystudents" resultType="mybatis.prj.oracle.vo.StudentEntity">
SELECT * FROM
student
</select>
<select id="queryByWays" resultType="mybatis.prj.oracle.vo.StudentEntity"
parameterType="mybatis.prj.oracle.vo.StudentEntity">
SELECT * FROM student WHERE sId=#{sId} AND sAge <![CDATA[ < ]]>
#{sAge}
</select>
<select id="querystudentsByPD" resultType="mybatis.prj.oracle.vo.StudentEntity">
select p.pId as
sId,p.pName as sNo, d.dName
as sName, p.pAge as sAge from person
p,department d where
p.dptId=d.dId
</select>
<insert id="saveStudent" parameterType="mybatis.prj.oracle.vo.StudentEntity">
INSERT INTO student
VALUES
(s_increment.nextval, #{sName}, #{sAge})
</insert>
<update id="updateStudent" parameterType="mybatis.prj.oracle.vo.StudentEntity">
update student set
sName=#{sName},sAge=#{sAge} where sId=#{sId}
</update>
<delete id="deleteStudent" parameterType="mybatis.prj.oracle.vo.StudentEntity">
DELETE FROM student
WHERE sId=#{sId}
</delete>
</mapper>
------------------------------------------------------------
TestAction.java
package mybatis.prj.oracle.action;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import mybatis.framework.abstracts.BasicAction;
import mybatis.prj.oracle.vo.StudentEntity;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class TestAction extends BasicAction {
public static void main(String args[]) {
TestAction ta = new TestAction();
// Query all.
// ta.queryAll("querystudents");
// Query by entity ways.
// ta.queryByWays();
// Get result by other entity.
// ta.queryAll("queryStudentsByPD");
// Save a StudentEntity.
// main 1
// Map map = new HashMap();
// // map.put("sId", null);
// map.put("sName", "董存瑞");
// map.put("sAge", (byte) 44);
// main 2
// StudentEntity se = new StudentEntity();
// se.setsName("王小丫");
// se.setsAge((byte) 22);
//
// ta.save(se);
ta.queryAll("querystudents");
// Update a entity.
// StudentEntity se = new StudentEntity();
// se.setsId(71);
// se.setsName("金梅儿1");
// se.setsAge((byte) 26);
//
// ta.update(se);
// Delete a entity.
// StudentEntity se = new StudentEntity();
// se.setsId(69);
//
// ta.delete(se);
}
private boolean delete(StudentEntity se) {
boolean b = false;
// 读取myBatis的配置文件
Reader reader;
try {
reader = Resources.getResourceAsReader("mybatis-config__Oracle.xml");
// 创建SessionFactory工厂
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
// 取出一个连接
SqlSession session = sqlMapper.openSession();
int flag = session.delete("deleteStudent", se);
if (flag > 0) {
session.commit();
session.close();
reader.close();
b = true;
} else {
b = false;
}
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
private boolean update(StudentEntity se) {
boolean b = false;
// 读取myBatis的配置文件
Reader reader;
try {
reader = Resources.getResourceAsReader("mybatis-config__Oracle.xml");
// 创建SessionFactory工厂
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
// 取出一个连接
SqlSession session = sqlMapper.openSession();
int flag = session.update("sch_student.updateStudent", se);
if (flag > 0) {
session.commit();
session.close();
reader.close();
b = true;
} else {
b = false;
}
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
private boolean save(StudentEntity se) { // Map map
boolean b = false;
// 读取myBatis的配置文件
Reader reader;
try {
reader = Resources.getResourceAsReader("mybatis-config__Oracle.xml");
// 创建SessionFactory工厂
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
// 取出一个连接
SqlSession session = sqlMapper.openSession();
int flag = session.insert("saveStudent", se);
if (flag > 0) {
session.commit();
session.close();
reader.close();
b = true;
} else {
b = false;
}
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
private void queryByWays() {
// 读取myBatis的配置文件
Reader reader;
try {
reader = Resources.getResourceAsReader("mybatis-config__Oracle.xml");
// 创建SessionFactory工厂
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
// 取出一个连接
SqlSession session = sqlMapper.openSession();
StudentEntity se = new StudentEntity();
se.setsId(71);
se.setsAge((byte) 30);
List<StudentEntity> list = session.selectList("queryByWays", se);
for (int i = 0; i < list.size(); i++) {
StudentEntity student = list.get(i);
System.out.println("---------------[" + (i + 1)
+ "]--------------");
System.out.println(student.getsId());
System.out.println(student.getsName());
System.out.println(student.getsAge());
System.out.println("---------------------------------");
}
reader.close();
session.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void queryAll(String selId) {
// 读取myBatis的配置文件
Reader reader;
try {
reader = Resources
.getResourceAsReader("mybatis-config__Oracle.xml");
// 创建SessionFactory工厂
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder()
.build(reader);
// 取出一个连接
SqlSession session = sqlMapper.openSession();
List<StudentEntity> list = session.selectList(selId);
for (int i = 0; i < list.size(); i++) {
StudentEntity student = list.get(i);
System.out.println("---------------[" + (i + 1)
+ "]--------------");
System.out.println(student.getsId());
System.out.println(student.getsName());
System.out.println(student.getsAge());
System.out.println("---------------------------------");
}
reader.close();
session.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
------------------------------------------------------------
Recursion.java
package mybatis.prj.exercise;
/**
* 递归
*
* @author AARON
*
*/
public class Recursion {
public static void main(String[] args) {
Recursion r = new Recursion();
double show = r.getValue(6);
System.out.println(show);
}
private double getValue(double para) {
double flag = 0.00;
if (para == 1 || para == 2) {
flag = 1;
} else {
flag = getValue(para - 1) + getValue(para - 2);
}
return flag;
}
}