MyBatis -- 对表进行增删改查(基于注解的实现)
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 -- 对表进行增删改查(基于注解的实现)的更多相关文章
- MyBatis简单的增删改查以及简单的分页查询实现
MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...
- Mybatis入门之增删改查
Mybatis入门之增删改查 Mybatis如果操作成功,但是数据库没有更新那就是得添加事务了.(增删改都要添加)----- 浪费了我40多分钟怀疑人生后来去百度... 导入包: 引入配置文件: sq ...
- Mybatis实现简单增删改查
Mybatis的简单应用 学习内容: 需求 环境准备 代码 总结: 学习内容: 需求 使用Mybatis实现简单增删改查(以下是在IDEA中实现的,其他开发工具中,代码一样) jar 包下载:http ...
- 基于SSM之Mybatis接口实现增删改查(CRUD)功能
国庆已过,要安心的学习了. SSM框架以前做过基本的了解,相比于ssh它更为优秀. 现基于JAVA应用程序用Mybatis接口简单的实现CRUD功能: 基本结构: (PS:其实这个就是用的Mapper ...
- mybatis中的增删改查操作
在这一个部分,主要进行增删改查的示例书写. 增删改查可以基于xml的,也可以基于注解的方式. 一:对单条数据的查询 1.目录结构 这个使得目录更加清晰 2.User.java 这个使用以前的user表 ...
- SpringMVC,MyBatis商品的增删改查
一.需求 商品的增删改查 二.工程结构 三.代码 1.Mapper层 (1) ItemsMapperCustom.java package com.tony.ssm.mapper; import ja ...
- SpringBoot2+Druid+MyBatis+MySql实现增删改查
1.配置pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- Mybatis的简单增删改查
刚开始学习Mybatis可以先看下官方文档,MyBatis是支持定制化SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis避免了几乎所有的JDBC代码和手工设置参数以及抽取结果集.MyBat ...
- ztree使用系列三(ztree与springmvc+spring+mybatis整合实现增删改查)
在springmvc+spring+mybatis里整合ztree实现增删改查,上一篇已经写了demo,以下就仅仅贴出各层实现功能的代码: Jsp页面实现功能的js代码例如以下: <script ...
随机推荐
- Android基础TOP2_1:输出系统时间
Activity: <TextView android:id="@+id/tv" android:layout_width="wrap_content" ...
- PHP MySQL 连接数据库,进行增、删、改、查、操作
<table width="100%" border="1" cellpadding="0" cellspacing="0& ...
- 如何在Linuxt系统下运行maven项目
如何在Linuxt系统下运行maven项目 我们知道现在利用MAVEN来管理JAVA项目是非常常见的.比如公司一般都有一个自己的MAVEN仓库,通过MAVEN仓库来解决我们的项目依赖,更加方便的构建项 ...
- 轻松理解 Android Binder,只需要读这一篇
在 Android 系统中,Binder 起着非常重要的作用,它是整个系统 IPC 的基石.网上已经有很多文章讲述 Binder 的原理,有的讲的比较浅显,没有触及到关键,有的讲的太过于深入底层,难以 ...
- 为什么有些异常throw出去需要在函数头用throws声明,一些就不用
throw new IllegalStateException(".");不用在函数头声明throws IllegalStateExceptionthrow new IOExcep ...
- CAD动态绘制带面积周长的圆(com接口)
CAD绘制图像的过程中,画圆的情况是非常常见的,用户可以在控件视区点取任意一点做为圆心,再动态点取半径绘制圆. 主要用到函数说明: _DMxDrawX::DrawCircle 绘制一个圆.详细说明如下 ...
- 最适合初学者的Linux运维学习教程2018版
Linux运维工程师是一个新颖岗位,现在非常吃香,目前从行业的角度分析,随着国内软件行业不断发展壮大,越来越多复杂系统应运而生,为了保证系统稳定运行,必须要有足够多的Linux运维工程师.维护是软件生 ...
- Linux如何查看端口(转)
Linux如何查看端口 1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000 # lsof -i:8000 COMMAND PID USER ...
- 考试T1总结(又CE?!)
考试T1CE... 最近不适合考试 T1 扶苏是个喜欢一边听古风歌一边写数学题的人,所以这道题其实是五三原题.歌曲中的主人公看着墙边的海棠花,想起当年他其实和自己沿着墙边种了一排海棠,但是如今都已枯萎 ...
- 【原创】使用JS封装的一个小型游戏引擎及源码分享
1 /** * @description: 引擎的设计与实现 * @user: xiugang * @time: 2018/10/01 */ /* * V1.0: 引擎实现的基本模块思路 * 1.创建 ...