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  测试结果:

转载于:https://my.oschina.net/u/1781072/blog/542610

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值