首先入门案例(并且拿到新增记录当前id)

1.创建mybatis-config.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>
<!-- 使用别名 -->
<typeAliases>
<!-- 为包下所有类使用别名,默认是简单类名 -->
<package name="cn.cnsdhzzl.entity" />
<!-- 对单个类,按类型名使用别名 -->
<!-- <typeAlias type="cn.cnsdhzzl.entity.Student" alias="student" /> -->
</typeAliases> <!-- 开发模式 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc的事务 -->
<transactionManager type="JDBC" />
<!-- 使用自带的连接池 -->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="**" />
<property name="password" value="**" />
</dataSource>
</environment>
</environments> <!-- 映射小配置 -->
<mappers>
<mapper resource="cn/cnsdhzzl/dao/StudentDAO.xml" />
</mappers>
</configuration>

2.创建分层搭建架构

3.创建StudentDAO.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="cn.cnsdhzzl.dao">
<!-- 添加操作 -->
<insert id="insertStudent">
insert into student(id,name,address,sex)
values(seq_ssm.nextval,#{name},#{address},#{sex})
<selectKey keyProperty="id" resultType="int">
select
seq_ssm.currval from dual
</selectKey>
</insert> <!-- 删除的id名称随便写 -->
<delete id="deleteStudentById">
delete student where id=#{id}
</delete> <!-- 修改操作 -->
<update id="updateStudent">
update student set
name=#{name},address=#{address},sex=#{sex} where id=#{id}
</update> <!-- 返回集合要加resultType -->
<select id="findAll" resultType="student">
select * from student
</select> <!-- 方法一 -->
<select id="likeSelectByStr" resultType="student">
select * from student
where name like '%${value}%'
</select>
<!-- 方法二 -->
<!-- <select id="likeSelectByStr" resultType="student"> select * from student
where name like concat('%',#{stuname},'%') </select> --> <!-- 方法一 -->
<select id="likeSelectByEntity" resultType="student">
select * from student
where name like '%${name}%'
</select>
<!-- 方法二 -->
<!-- <select id="likeSelectByEntity" resultType="student"> select * from
student where name like concat('%',#{stuname},'%') </select> --> </mapper>

4.实现接口方法

@Override
public Integer addStudent(Student stu) {
SqlSession sqlSession = SessionUtil.getSqlSession();
int result = sqlSession.insert("insertStudent", stu); sqlSession.commit();
sqlSession.close(); System.out.println("保存结果" + result);
return result;
}

5.测试

@Test
public void frist() {
Student stu = new Student("BBB", "北京", "男");
StudentDaoImpl dao = new StudentDaoImpl();
dao.addStudent(stu);
System.out.println("成功");
}

结果图

注意事项:

语句后面不能加';'(分号),会报sql异常

mybatis高级(1)(入门回顾)的更多相关文章

  1. MyBatis高级篇之整合ehcache缓存框架

    MyBatis高级篇之整合ehcache缓存框架  2017-09-01  0 Comments  1,671 Views  0 Times 一.前言 MyBatis为我们提供了Cache接口,也提供 ...

  2. 【Mybatis高级映射】一对一映射、一对多映射、多对多映射

    前言 当我们学习heribnate的时候,也就是SSH框架的网上商城的时候,我们就学习过它对应的高级映射,一对一映射,一对多映射,多对多映射.对于SSM的Mybatis来说,肯定也是差不多的.既然开了 ...

  3. 8、web入门回顾/ Http

    1 web入门回顾 web入门 1)web服务软件作用: 把本地资源共享给外部访问 2)tomcat服务器基本操作      : 启动:  %tomcat%/bin/startup.bat 关闭: % ...

  4. mybatis高级映射(一对一,一对多)

    mybatis高级映射 一对一关联映射 需求:查询订单信息,关联查询用户信息(一个订单对应一个用户) (1)通过resultType实现 sql语句: select orders.* , USER.u ...

  5. MyBatis(1)——快速入门

    MyBatis 简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...

  6. (转) MyBatis(1)——快速入门

    MyBatis 简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...

  7. MyBatis高级查询

    -------------------------siwuxie095 MyBatis 高级查询 1.MyBatis 作为一个 ORM 框架,也对 SQL 的高级查询做了支持, MyBatis 高级查 ...

  8. Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建

    目录 Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建 0.项目准备 1.数据持久层Mybatis+MySQL 1.1 MySQL数据准备 1.2 Mybatis ...

  9. (转)MyBatis框架的学习(二)——MyBatis架构与入门

    http://blog.csdn.net/yerenyuan_pku/article/details/71699515 MyBatis框架的架构 MyBatis框架的架构如下图: 下面作简要概述: S ...

随机推荐

  1. CSS 设置背景透明度,不影响子元素

    由于 opacity 属性能被子元素继承,使用它设置父元素背景透明度时也会影响子元素. 解决方法: 1> 使用 RGBA Example .classname { /* RGBa, 透明度0.6 ...

  2. 同一界面放两个TTIWDBAdvWebGrid的问题(delphi IW TMS)

    同一个界面放了两个 TTIWDBAdvWebGrid 时,操作一个表,另一个的也跟着一起更改了,后查看网页生成的DIV,发现ID相同. 后查,有一个ID属性,更改后解决问题.

  3. build.gradle文件详解<转> 推荐

    apply plugin: 'com.android.application'//说明module的类型,com.android.application为程序,com.android.library为 ...

  4. Android动态方式破解apk终极篇(加固apk破解方式)

    一.前言 今天总算迎来了破解系列的最后一篇文章了,之前的两篇文章分别为: 第一篇:如何使用Eclipse动态调试smali源码 第二篇:如何使用IDA动态调试SO文件 现在要说的就是最后一篇了,如何应 ...

  5. Android BinderService 过程

    步骤:建立服务器端服务,暴露接口 1.BinderService /** * @Title BinderService.java * @package cn.boxai.binderservice * ...

  6. MyBatis调用Oracle存储过程

    MyBatis调用Oracle存储过程 1.无输入和输出参数的存储过程 2.带有输入和输出参数的存储过程 3.返回游标的存储过程 mybatis中的配置文件代码 <resultMap type= ...

  7. Jquery ajax提交表单几种方法

    在jquery中ajax提交表单有post与get方式,在使用get方式时我们可以直接使用ajax 序列化表单$('#表单ID').serialize();就行了,下面我来介绍两个提交表单数据的方法. ...

  8. SpringBoot Schedule 配置

    1. 定时任务实现方式 定时任务实现方式: Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行 ...

  9. consul模板的说明2

    保证模板的正常执行 使用||true $ consul-template -template "in.ctmpl:out.file:service nginx restart || true ...

  10. 未解决的问题,登录163邮箱http://mail.163.com/,用xpath的方式定位密码输入框的时候,总是报找不到该元素

    退出的时候出现: xpath定位方法: 注意xpath路径写的太长,如果层级全部写完定位不到,就尝试去掉一些层级