一.Mybatis注解开发单表操作
MyBatis的常用注解
这几年来注解开发越来越流行,Mybatis也可以使用注解开发方式,这样我们就可以减少编写Mapper
映射文件了。我们先围绕一些基本的CRUD来学习,再学习复杂映射多表操作。
@Insert:实现新增
@Update:实现更新
@Delete:实现删除
@Select:实现查询
@Result:实现结果集封装
@Results:可以与@Result 一起使用,封装多个结果集
@One:实现一对一结果集封装
@Many:实现一对多结果集封装
public interface StudentMapper {
@Select("select * from student")
public List<Student> findStudenAll();
@Select("select * from student where ssex=#{xingbie} limit #{page},#{neirong}")
public List<Student> findStudentparm
(@Param("xingbie")String s1 , //起别名
@Param("page") int x,
@Param("neirong") int y);
@Insert("insert into student (sname,birthday,ssex,classid) "
+ "values(#{sname},#{birthday},#{ssex},#{classid})")
@Options(useGeneratedKeys = true ,keyProperty = "sid") //主键回填
public int addStudent(Student s);
@Delete("delete from student where sid=#{v}")
public int deletestu(int a);
@Update ("update student set sname=#{sname},birthday=#{birthday},"
+ "ssex=#{ssex},classid=#{classid} where sid=#{sid}")
public int updateStu(Student s);
@Results(id = "smmap", value = {
@Result( column = "sm_name",
property = "smname")
})
@Select("select * from schoolmaster ")
public List<SchoolMaster> findSm();
@Select("select * from schoolmaster where smid=#{v}")
@ResultMap("smmap")
public SchoolMaster findonesm(int smid);
// public List<Student> findStudentArr(int[] arr);
//xml中的 id标签标记主键
@Results({
@Result(column = "classid", property = "classid"),
@Result(column = "classid", property = "bj",
one= @One(select="com.ape.mapper.BanJiMapper.findBanJi"))
})
@Select("select * from student")
public List<Student> findstuclass();
@Select("select * from student where classid =#{}")
public List<Student> findStuBj();
//
// public List<Student> findStudentList(List<Integer> sidlist);
//
// public int addStudentList(List<Student> slist);
二注解动态sql的三种方式
注解可以简化开发操作,省略映射配置文件的编写。
脚本sql
XML配置方式的动态SQL,是 用<script>的方式把它照搬过来,用注解来实现。适用于xml 配置转换到注解配置
方法中构建sql
@SelectProvider
@InsertProvider
@UpdateProvider
@DeleteProvider
sql语句构造器:
实现动态sql的内容
代码举例:
@SelectProvider(type=NeiBuclass.class,method ="gzqSelectStu" )
public List<Student> findStuGzq(Student s);
@DeleteProvider(type=NeiBuclass.class,method = "gzqDeleteStu")
public int deleteStuGzq(int sid);
@UpdateProvider(type = NeiBuclass.class,method = "gzqUpdateStu")
public int upadateStuGzq(Student s);
@InsertProvider(type = NeiBuclass.class,method="gzqInsertStu")
public int insertStuGzq(Student s);
class NeiBuclass{
public String gzqSelectStu(Student s) {
return new SQL() {
{
SELECT ("sid");
SELECT ("sname,birthday");
SELECT ("ssex,classid");
FROM("student");
if(s.getSsex()!=null) {
WHERE("ssex=${ssex}");
}
if(s.getClassid()!=0) {
WHERE("classid =#{classid}");
}
}
}.toString();
}
public String gzqDeleteStu(int tid) {
return new SQL() {
{
DELETE_FROM("student");
WHERE ("sid=#{v}");
}
}.toString();
}
public String gzqUpdateStu(Student s) {
return new SQL() {
{
UPDATE ("student");
if(s.getSname() != null) {
SET("sname=#{sname}");
}
if(s.getBirthday()!=null) {
SET("birthday=#{birthday}");
}
if(s.getClassid()!=0) {
SET("classid=#{classid}");
}
if(s.getSsex()!=null) {
SET("ssex=#{ssex}");
}
}
}.toString();
}
public String gzqInsertStu(Student s) {
return new SQL() {
{
INSERT_INTO("student") ;
if(s.getSid()!=0) {
VALUES("sid","#{sid}");
}
if(s.getBirthday()!=null) {
VALUES("birthday","#{birthday}");
}
if(s.getClassid()!=0) {
VALUES("classid","#{classid}");
}if(s.getSname()!=null) {
VALUES("sname","#{sname}");
}if(s.getSsex()!=null) {
VALUES("ssex","#{ssex}");
}
}
}.toString();
}
}
@SelectProvider 的用法:
1创建 SQL 语句类 :该类包含需要动态生成的SQL 语句;
2:创建Mapper接口类 :该类和配置文件的接口文件一 样,用来处理数据库操作;
3:利用@SelectProvider 将 SQL 语句类和 Mapper 接 口类关联,利用 @SelectProvider 的 type 属 性和 method 属性;
4测试验证 编写测试类,测试动态生成的 SQL 语句是否准确
代码举例:
@SelectProvider(type=NeiBuclass.class,method ="gzqSelectStu" )
public List<Student> findStuGzq(Student s);
@DeleteProvider(type=NeiBuclass.class,method = "gzqDeleteStu")
public int deleteStuGzq(int sid);
@UpdateProvider(type = NeiBuclass.class,method = "gzqUpdateStu")
public int upadateStuGzq(Student s);
@InsertProvider(type = NeiBuclass.class,method="gzqInsertStu")
public int insertStuGzq(Student s);