首先入门案例(并且拿到新增记录当前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. jquery autocomplete

    <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="http: ...

  2. HTTP请求 GET与POST是怎么实现?

    1.HTTP请求格式: <request line> <headers> <blank line> [<request-body>] 在HTTP请求中, ...

  3. 大理石在哪里UVa 10474

    我自己写的代码 #include<iostream>#include<algorithm>using namespace std;int main(){    int N,a[ ...

  4. javax mail网址

    http://www.oracle.com/technetwork/java/javamail/faq/index.html#tomcatconfig

  5. 用hexdump获取event的输出信息

    当我们在调试输入设备时,如:键盘,触摸屏 会使用到hexdump工具.其内容如下: 1. 键盘: # cat /dev/input/event0 | hexdump 0000000 f6a6 4e15 ...

  6. 【GO】GO语言学习笔记二

    基本类型: 布尔型:boolean 整型:int8,byte,int16,int,uint,uintptr等 浮点型:float32,float64 复数类型:complex64,complex128 ...

  7. SQL表连接查询(inner join、full join、left join、right join)

    SQL表连接查询(inner join.full join.left join.right join) 前提条件:假设有两个表,一个是学生表,一个是学生成绩表. 表的数据有: 一.内连接-inner ...

  8. Hibernate的关联映射

    单向N-1关联 <many-to-one> 单向N-1关系,比如多个人对应同一个住址,只需要从人实体端找到对应的住址实体,无须关系某个地址的全部住户.程序在N的一端增加一个属性,该属性引用 ...

  9. linux内存占用查看命令

    ps -e -o 'pid,comm,args,pcpu,rsz,vsz,stime,user,uid' | grep tomcat | sort -nrk5 其中rsz为实际内存,上例实现按内存排序 ...

  10. Netty : writeAndFlush的线程安全及并发问题

    使用Netty编程时,我们经常会从用户线程,而不是Netty线程池发起write操作,因为我们不能在netty的事件回调中做大量耗时操作.那么问题来了 – 1, writeAndFlush是线程安全的 ...