目录
5.lombok 插件 大大简化代码编写:注解生成get/set方法
1.mybatis注解操作步骤:
①(mybatis.xml中)mapper 接口,而不是mapper .xml 文件
<mappers>
<mapper resource="GradeMapper.xml"></mapper>
<mapper class="com.yuyu.dao.StudentDao"></mapper>
</mappers>
②在接口上使用注解 (StudentDao.java[interface])
@Insert("insert into student(studentno,stuname,stuage) values(#{studentNo},#{stuName},#{age})")
@Options(useGeneratedKeys = true,keyProperty = "studentId")//获得新增数据的id
public int insertStu(Student student);
③直接通过接口调用方法操作数据库(Demo.java)
Student student = new Student();
student.setStudentNo("a1101");
student.setStuName("abc");
student.setAge(18);
//在 mybatis.xml 中给类起了别名:studentDao = com.yuyu.bean.studentDao
int i = studentDao.insertStu(student);
session.commit();
System.out.println("i="+i+",id="+student.getStudentId());
修改 @Update()
//(StudentDao.java[interface])
@Update("update student set studentno=#{studentNo},stuname=#{stuName} where studentid=#{studentId}")
public int updateStu(Student student);
//(Demo.java)
Student student = new Student();
student.setStudentId(119);
student.setStudentNo("a1101");
student.setStuName("张娜");
student.setAge(20);
int i = studentDao.updateStu(student);
session.commit();
System.out.println("i="+i);
结果@Results()
//(StudentDao.java[interface])
@Select("select * from student")
@Results({
@Result(column = "stuage",property = "age")
})
public List<Student> findall();
//(Demo.java)
List<Student> students = studentDao.findall();
for (Student student : students) {
System.out.println(student);
}
查询@Select()
//(StudentDao.java[interface])
@Select("select count(*) from student")
public int totalCount();
//(Demo.java)
int i = studentDao.totalCount();
System.out.println("total="+i);
删除@Delete
//(StudentDao.java[interface])
@Delete("delete from student where studentid=#{sid}")
public int deleteStu(int sid);
//(Demo.java)
int i = studentDao.deleteStu(119);
session.commit();
System.out.println("i="+i);
查询结果没有对应属性
//(StudentDao.java[interface])
//计算id的最大值,最小值,平均值
@Select("select max(studentid) max,min(studentid) min,avg(studentid) avg from student")
public Map total2();
//(Demo.java)
Map map = studentDao.total2();
Set<Map.Entry> set = map.entrySet();
for (Map.Entry entry : set) {
System.out.println(entry);
}
2.注解使用自定义 sql 类,方便 sql 管理
@SelectProvider(type = ⾃定义sql所在的类.class, method = "sql定义的⽅法")
//实例:
@SelectProvider(type = SqlTemp.class,method ="getall44")
@InsertProvider(type = SqlTemp.class,method = "insert")
@DeleteProvider(type = SqlTemp.class,method = "delete")
@UpdateProvider(type = SqlTemp.class,method = "update")
①自定义 sql 类,用来定义sql语句
//(sqlUtil.java)
public class SqlUtil {
//返回值一定要是字符串
public String insertMethod(){
return "insert into student(studentno,stuname,stuage) values(#{studentNo},#{stuName},#{age})";
}
public String updateMethod(){
return "update student set studentno=#{studentNo},stuname=#{stuName} where studentid=#{studentId}";
}
public String selectall(){
return "select * from student";
}
}
②接口使用注解
//(StudentDao2.java[interface])
@InsertProvider(type = SqlUtil.class,method = "insertMethod")
@Options(useGeneratedKeys = true,keyProperty = "studentId")//获得新增数据的id
public int insertStu(Student student);
③直接通过接口调用方法
//(Demo2.java)
Student student = new Student();
student.setStudentNo("b1101");
student.setStuName("abc");
student.setAge(18);
int i = studentDao2.insertStu(student);
session.commit();
System.out.println("i="+i+",id="+student.getStudentId());
@UpdateProvider()
//接口使用注解 (StudentDao2.java[interface])
@UpdateProvider(type = SqlUtil.class,method = "updateMethod")
public int updateStu(Student student);
//直接通过接口调用方法(Demo2.java)
Student student = new Student();
student.setStudentId(120);
student.setStudentNo("a1101");
student.setStuName("张娜");
student.setAge(20);
int i = studentDao2.updateStu(student);
session.commit();
System.out.println("i="+i);
@Options()、@CacheNamespace()
//接口使用注解 (StudentDao2.java[interface])
@SelectProvider(type = SqlUtil.class,method = "selectall")
@Results({
@Result(column = "stuage",property = "age")
})
@Options(useCache = true,flushCache = Options.FlushCachePolicy.FALSE,timeout = 9999999)
//注意:需要和@CacheNamespace⼀起使⽤,并且对象需要实现序列化接⼝ 在 public interface StudentDao2 {} 前添加注解 @CacheNamespace
public List<Student> findall();
//直接通过接口调用方法(Demo2.java)
List<Student> students = studentDao.findall();
for (Student student : students) {
System.out.println(student);
}
3.@ResultMap 注解
接口使用注解
//(StudentDao2.java[interface])
//两表联查
//查询所有学生以及其班级信息
@ResultMap("com.xzk.dao.StudentDao2.rs3")
@Select("select * from student s,grade g where s.gradeid=g.gid")
public List<Student> getAllStudent();
结果在 GradeMapper.xml 中完成映射
<mapper namespace="com.yuyu.dao.StudentDao2">
<resultMap id="rs3" type="com.yuyu.bean.Student">
<id column="studentid" property="studentId"></id>
<result column="stuname" property="stuName"></result>
<result column="studentno" property="studentNo"></result>
<result column="stuage" property="age"></result>
<association property="grade" javaType="com.yuyu.bean.Grade">
<id column="gid" property="gid"></id>
<result column="gname" property="gname"></result>
</association>
</resultMap>
</mapper>
直接通过接口调用方法
//(Demo2.java)
List<Student> students = studentDao.getAllStudent();
for (Student student : students) {
System.out.println(student+","+student.getGrade().getGname());
}
4.绑定参数
接口使用注解
//(StudentDao2.java[interface])
@InsertProvider(type = SqlUtil.class,method = "insertMethod")
@Options(useGeneratedKeys = true,keyProperty = "studentId")//获得新增数据的id
public int insertParam(@Param("studentNo") String stuno,@Param("stuName") String stuname,@Param("age") int age);
直接通过接口调用方法
//(Demo2.java)
int i = studentDao.insertParam("c101","广坤",50);
session.commit();
System.out.println("i="+i);
5.lombok 插件 大大简化代码编写:注解生成get/set方法
步骤:
①添加 plugin
下载地址:https://plugins.jetbrains.com/
②添加依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
③lombok注解
@Data 注解在类上;提供类所有属性的 getting 和 setting ⽅法,此外还提供了equals、canEqual、
hashCode、toString ⽅法
@Setter :注解在属性上;为属性提供 setting ⽅法
@Getter :注解在属性上;为属性提供 getting ⽅法
@Log4j :注解在类上;为类提供⼀个 属性名为log 的 log4j ⽇志对象
@NoArgsConstructor :注解在类上;为类提供⼀个⽆参的构造⽅法
@AllArgsConstructor :注解在类上;为类提供⼀个全参的构造⽅法
@Cleanup : 可以关闭流
@Builder : 被注解的类加个构造者模式
@Synchronized : 加个同步锁
@SneakyThrows : 等同于try/catch 捕获异常
@NonNull : 如果给参数加个这个注解 参数为null会抛出空指针异常
@Value : 注解和@Data类似,区别在于它会把所有成员变量默认定义为private final修饰,并且不会⽣成set⽅法。
@ToString 重写toString()⽅法
④实体类上使用注解(没有get/set/构造方法)
@Data
@NoArgsConstructor
@AllArgsConstructor
//@Value
public class Student implements Serializable {
private int studentId; //属性名=列名
private String studentNo;
private String stuName;
//private int stuAge;
private int age;
private Grade grade;
}