1、MyBatis对数据库表进行增/删/改/查

前一篇使用基于XML的方式实现对数据库的增/删/改/查

以下我们来看怎么使用注解的方式实现对数据库表的增/删/改/查

1.1  首先须要定义映射sql的接口。代码例如以下:

package org.guus.inter;

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 org.guus.bean.User; /**
*
* @描写叙述:定义sql映射的接口,使用注解指明方法要运行的SQL
* @author Guus
* @date 2015年8月7日
*/
public interface UserMapperInterface { //使用@Insert注解指明add方法要运行的SQL
@Insert("insert into users(name, age) values(#{name}, #{age})")
public int add(User user); //使用@Delete注解指明deleteById方法要运行的SQL
@Delete("delete from users where id=#{id}")
public int deleteById(int id); //使用@Update注解指明update方法要运行的SQL
@Update("update users set name=#{name},age=#{age} where id=#{id}")
public int update(User user); //使用@Select注解指明getById方法要运行的SQL
@Select("select * from users where id=#{id}")
public User getById(int id); //使用@Select注解指明getAll方法要运行的SQL
@Select("select * from users")
public List<User> getAll();
}

1.2  接着须要在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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="2015" />
</dataSource>
</environment>
</environments> <mappers>
<!-- 注冊userMapper.xml文件, userMapper.xml位于org.guus.mapping这个包下,
所以resource写成org/guus/mapping/userMapper.xml -->
<mapper resource="org/guus/mapping/userMapper.xml" />
<!-- 注冊UserMapper映射接口-->
<mapper class="org.guus.inter.UserMapperInterface"/>
</mappers> </configuration>

1.3  以下我们编写測试类进行測试 ,測试类中用的SessionUtil是一个获取Session的工具类,详细见:MyBatis
-- 对表进行增删改查(基于XML的实现)

package org.guus.test;

import java.io.IOException;
import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.guus.bean.User;
import org.guus.inter.UserMapperInterface;
import org.guus.utils.SessionUtil;
import org.junit.Test; /**
*
* @描写叙述:測试MyBatis的CURD操作 -- 基于注解
* @author Guus
* @date 2015年8月7日
*/
public class TestCURD2 { @Test
public void Add() throws IOException{
SqlSession sqlSession = SessionUtil.getSqlSession(true); //true代表自己主动提交事务
//得到UserMapperI接口的实现类对象,UserMapperI接口的实现类对象由sqlSession.getMapper(UserMapperI.class)动态构建出来
UserMapperInterface mapper = sqlSession.getMapper(UserMapperInterface.class);
User user = new User();
user.setName("Guus3");
user.setAge(3);
//运行插入操作
int retResult = mapper.add(user);
//使用SqlSession运行完SQL之后须要关闭SqlSession
sqlSession.close();
System.out.println("Add操作返回值----> "+retResult);
} @Test
public void Update() throws IOException{
SqlSession sqlSession = SessionUtil.getSqlSession(true);
UserMapperInterface mapper = sqlSession.getMapper(UserMapperInterface.class);
User user = new User();
user.setId(3);
user.setName("Guus333");
user.setAge(4);
//运行改动操作
int retResult = mapper.update(user);
sqlSession.close();
System.out.println("Update操作返回值----> "+retResult);
} @Test
public void Delete() throws IOException{
SqlSession sqlSession = SessionUtil.getSqlSession(true);
UserMapperInterface mapper = sqlSession.getMapper(UserMapperInterface.class);
//运行删除操作
int retResult = mapper.deleteById(2);
sqlSession.close();
System.out.println("Delete操作返回值----> "+retResult);
} @Test
public void GetAll() throws IOException{
SqlSession sqlSession = SessionUtil.getSqlSession();
UserMapperInterface mapper = sqlSession.getMapper(UserMapperInterface.class);
//运行查询操作。将查询结果自己主动封装成List<User>返回
List<User> lstUsers = mapper.getAll();
sqlSession.close();
System.out.println("GetAll操作返回值----> "+lstUsers);
}
}

1.4  測试结果:

MyBatis -- 对表进行增删改查(基于注解的实现)的更多相关文章

  1. MyBatis简单的增删改查以及简单的分页查询实现

    MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...

  2. Mybatis入门之增删改查

    Mybatis入门之增删改查 Mybatis如果操作成功,但是数据库没有更新那就是得添加事务了.(增删改都要添加)----- 浪费了我40多分钟怀疑人生后来去百度... 导入包: 引入配置文件: sq ...

  3. Mybatis实现简单增删改查

    Mybatis的简单应用 学习内容: 需求 环境准备 代码 总结: 学习内容: 需求 使用Mybatis实现简单增删改查(以下是在IDEA中实现的,其他开发工具中,代码一样) jar 包下载:http ...

  4. 基于SSM之Mybatis接口实现增删改查(CRUD)功能

    国庆已过,要安心的学习了. SSM框架以前做过基本的了解,相比于ssh它更为优秀. 现基于JAVA应用程序用Mybatis接口简单的实现CRUD功能: 基本结构: (PS:其实这个就是用的Mapper ...

  5. mybatis中的增删改查操作

    在这一个部分,主要进行增删改查的示例书写. 增删改查可以基于xml的,也可以基于注解的方式. 一:对单条数据的查询 1.目录结构 这个使得目录更加清晰 2.User.java 这个使用以前的user表 ...

  6. SpringMVC,MyBatis商品的增删改查

    一.需求 商品的增删改查 二.工程结构 三.代码 1.Mapper层 (1) ItemsMapperCustom.java package com.tony.ssm.mapper; import ja ...

  7. SpringBoot2+Druid+MyBatis+MySql实现增删改查

    1.配置pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...

  8. Mybatis的简单增删改查

    刚开始学习Mybatis可以先看下官方文档,MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis避免了几乎所有的JDBC代码和手工设置参数以及抽取结果集.MyBat ...

  9. ztree使用系列三(ztree与springmvc+spring+mybatis整合实现增删改查)

    在springmvc+spring+mybatis里整合ztree实现增删改查,上一篇已经写了demo,以下就仅仅贴出各层实现功能的代码: Jsp页面实现功能的js代码例如以下: <script ...

随机推荐

  1. drupal 8——打补丁(patch)

    druapl 的核心可能会有漏洞,这时就需要我们去打补丁.很多补丁都已经有人写好了,我这里讲的就是如何去打这些已经写好的补丁. 对于这个问题:drupal8 核心有bug导致了两个相同的错误提示的出现 ...

  2. Android开发笔记(8)——调用子Activity

     转载请注明:http://www.cnblogs.com/igoslly/p/6853730.html  调用子Activity 需要子Activity返回值 MainActivity使用start ...

  3. android studio 控件提示大写

    方法一: 在第一行找到File进入找到setting,找到code completion 右侧复选框 选择-->None—->ok 方法二:<item name="andr ...

  4. Centos6.6 安装nfs网络文件系统

    一.介绍 nfs网络文件系统的,大部分用在内网文件共享,比如,对集群上传文件做共享,经常用在图片部分,当然数据量大了还是要做分离,做为专门的接口比较好,介绍一下基本安装环境: 1)Cnetos6.6 ...

  5. Codeforces_731F_(前缀和)

    F. Video Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. resize监听div的size变化

    具体实现分两类, ie9-10 默认支持div的resize事件,可以直接通过div.attachEvent('onresize', handler);的方式实现 其它浏览器 通过在div中添加一个内 ...

  7. [forward]警惕UNIX下的LD_PRELOAD环境变量

    From: https://blog.csdn.net/haoel/article/details/1602108 警惕UNIX下的LD_PRELOAD环境变量 前言 也许这个话题并不新鲜,因为LD_ ...

  8. [C#] Linq 动态条件查询

    应用背景:以货品为例,在基础数据中配置货品的判断规则,要根据这个规则筛选出符合条件的集合. 创建货品类 public class Product { public string Name { get; ...

  9. HUD 1426 Sudoku Killer (DFS)

    链接 : Here! 思路 : 记录下所有 "?" , 出现的位置, 然后 $DFS$ 一下, 对于每个位置来说都可以填充 $9$ 种数值, 然后对于判断填充是否合法需要三个标记数 ...

  10. cuda npp库旋转图片

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h&g ...