方法一:通过配置文件

<?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="test2.userMapper">
<insert id="addUser" parameterType="test.User">
insert into users(name, age) values(#{name}, #{age})
</insert> <delete id="deleteUser" parameterType="int">
delete from users where id=#{id}
</delete> <update id="updateUser" parameterType="test.User">
update users set name=#{name}, age=#{age} where id=#{id}
</update> <select id="getAllUsers" resultType="test.User">
select * from users
</select>
</mapper>

然后在conf里配置

使用方式:

String resource = "conf.xml";
InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
String statement = "test.userMapper.getUser";
User user = session.selectOne(statement, 2);
System.out.println(user);

方式二:注释

package test3;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import test.User; public interface UserMapper { @Insert("insert into users(name, age) values(#{name}, #{age})")
public int addUser(User user); @Delete("delete from users where id=#{id}")
public int deleteById(int id); @Update("update users set name=#{name}, age=#{age} where id=#{id}")
public int update(User user); @Select("select * from users where id=#{id}")
public User getById(int id); @Select("select * from users")
public List<User> getAll();
}

然后在conf里配置

<mappers>
<mapper resource="test/userMapper.xml"/>
<mapper resource="test2/userMapper.xml"/>
<mapper class="test3.UserMapper"/>
</mappers>

使用方式:

String resource = "conf.xml";
InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession(true);
UserMapper mapper = session.getMapper(UserMapper.class);
int add = mapper.addUser(new User(-1, "SS", 45));
System.out.println(add);
session.close();

注意:UserMapper不能和userMapper.xml在同一个包下,例如都是test2然后注册的话会产生冲突,即使一个是userMapper一个是UserMapper

mybatis CRUD的更多相关文章

  1. [MyBatis]完整MyBatis CRUD工程

    下载地址:https://files.cnblogs.com/files/xiandedanteng/Person191005.rar pom.xml:这个文件主要是引入依赖 <project ...

  2. 【MyBatis】MyBatis CRUD

    MyBtis CRUD 文章源码 基于代理 DAO 的 CRUD 根据 ID 查询操作 在持久层接口中添加 findById 方法: public interface UserDAO { /** * ...

  3. mybatis crud 学习总结02

    1.db.properties 原因:数据库的连接直接写到配置(mybati-config.xml)里存在安全隐患 解决:将配置的信息放到一个文件中管理 右键resources --> File ...

  4. MyBatis crud操作

    Test2.java package com.mycom.mybatis_1.crud; import java.util.List; import org.apache.ibatis.session ...

  5. MyBatis CRUD Java POJO操作

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...

  6. 1.SpringBoot整合Mybatis(CRUD的实现)

    准备工具:IDEA  jdk1.8 Navicat for MySQL Postman 一.新建Project 选择依赖:mybatis  Web  Mysql  JDBC 项目结构 pom依赖: & ...

  7. Mybatis的CRUD案例

    一.Mybatis增删改查案例 上一节<Mybatis入门和简单Demo>讲了如何Mybatis的由来,工作流程和一个简单的插入案例,本节主要继上一讲完整的展示Mybatis的CRUD操作 ...

  8. MyBatis之二:简单增删改查

    这一篇在上一篇的基础上简单讲解如何进行增删改查操作. 一.在mybatis的配置文件conf.xml中注册xml与注解映射 <!-- 注册映射文件 --> <mappers> ...

  9. 浅谈mybatis如何半自动化解耦

    在JAVA发展过程中,涌现出一系列的ORM框架,JPA,Hibernate,Mybatis和Spring jdbc,本系列,将来研究Mybatis. 通过研究mybatis源码,可将mybatis的大 ...

随机推荐

  1. GDI+ 填充背景时,非常多时候不起作用,GDI、GDI+配合运用

    在ONDRAW中运行GDI+ 填充背景时,不起作用,不知道什么原因 [cpp] view plaincopy Graphics graphics(pDC->GetSafeHdc()); Bitm ...

  2. ASP.NET导出EXCEl方法使用EXCEl对象

    导出功能必须使用  office中EXCEl对象,整个操作如同在操作EXCEl一样,建立EXCEl应用----建立工作簿---建立sheet表单页, 代码实现过程中,如果想对单元格实现一些操作,或者汇 ...

  3. Putty以及adb网络调试

    1.什么是SSH? SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议. 传 ...

  4. 2014.8.16 if语句

    语句 if语句 大体可以分一下几种: 小知识  生成一个随机数: Random sss = new Random(); int a = sss.Next(100); Console.WriteLine ...

  5. jQuery的三种$()

    参考脚本之家“http://www.jb51.net/article/21660.htm”   $号是jQuery“类”的一个别称,$()构造了一个jQuery对象.所以,“$()”可以叫做jQuer ...

  6. 软件开发常用Linux命令

    解压缩 tar -zxvf xxx.tar.gz 文件显示及查找常用于分析log //显示file中包含aaa的行 cat <file>|grep aaa 查看cpu memory基本信息 ...

  7. ring3 dll hide

    ZwQuerySystemInformation(SystemProcessInformation,SystemInformation,Length,ReturnLength);         pS ...

  8. Hadoop学习之自定义二次排序

    一.概述    MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往 往有要对reduce输出结果进行二次排 ...

  9. win7系统下连接使用mac 蓝牙键盘(Apple Wireless Keyborad)

    这几天买了一个apple wireless keyborad 玩玩,主要是给孩子买了一个ipad 搭配上wireless keyborad让她玩app足够了,就当一部电脑用吧. 看起来挺精致的,可以了 ...

  10. 《JavaScript+DOM编程艺术》的摘要(五)-----添加insertAfter

    在JS原生里面,没有提供insertAfter这个方法,不过我们可以利用appendChild.insertBefore.parentNode这些方法创建一个insertAfter方法,代码如下: f ...