简单介绍:
在上一个章节我们简单的介绍了关于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语句对应关系就可以