MyBatis -- 对表进行增删改查(基于注解的实现)
来源:互联网 发布:gaga软件怎么样 编辑:程序博客网 时间:2024/05/22 00:07
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 测试结果:
3 0
- MyBatis -- 对表进行增删改查(基于注解的实现)
- 使用MyBatis对表执行增删改查操作——基于注解的实现
- MyBatis -- 对表进行增删改查(基于XML的实现)
- MyBatis第二讲学习笔记 ,使用MyBatis对表执行增删改查操作——基于注解的实现
- Mybatis-基于注解的增删改查
- Mybatis框架基于注解的方式,实对数据现增删改查
- Mybatis框架基于注解的方式,实对数据现增删改查
- MyBatis(四)增删改查--基于注解
- Mybatis注解方式进行简单的增删改查
- mybatis利用注解实现增删改查
- MyBatis使用注解实现增删改查
- Mybatis 注解实例实现增删改查
- 使用MyBatis进行对数据表的增删改查操作
- mybatis实现的对数据的增删查改
- mybatis实现的对数据的增删查改
- Mybatis实现的对数据的增删查改
- (二)hibernate基于注解对单张表的增删改查
- 基于mybatis的增删改查
- VPN学习
- 第二周作业
- 第一篇博客 :水题试练
- 第14章:时钟AnalogClock和TextClock的功能和用法
- cocos坐标详解
- MyBatis -- 对表进行增删改查(基于注解的实现)
- hdu4198
- 开源库x265的编译
- Mac管理员账户恢复,找回删除的账户
- 工作小记 之更改数据恢复,
- php会话管理
- 浅谈对主成分分析(PCA)算法的理解
- HDU - 4287 Intelligent IME 字典树
- 用apple script读取excel数据