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. 关于加号传递到后端会变为空格的c#例子

    参考博客:http://blog.csdn.net/nsdnresponsibility/article/details/50965262 以前在一次传递参数的情况中遇到,特此记录一下. 之前传递的参 ...

  2. Python 前端 Css基础

    CSS样式存在的位置 1.放置在标签内,局部生效 <div style="color: red;font-size: 18px;">hello world</di ...

  3. Leetcode 332.重新安排行程

    重新安排行程 给定一个机票的字符串二维数组[from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序.所有这些机票都属于一个从JFK(肯尼迪国际机场)出发的先生 ...

  4. Educational Codeforces Round 24

    A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...

  5. rabbitmq php 学习

    参考文档:http://www.cnblogs.com/phpinfo/p/4104551...http://blog.csdn.net/historyasamirror/ar... 依赖包安装 yu ...

  6. HDU 5833 Zhu and 772002 ——线性基

    [题目分析] 这题貌似在UVA上做过,高精度高斯消元. 练习赛T2,然后突然脑洞出来一个用Bitset的方法. 发现代码只需要30多行就A掉了 Bitset大法好 [代码] #include < ...

  7. tomact和eclipse的关联

    tomact和eclipse的关联有很多文档,这里说下下面的问题: 问题: tomact安装成功,点击startup.sh能正常访问,通过eclipse启动后,不能打开8080页面  解决: l  重 ...

  8. bzoj3000 Big Number 数论,斯特林公式

    Description 给你两个整数N和K,要求你输出N!的K进制的位数. Input 有多组输入数据,每组输入数据各一行,每行两个数——N,K Output 每行一个数为输出结果 Sample In ...

  9. [暑假集训--数位dp]hdu2089 不要62

    杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍, ...

  10. 云计算与 OpenStack

    “云计算” 算是近年来最热的词了.现在 IT 行业见面不说这三个字您都不好意思跟人家打招呼. 对于云计算,学术界有各种定义,大家有兴趣可以百度一下. CloudMan 这里主要想从技术的角度谈谈对云计 ...