MyBatis中常见的注解
@Insert:设置添加数据的SQL语句注解,标注在数据访问层的方法上
@Insert("insert into student_tab values(null,#{stuname},#{stuage},#{stuaddress},#{password});")
boolean insertStudent(StudentBean studentBean);
@Upadte:设置修改数据的SQL语句注解,标注在数据访问层的方法上
@Update("update student_tab set stuname=#{stuname},stuage=#{stuage},stuaddress=#{stuaddress},password=#{password} where stuid=#{stuid};")
boolean updateStuednt(StudentBean studentBean);
@Delete:设置删除数据的SQL语句注解,标注在数据访问层的方法上
@Delete("delete from student_tab where stuid=#{stuid};")
boolean deleteStudent(int stuid);
@Select:设置查询数据的SQL语句注解,标注在数据访问层的方法上;
注意:当数据库表中的列名称和javaBean类中的成员变量名称不同时@Select注解上要有一个@Results注解接收查询结果集,如果相同则不需要
//根据id进行查询
@Select("select * from student_tab where stuid=#{stuid};")
StudentBean selectOne(int stuid);
//查询所有
@Select("select * from student_tab;")
List<StudentBean> selectAll();
@Results:设置查询结果集,成员变量与数据库表列名的映射关系,id=xxxx,为注解设置名称,value{@Result(column="数据库表列名",property="成员变量名称")},标注在数据访问层与@Select注解上面
@Results(id = "stumap1",value = {
@Result(column = "stu_id",property = "stuid"),
@Result(column = "stu_name",property = "stuname"),
@Result(column = "stu_pass",property = "stupass"),
@Result(column = "stu_age",property = "stuage"),
@Result(column = "stu_address",property = "stuaddress")
})
@Select("select * from t_student where stu_id=#{stuid};")
StudentBean selectStudentById(int stuid);
@Results(id = "stumap2",value = {
@Result(column = "stu_id",property = "stuid"),
@Result(column = "stu_name",property = "stuname"),
@Result(column = "stu_pass",property = "stupass"),
@Result(column = "stu_age",property = "stuage"),
@Result(column = "stu_address",property = "stuaddress")
})
@Select("select * from t_student;")
List<StudentBean> selectStudent();
@ResultMap:引用SQL映射文件中的ResultMap,value="SQL映射文件的中ResultMap的id值",标注在数据访问层与@Select注解上
Spring框架中常见的注解
@Component:创建对象,name="设置对象名称",标注在类上面,但它是一个泛化的概念,仅仅表示一个组件(Bean),并且可以作用在任何层次【控制层/业务层/数据访问层】,此注解若不设置name属性,则默认对象名称为类名称首字母小写
@Component
@Component("student")
public class StudentBean {
public void testStudent(){
System.out.println("StudentBean的实例方法");
}
}
@Autowired:完成依赖注入,默认按照Bean的类型进行装配依赖注入,Bean的属性变量,属性的set方法及构造方法进行标注
package com.wangxing.demo2;
import org.springframework.stereotype.Component;
@Component("student")
public class StudentBean {
public String getStringInfo(){
return "网星";
}
}
package com.wangxing.demo2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("person")
public class PersonBean {
@Autowired
private StudentBean studentBean;
public void testPerson(){
System.out.println("PersonBean的实例方法----"+studentBean.getStringInfo());
}
}
@Resource:完成依赖注入,先按照Bean实例名称进行装配,后按照Bean类型进行装配,@Resource有两个重要属性:name和type,如果指定name则按照Bean实例名称进行装配,如果指定Type则按照Bean类型进行装配,如果都不指定,先按照Bean实例名称进行装配,如果不能匹配,则按照Bean类型进行装配,如果都指定,则实例名称和类型都需要匹配,否则无法装配,会抛出 NoSuchBeanDefinitionException 异常。
@Component("student")
public class StudentBean {
public String getString