增加

  1.增删改在接口中的返回值

    Integer、Long、Boolean、void 返回影响多少行 或 true | false

  2.mapper 中 增删改没有返回值 (resultType或resultMap)

  3.mysql支持自增主键,自增主键的值的获取,mybatis利用statement.getGeneratedKey();
  userGeneratedKeys="true" :使用自增主键获取主键的策略
  keyProperty:指定对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装到JavaBean中哪个属性中

  获取则是通过插入后,在查询一遍插入的对象,就可以拿到主键值
  4.sqlSessionFactory.openSession()需要手动提交事务,或者采用sqlSessionFactory.openSession(true)

public interface EmployeeMapper {

    Integer addEmp(Employee employee);

}
    <!--
mysql支持自增主键,自增主键的值的获取,mybatis利用statement.getGeneratedKey();
userGeneratedKeys="true" :使用自增主键获取主键的策略
keyProperty:指定对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装到JavaBean中哪个属性中
-->
<insert id="addEmp" useGeneratedKeys="true" keyProperty="id">
INSERT INTO tbl_employee(last_name, email, gender)
VALUES (#{name}, #{email}, #{gender})
</insert>
    @Test
public void addEmp() throws IOException {
//读取配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//获取sqlSession实例,能直接执行已经映射的sql语句
     //不会自动提交,除非在openSession(true)
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行查询方法
try {
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
Employee employee = new Employee(null,"麦克雷",'1',"mapleins@qq.com");
//i判断是否插入成功
Integer i = mapper.addEmp(employee);
System.out.println(i);
//可以获取插入后的主键值
System.out.println(employee.getId());
sqlSession.commit();
}finally {
//关闭sqlSession
sqlSession.close();
}
}

删除和修改

public interface EmployeeMapper {

    Long updateEmp(Employee employee);

    Boolean deleteEmpById(Integer id);

}
    <update id="updateEmp">
UPDATE tbl_employee
SET last_name=#{name},
email=#{email},
gender=#{gender}
WHERE id = #{id}
</update> <delete id="deleteEmpById">
delete from tbl_employee where id = #{id}
</delete>
    @Test
public void updateEmp() throws IOException {
//读取配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//获取sqlSession实例,能直接执行已经映射的sql语句
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行查询方法
try {
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
Employee employee = new Employee(17,"天使",'0',"mapleins@qq.com");
Long l = mapper.updateEmp(employee);
System.out.println(l);
sqlSession.commit();
}finally {
//关闭sqlSession
sqlSession.close();
}
} @Test
public void deleteEmpById() throws IOException {
//读取配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//获取sqlSession实例,能直接执行已经映射的sql语句
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行查询方法
try {
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
Boolean b = mapper.deleteEmpById(17);
System.out.println(b);
sqlSession.commit();
}finally {
//关闭sqlSession
sqlSession.close();
}
}

<MyBatis>入门三 sqlMapper文件的更多相关文章

  1. python入门三:文件操作

    一.文件操作 1.文件对象:和c一样,要想对一个文件进行操作,需要获取该文件的对象 f = open("xxx") # 打开文件并获取文件对象 f.xxx # 对文件进行某些操作 ...

  2. Mybatis入门三

    一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下: <?xml version="1 ...

  3. mybatis入门(三):mybatis的基础特性

    mybatis的知识点: 1.mybatis和hibernate本质区别和应用场景 hibernate:是一个标准的ORM框架(Ojbect relation mapper对象关系映射).入门门槛较高 ...

  4. mybatis 学习三 关键文件解析

    1:  mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYP ...

  5. mybatis入门系列三之类型转换器

    mybatis入门系列三之类型转换器 类型转换器介绍 mybatis作为一个ORM框架,要求java中的对象与数据库中的表记录应该对应 因此java类名-数据库表名,java类属性名-数据库表字段名, ...

  6. Androlid入门之文件系统操作(三)文件读写

         import java.io.*; import android.app.Activity; import android.os.Bundle; import android.view.Vi ...

  7. MyBatis入门学习(三)

    在实际开发中,我们希望文件配置是分类配置存放,需要的时候引入即可.如果多文件的配置混合配置使用,那么对项目的后期维护将增加难度. 一.对于连接数据库的配置单独放在一个properties文件中 1.对 ...

  8. QT从入门到入土(三)——文件的读写操作

     引言 文件的读写是很多应用程序具有的功能,甚至某些应用程序就是围绕着某一种格式文件的处 理而开发的,所以文件读写是应用程序开发的一个基本功能. Qt 提供了两种读写纯文本文件的基本方法: 用 QFi ...

  9. Mybatis系列(一):Mybatis入门

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

随机推荐

  1. how to modify vs2017

    https://docs.microsoft.com/en-us/visualstudio/install/modify-visual-studio 直接用everything搜索vs_install ...

  2. flash builder 4.6 下载完成后安装不成功

    从网上下载了一下flash builder 4.6 下载完成后安装不成功,说是有一个安装被挂起,不成安装成功结果从注册表中删除了pendingobject,还是不行,没有办法,从网上搜了一下,发现了大 ...

  3. JSSDK使用步骤

    绑定域名 先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”. 备注:登录后可在“开发者中心”查看对应的接口权限. 引入js文件 在需要调用JS接口的页面引入如下JS文件,( ...

  4. bzoj4407

    http://www.lydsy.com/JudgeOnline/problem.php?id=4407 以前写过一次线性筛 发现不是很理解 写了个欧拉筛的 t了 其实每次推式子,都会先推出一组的解法 ...

  5. 29. ExtJs - Struts2 整合(1) - 登录页面

    转自:https://yarafa.iteye.com/blog/729197 初学 ExtJS,在此记录下学习过程中的点点滴滴,以备不时只需,也希望能给跟我一样的菜鸟一些帮助,老鸟请忽略.如有不当之 ...

  6. E20171102-E

    segment   n. 环节; 部分,段落; [计算机] (字符等的) 分段; [动物学] 节片; distinct  adj. 明显的,清楚的; 卓越的,不寻常的; 有区别的; 确切的;

  7. js获取标签的三种方式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. EntityFramework:An error occurred while executing the command definition. See the inner exception for details.

    错误描述: 调用EF中的FirstOrDefault()时,报错误:An error occurred while executing the command definition. See the ...

  9. 为什么前后端分离不利于seo

    搜索引擎的基础爬虫的原理就是抓取你的url,然后获取你的html源代码并解析. 而你的页面通常用了vue等js的数据绑定机制来展示页面数据,爬虫获取到的html是你的模型页面而不是最终数据的渲染页面, ...

  10. Light 1289 - LCM from 1 to n (位图标记+素数筛选)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1289 题目描述: 给出一个n,求出lcm(1,2,3......n)为多少? ...