目录
- 在MyBatis中,除了XML的映射方式,MyBatis还支持通过注解实现POJO对象和数据表之间的关系映射。使用注解时,一般将SQL语句直接写在接口上。与XML的映射方式相比,注解相对简单并且不会造成大量的开销。
- MyBatis提供了若干注解,其中常用的注解如表所示。
以上列举出了MyBatis提供的常用注解,接下来,将对这些注解作详细讲解。
1 修改配置文件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>
<!-- 配置别名-->
<typeAliases>
<package name="com.qfedu.pojo"/>
</typeAliases>
<!--设置连接数据库的环境-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/chapter04"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--引入映射文件-->
<mappers>
<mapper resource="com.qfedu.mapper/StudentMapper.xml"></mapper>
<mapper class="com.qfedu.mapper.StudentMapper"></mapper>
</mappers>
</configuration>
2 创建StudentMapper接口
package com.qfedu.mapper;
import com.qfedu.pojo.Student;
import org.apache.ibatis.annotations.*;
public interface StudentMapper {
/**
* @param sid 通过学号查询学生信息
* @return 返回学生信息
*/
@Select("select * from student where sid = #{sid}")
Student selectStudent(int sid);
/**
* @param student 插入学生信息
* @return
*/
@Insert("insert into student(sname,age,course)" + "values(#{sname},#{age},#{course})")
int insertStudent(Student student);
/**
* @param student 更新学生信息
* @return
*/
@Update("update student " + "set sname = #{sname},course = #{course} where sid = #{sid}")
int updateStudent(Student student);
/**
* @param sid 删除学生信息
* @return
*/
@Delete("delete from student where sid = #{sid} ")
int deleteStudent(int sid);
/**
* @param sname 姓名
* @param course 课程
* @return
*/
@Select("select * from student where sname = #{param01}"+"and course = #{param02}")
Student selectBySnameAndCourse(@Param("param01")String sname,@Param("param02")String course);
}
3 常用的注释
@Select
@Select注解用于映射查询语句,其作用等同于xml文件中的<select>元素。
package com.qfedu.com.qfedu.test;
import com.qfedu.mapper.StudentMapper;
import com.qfedu.pojo.Student;
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 java.io.IOException;
import java.io.InputStream;
public class TestSelect {
public static void main(String[] args) {
String resource = "mybatis-config.xml";
try {
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
Student student1 = new Student();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectBySnameAndCourse("LiSi","Java");
System.out.println(student.toString());
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Insert
@Insert注解用于映射插入语句,其作用等同于xml文件中的<insert>元素。
package com.qfedu.com.qfedu.test;
import com.qfedu.mapper.StudentMapper;
import com.qfedu.pojo.Student;
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 java.io.IOException;
import java.io.InputStream;
public class TestInsert {
public static void main(String[] args) {
String resource = "mybatis-config.xml";
try {
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
Student student1 = new Student();
student1.setSname("ZhaoBa");
student1.setAge("21");
student1.setCourse("Java");
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
int result = mapper.insertStudent(student1);
if(result>0){
System.out.println("成功插入"+result+"条数据");
}else {
System.out.println("插入操作失败");
}
sqlSession.commit();
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在以上代码中,通过调用SqlSession对象的getMapper ()方法获取StudentMapper对象,进而执行StudentMapper对象的selectStudent()方法。
@Update
@Update注解用于映射更新语句,其作用等同于xml文件中的<update>元素。
package com.qfedu.com.qfedu.test;
import com.qfedu.mapper.StudentMapper;
import com.qfedu.pojo.Student;
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 java.io.IOException;
import java.io.InputStream;
public class TestUpdate {
public static void main(String[] args) {
String resource = "mybatis-config.xml";
try {
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
Student student1 = new Student();
student1.setSid(6);
student1.setSname("WangWu");
student1.setCourse("Python");
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
int result = mapper.updateStudent(student1);
if(result>0){
System.out.println("成功更新"+result+"条数据");
}else {
System.out.println("更新操作失败");
}
sqlSession.commit();
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Delete
@Delete注解用于映射删除语句,其作用等同于xml文件中的<delete>元素。
package com.qfedu.com.qfedu.test;
import com.qfedu.mapper.StudentMapper;
import com.qfedu.pojo.Student;
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 java.io.IOException;
import java.io.InputStream;
public class TestDelete {
public static void main(String[] args) {
String resource = "mybatis-config.xml";
try {
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
int result = mapper.deleteStudent(6);
if(result>0){
System.out.println("成功删除"+result+"条数据");
}else {
System.out.println("删除操作失败");
}
sqlSession.commit();
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Param
@Param注解的功能是指定参数,通常用于SQL语句中参数比较多的情况。
package com.qfedu.com.qfedu.test;
import com.qfedu.mapper.StudentMapper;
import com.qfedu.pojo.Student;
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 java.io.IOException;
import java.io.InputStream;
public class TestFindBySid {
public static void main(String[] args) {
String resource = "mybatis-config.xml";
try {
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectStudent(1);
System.out.println(student.toString());
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}