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. java 位向量

    public class BitVectory { private int count; private int[] a; private static final int BIT_LEN = 32; ...

  2. node.js中的http.request方法使用说明

    http.get(options, callback) 由于该方法属于http模块,使用前需要引入http模块(var http= require(“http”) ) 接收参数: option   数 ...

  3. 七丶人生苦短,我用python【第七篇】

    模块 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个 ...

  4. 【LeetCode】Integer to Roman(整数转罗马数字)

    这道题是LeetCode里的第12道题. 吐了,刚做完"罗马数字转整数",现在又做这个.这个没什么想法,只能想到使用if语句嵌套,或者使用哈希表.但哈希表我还不熟练啊.先拿if嵌套 ...

  5. Codeforces Round #305 (Div. 2) D. Mike and Feet

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. soa服务治理

    SOA服务治理 文章:SOA 治理简介 文章:中小型互联网公司微服务实践-经验和教训

  7. ASP.NET上传大文件404报错

    报错信息: Failed to load resource: the server responded with a status of 404 (Not Found) 尝试1: 仅修改Web.con ...

  8. Terracotta

    Terracotta 3.2.1简介 (一) 博客分类: 企业应用面临的问题 Java&Socket 开源组件的应用 hibernatejava集群服务器EhcacheQuartzTerrac ...

  9. P2015 二叉苹果树 (树形动规)

    题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...

  10. Codevs 2602 最短路径问题

     时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间. ...