mybatis 简单项目步骤
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 简单项目步骤的更多相关文章
- mybatis简单项目
1,mybatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...
- Spring Boot Mybatis简单使用
Spring Boot Mybatis简单使用 步骤说明 build.gradle:依赖添加 application.properties:配置添加 代码编写 测试 build.gradle:依赖添加 ...
- springboot + mybatis 的项目,实现简单的CRUD
以前都是用Springboot+jdbcTemplate实现CRUD 但是趋势是用mybatis,今天稍微修改,创建springboot + mybatis 的项目,实现简单的CRUD 上图是项目的 ...
- IntelliJ IDEA 创建 Maven简单项目
创建简单Maven项目 使用IDEA提供的Maven工具,根据artifact创建简单Maven项目.根据下图操作,创建Maven项目. 使用IDEA提供的Maven工具创建的Maven简单项目目录结 ...
- 05_ssm基础(一)之mybatis简单使用
01.mybatis使用引导与准备 1.ssm框架 指: sping+springMVC+mybatis 2.学习mybatis前准备web标准项目结构 model中的Ticket代码如下: pack ...
- 最详细的SSM(Spring+Spring MVC+MyBatis)项目搭建
速览 使用Spring+Spring MVC+MyBatis搭建项目 开发工具IDEA(Ecplise步骤类似,代码完全一样) 项目类型Maven工程 数据库MySQL8.0 数据库连接池:Druid ...
- eclipse建立springMVC 简单项目
http://jinnianshilongnian.iteye.com/blog/1594806 如何通过eclipse建立springMVC的简单项目,现在简单介绍一下. 工具/原料 eclip ...
- Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建(转)
这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 如果还没有搭建好环境( ...
- asp.net mvc 简单项目框架的搭建(二)—— Spring.Net在Mvc中的简单应用
摘要:上篇写了如何搭建一个简单项目框架的上部分,讲了关于Dal和Bll之间解耦的相关知识,这篇来把后i面的部分说一说. 上篇讲到DbSession,现在接着往下讲. 首先,还是把一些类似的操作完善一下 ...
随机推荐
- 笛卡尔&小雷:科学发展有规律,研究科学有方法
一直在总结自己的学习和研究方法,最近在读吴军写的<文明之光> ,感觉这篇介绍笛卡尔的内容非常有价值,特此整理.最近开始在密谋自己的理论体系,低调实施中... 笛卡尔按照感知的方式,把人的 ...
- !!注意!部署出现the requested resource is not available
避免项目里重复包出现 同时tomcat的lib里避免重复包,也会出现requested resource isnot available!
- selenium常见控件操作
下拉选择框 第一种方法:from selenium.webdriver.support.select import Select# 实例化一个Select类的对象 selector = Select( ...
- 一堆Offer怎么选?这样做就不纠结了
有个朋友,工作了10年左右,春节后换工作,拿了三个Offer(西安): 通信行业的一家研究所,软件开发工程师,月薪7K,承诺有月奖金.年终奖金 一家做大数据的公司,软件开发工程师,月薪15K,13薪 ...
- Caffe 不同版本之间layer移植方法
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/52185521 (前两天这篇博客不小心被 ...
- iossharesdk微信登录出错
只用下面的初始化就行了 // //添加微信应用 注册网址 http://open.weixin.qq.com // [ShareSDK connectWeChatWithAppId:mod ...
- leetcode 347 priority,map的使用
主要是对次数进行排序,然后去前几个最大次数的值,输出即可 class Solution { public: vector<int> topKFrequent(vector<int&g ...
- 货车运输(codevs 3287)
题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过 ...
- gridview无数据源实现更新数据库(即断开更新数据库)
原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- 标准C程序设计七---17
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...