mybatis.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="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="project" />
<property name="password" value="1234" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/oracle/mapper/StudentMapper.xml" />
</mappers>
</configuration>

获取数据源

public class MyBatisSqlSession {

    // 获取数据源
private static String resource = "mybatis.xml";
private static InputStream inputStream = null;
private static SqlSessionFactory sqlSessionFactory = null; static {
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
} public static SqlSession getSession() {
return sqlSessionFactory.openSession();
} }

mapper.xml

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace配置的是dao接口-->
<mapper namespace="com.oracle.dao.IStudentMapperDao">
<!--#{}固定写法,中间的值是属性值
id 属性值就是接口里面的方法名-->
<insert id="saveStudent" parameterType="com.oracle.pojo.Student">
<!-- keycolum表的字段,keyproperty属性 order=before在insert语句执行之前执行-->
<selectKey keyColumn="id" keyProperty="id" resultType="java.lang.Long" order="BEFORE">
select student_seq.nextval as id from dual
</selectKey>
insert into student (id, name, address, gender,age) values (
#{id}, #{name}, #{address}, #{gender},#{age})
</insert> <select id="getStudentById" parameterType="java.lang.Long" resultType="java.util.HashMap">
select * from student where id=#{id}
</select> <resultMap type="com.oracle.pojo.Student" id="studentResultMap">
<id column="id" property="id" javaType="long" jdbcType="BIGINT" />
<result column="name" property="name" javaType="string" jdbcType="VARCHAR"/>
<result column="address" property="address" javaType="string" jdbcType="VARCHAR"/>
<result column="gender" property="gender" javaType="string" jdbcType="VARCHAR"/>
<result column="age" property="age" javaType="int" jdbcType="INTEGER"/>
</resultMap> <select id="getStudent" parameterType="java.lang.Long" resultMap="studentResultMap">
select * from student where id=#{id}
</select> <select id="getStudentByParam22222" parameterType="java.util.HashMap" resultMap="studentResultMap">
select * from student where id=#{id} and address=#{address}
</select> <sql id="studentsql"> id,name ,address,gender,age </sql>
<sql id="wheresql"> address=#{address} </sql> <select id="getStudentByParam" parameterType="java.lang.String" resultType="java.util.HashMap">
select <include refid="studentsql"></include> from student where <include refid="wheresql"></include>
</select> <delete id="deleteStudent" parameterType="long">
delete from student where id=#{id}
</delete> <update id="updateStudent" parameterType="java.util.HashMap" >
update student set address=#{address} where id=#{id}
</update> <select id="selectByCondition" parameterType="com.oracle.pojo.Student" resultType="com.oracle.pojo.Student" >
select id,name,address,gender,age
from student
where 1=1
<if test="id != null">
and id = #{id}
</if>
<if test="name != null">
and name = #{name}
</if>
<if test="address != null">
and address like #{address}
</if>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="age != 0">
and age = #{age}
</if>
</select> <sql id="key">
<trim suffixOverrides=",">
id,
<if test="name !=null">
name,
</if>
<if test="address !=null">
address,
</if>
<if test="gender != null">
gender,
</if>
<if test="age != 0">
age,
</if>
</trim>
</sql>
<sql id="values">
<trim suffixOverrides=",">
#{id},
<if test="name !=null">
#{name},
</if>
<if test="address !=null">
#{address},
</if>
<if test="gender != null">
#{gender},
</if>
<if test="age != 0">
#{age},
</if>
</trim>
</sql>
<insert id="dynainsert" parameterType="com.oracle.pojo.Student" >
<selectKey keyColumn="id" keyProperty="id" resultType="java.lang.Long" order="BEFORE">
select student_seq.nextval as id from dual
</selectKey>
insert into student(<include refid="key"></include>) values (<include refid="values"></include>)
</insert> <delete id="dynaDeleteArray" >
delete student where id in
<foreach collection="array" open="(" close=")" separator="," item="ids">
#{ids}
</foreach>
</delete> <delete id="dynaDeleteList">
delete from students where students_id in
<foreach collection="list" open="(" close=")" separator="," item="ids">
#{ids}
</foreach>
</delete> <update id="dynaUpdate" parameterType="com.oracle.pojo.Student">
update student
<set>
<if test="address !=null">
address = #{address},
</if>
<if test="age!=0">
age = #{age},
</if>
</set>
where id=#{id}
</update> </mapper>

dao

public interface StudentDao {
public int save(Student stu); public List query();
}

test

public class Student_Test {

    public static void main(String[] args) {
SqlSession session = MyBatisSqlSession.getSession();
StudentDao mapper = session.getMapper(StudentDao.class); // List list = mapper.query();
// System.out.println(list.size()); Student stu=new Student();
stu.setName("qqqqqqq");
stu.setAge(100);
int save = mapper.save(stu);
session.commit();
} }

实体类忽略,

必须的jar包,mabatis.jar,ojdbc.jar,和一个log4j用来输出日志

log4j配置文件名:/MyWeb7.12/src/log4j.properties

log4j.rootLogger = debug ,  stdout 

log4j.appender.stdout = org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target = System.out

log4j.appender.stdout.layout = org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern =  %d %p [%c] - %m%n

log4j.logger.com.ibatis=debug

log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug

log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug

log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug

log4j.logger.java.sql.Connection=debug

log4j.logger.java.sql.Statement=debug

log4j.logger.java.sql.PreparedStatement=debug,stdout

mybatis 简单项目步骤的更多相关文章

  1. mybatis简单项目

    1,mybatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...

  2. Spring Boot Mybatis简单使用

    Spring Boot Mybatis简单使用 步骤说明 build.gradle:依赖添加 application.properties:配置添加 代码编写 测试 build.gradle:依赖添加 ...

  3. springboot + mybatis 的项目,实现简单的CRUD

    以前都是用Springboot+jdbcTemplate实现CRUD 但是趋势是用mybatis,今天稍微修改,创建springboot + mybatis 的项目,实现简单的CRUD  上图是项目的 ...

  4. IntelliJ IDEA 创建 Maven简单项目

    创建简单Maven项目 使用IDEA提供的Maven工具,根据artifact创建简单Maven项目.根据下图操作,创建Maven项目. 使用IDEA提供的Maven工具创建的Maven简单项目目录结 ...

  5. 05_ssm基础(一)之mybatis简单使用

    01.mybatis使用引导与准备 1.ssm框架 指: sping+springMVC+mybatis 2.学习mybatis前准备web标准项目结构 model中的Ticket代码如下: pack ...

  6. 最详细的SSM(Spring+Spring MVC+MyBatis)项目搭建

    速览 使用Spring+Spring MVC+MyBatis搭建项目 开发工具IDEA(Ecplise步骤类似,代码完全一样) 项目类型Maven工程 数据库MySQL8.0 数据库连接池:Druid ...

  7. eclipse建立springMVC 简单项目

    http://jinnianshilongnian.iteye.com/blog/1594806 如何通过eclipse建立springMVC的简单项目,现在简单介绍一下. 工具/原料   eclip ...

  8. Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建(转)

    这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 如果还没有搭建好环境( ...

  9. asp.net mvc 简单项目框架的搭建(二)—— Spring.Net在Mvc中的简单应用

    摘要:上篇写了如何搭建一个简单项目框架的上部分,讲了关于Dal和Bll之间解耦的相关知识,这篇来把后i面的部分说一说. 上篇讲到DbSession,现在接着往下讲. 首先,还是把一些类似的操作完善一下 ...

随机推荐

  1. 算法学习记录-排序——选择排序(Simple Selection Sort)

    之前在冒泡排序的附录中提到可以在每次循环时候,不用交换操作,而只需要记录最小值下标,每次循环后交换哨兵与最小值下标的书, 这样可以减少交换操作的时间. 这种方法针对冒泡排序中需要频繁交换数组数字而改进 ...

  2. 关于Linux下使用expdp和impdp命令对Oracle数据库进行导入和导出操作

    说明:本次导入和导出采用expdp和impdp命令进行操作,这2个命令均需要在服务器端进行操作 http://www.cnblogs.com/huacw/p/3888807.html 一.    从O ...

  3. Java-克隆一个对象

    如可方便的克隆一个对象 package com.tj; public class MyClass implements Cloneable { public Object clone() { Clon ...

  4. [转]Python 之 使用 PIL 库做图像处理

    Python 之 使用 PIL 库做图像处理 1. 简介. 图像处理是一门应用非常广的技术,而拥有非常丰富第三方扩展库的 Python 当然不会错过这一门盛宴.PIL (Python Imaging ...

  5. [android 应用开发]android 分层

    1 应用层, 2 应用框架层(框架是所有开发人员共同使用和遵守的约定) 3 系统运行库层 4 linux内核层

  6. tab栏切换效 简易效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. 刷题总结——子串(NOIP2015提高组)

    题目: 题目背景 NOIP2015 提高组 Day2 T2 题目描述 有两个仅包含小写英文字母的字符串 A 和 B .现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k 个子串按照其在 ...

  8. 【数位DP】bnuoj 52813 J. Deciphering Oracles

    http://acm.bnu.edu.cn/v3/contest_show.php?cid=9208#problem/J [AC] #include<bits/stdc++.h> usin ...

  9. Spoj-BGSHOOT

    The problem is about Mr.BG who is a great hunter. Today he has gone to a dense forest for hunting an ...

  10. POJ 3104 Drying [二分 有坑点 好题]

    传送门 表示又是神题一道 Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9327   Accepted: 23 ...