JavaEE简单示例——MyBatis的注解式开发对单表操作的详细介绍

梦想不抛弃苦心追求的人,只要不停止追求,你们会沐浴在梦想的光辉之中。再美好的梦想与目标,再完美的计划和方案,如果不能尽快在行动中落实,最终只能是纸上谈兵,空想一番。只要瞄准了大方向,坚持不懈地做下去,才能够扫除挡在梦想前面的障碍,实现美好的人生蓝图。JavaEE简单示例——MyBatis的注解式开发对单表操作的详细介绍,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

简单介绍:

在上一个章节我们简单的介绍了关于MyBatis注解式开发的使用方法,这次我们详细的演示如何使用注解式开发来进行单表的简单查询

使用方法:

其实使用的方法非常的简答,就是使用我们在MySQL中的关键字首字母大写之后作为注解的名称,注解的参数就是我们要执行的SQL语句

增删改查:

基于注解的增删改查都非常的简单,格式也是一样的,所以这里我们放在一起展示包含了四种操作的接口文件和测试类:

接口文件:

package Mappers;

import com.mybatis.POJO.student;
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 java.util.List;

public interface stuMapper {
    @Select("select * from student;")
    public List<student> selectAll();

    @Update("update student set name = '赵六' where id = #{id}")
    public int UpdateStudent(int i);

    @Insert("insert into student values (#{id},#{name},#{password},null,null)")
    public int InsertStudent(student s);

    @Delete("delete from student where id = #{id}")
    public int DeleteStudent(int i);
}

测试类:

package Mappers;

import com.mybatis.POJO.student;
import junit.framework.TestCase;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;

public class studentMapperTest {
    SqlSession session = null;
    stuMapper mapper = null;

    @Before
    public void setUp() throws Exception {
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(stream);
        session = build.openSession(true);
        mapper = session.getMapper(stuMapper.class);
    }

    @Test
    public void testSelectAll() {
        for (student student : mapper.selectAll()) {
            System.out.println(student.toString());
        }
    }

    @Test
    public void testUpdateStudent(){
        int i = mapper.UpdateStudent(4);
        System.out.println(i);
    }
    @Test
    public void InsertStudent(){
        student student = new student();
        student.setId(5);
        student.setName("张三");
        student.setPassword("123456");
        int i = mapper.InsertStudent(student);
        if(i >= 0){
            System.out.println("插入成功!");
        }else {
            System.out.println("插入失败!");
        }
    }
    @Test
    public void DeleteStudent(){
        int i = mapper.DeleteStudent(5);
        if(i >= 0){
            System.out.println("删除成功!");
        }else {
            System.out.println("删除失败!");
        }
    }
}

 一个特殊的注解@Param注解:在SQL语句中使用多个参数的时候使用:

在有些时候,我们的查询SQL语句需要不止一个参数,但是这多个参数并不能通过类的属性进行传递的时候,大部分情况是我们的查询条件通过多个形参进行传递的时候,我们会需要使用@Param注解进行多个参数的传递。

在传递多个参数的时候,我们需要确定两个问题:1.如何确定参数传递到SQL语句中的顺序。2.如何区分这两个参数。

接口文件:

package Mappers;

import com.mybatis.POJO.student;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface stuMapper {
    @Select("select * from student where id = #{id} and name = #{name}")
    public List<student> selectStudentByIdAndName(@Param("id")int id,@Param("name")String name);
}

在接口文件中,可以很明显的看出来,我们需要根据id和name两个值进行查询,我们的@Parma注解是写在接口方法的参数前面,@Param的值就是我们为这个参数起的别名,这个别名需要我们使用在SQL语句中,是用来将我们的形参拼接到SQL语句中的。 

测试类:

package Mappers;

import com.mybatis.POJO.student;
import junit.framework.TestCase;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;

public class studentMapperTest {
    SqlSession session = null;
    stuMapper mapper = null;

    @Before
    public void setUp() throws Exception {
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(stream);
        session = build.openSession(true);
        mapper = session.getMapper(stuMapper.class);
    }
    @Test
    public void selectStudentByIdAndName(){
        for (student student : mapper.selectStudentByIdAndName(1, "张三")) {
            System.out.println(student.toString());
        }
    }
}

注意点:

使用注解式开发的目的就是用来简化我们的开发过程,所以注解式开发的单表操作是非常简单的,只需要注意我们使用的注解名字和我们的SQL语句对应关系就可以

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/153301.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!