mybatis-注解的使用
什么注解
Java 语言、方法、变量、包等都可以被参数和doc 等不同的参数和文档,Java 语言可以通过文件吸收获取类内容。在编译器生成,时间可以被嵌入到字节码中。
通俗来说就是:在类、方法、变量上的特殊标识,进而产生特殊效果
//我们常见的就是在重写时见到的
@Override
public void run(){
}
mybatis-注解的使用
在mybatis中使用注解可以不用写mapper配置文件而是直接在接口中方法上加上注解写SQL语句
如下:
注解的多表联插
package day01.dao;
import day01.Student;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface StudentDao {
@Insert("insert into student value(#{name},#{sex})")
void insert(Student student);
@Delete("delete from student where name=#{name}")
void delete(String name);
@Update("update student set sex=#{sex} where name=#{name}")
void update(Student student);
@Select("select * from student where name=#{name}")
Student selectOne(String name);
@Select("select * from course")
@Results({
@Result(column = "name",property = "name"),
@Result(column = "course",property = "course"),
@Result(
property = "course",
javaType=Student.class,
column ="cname",
one = @One(select = "selectOne")
)
})
List<Student> selectAll();
}
注意:需要在主配置文件中指定该接口或者所在包
<mappers>
<!--接口位置-->
<mapper class="com.StudentDao">
<!--检查包位置-->
<package name="day01.dao"/>
</mappers>
在使用mapper配置文件和注解时使用的查询方法
至于使用哪种就看你自己