常用注解

基本注解:实现简单的增删改查操作

结果映射注解:实现结果的映射关系,也可以完成级联映射

动态SQL注解:实现动态SQL的内容

基本注解:

增加操作:@Insert

删除操作:@Delete

修改操作:@Update

查询操作:@Select

//查找所有的学生
	@Select("select * from student")
	public List<Student> findAllStu();

	//根据sid查找学生
	@Select("select * from student where sid=#{v}")
	public Student findstudent(int sid);
	
	//增加学生
	@Insert("insert into student(sname,birthday,ssex,classid) "
			+ "values(#{sname},#{birthday},#{ssex},#{classid})")
	public int insertstu(Student s);
	
	//删除学生
	@Delete("delete from student where sid=#{v}")
	public int delstu(int sid);
	
	//修改学生
	@Update("update student set sname=#{sname},birthday=#{birthday},"
			+ "ssex=#{ssex},classid=#{classid} where sid=#{sid}")
	public int updatestu(Student s);
    
    @Select("select * from student where ssex = #{xb} and"
			+ " classid=#{bj} limit #{wz},#{bc}")
	public List<Student> findStudentByssexAndClassidLimit(
			@Param("xb") String ssex,
			@Param("bj") int calssid,
			@Param("wz") int weizhi,
			@Param("bc") int buchang
			);

主键回填:完成数据库自增主键的回填

//主键回填
	@Insert("insert into student(sname,birthday,ssex,classid) "
			+ "values(#{sname},#{birthday},#{ssex},#{classid})")
	@Options(useGeneratedKeys = true,keyProperty = "sid")
	public int addStudent(Student s);

主键自增:

@SelectKey ( statement = "自增规则", keyProperty

= "主键属性", resultType = 结果类型, before = true )

结果映射注解

@Results结果映射

功能:完成数据库字段和JavaBean属性的映射关系

说明:每个@Results可以包含多个@Result,其中通过id属性来判断是否为主键

@Results({ @Result(id = 是否为主键, column = "字段", property = "属性" ) })

@Results复用

一对一映射:

@One( Select= 一对一查询方法, fetchType = FetchType.EAGER)

FetchType.EAGER:即时加载,FetchType.LAZY:延时加载

一对多映射:

@Many( Select= 一对多查询方法, fetchType = FetchType.EAGER)

FetchType.EAGER:即时加载,FetchType.LAZY:延时加载

动态SQL注解

脚本sql:XML配置方式的动态SQL,是用<script>的方式把它照搬过来,用注解来实现。适用于xml 配置转换到注解配置

//	动态sql
	// 方式一 脚本sql
	@Select("<script>" 
			+ "select * from student" 
			+ "<where>" 
			+ "<if test=\"ssex != null\"> and ssex = #{ssex}</if>"
			+ "<if test=\"classid != 0\"> and classid = #{classid}</if>" 
			+ "</where>" 
			+ "</script>")
	public List<Student> findStudent(Student s);

方法中构建sql:

@SelectProvider

@InsertProvider

@UpdateProvider

@DeleteProvider

这些方法的使用

// 方式二 在方法中创建动态sql语句
	@SelectProvider(type = StudentSql.class, method = "getSelectStudentSql")
	public List<Student> findStudentFunc(Student s);

	// 方式三 构造器生成动态sql语句 将方式二进行封装-- 构造器
	@SelectProvider(type = StudentSql.class, method = "getGZQSelectStudentSql")
	public List<Student> getGZQSelectStudentSql(Student s);
	
	@InsertProvider(type=StudentSql.class,method="getGZQAddStudentSql")
	public int getGZQAddStudentSql(Student s);
	
	@DeleteProvider(type=StudentSql.class,method="getGZQDeleteStudentSql")
	public int getGZQDeleteStudentSql(int sid);
	
	@UpdateProvider(type=StudentSql.class,method="getGZQUpdateStudentSql")
	public int updateStudentGZQ(Student s);
	

	//内部类
	class StudentSql {
		
		public String getSelectStudentSql(Student s) {
			String sql = "select * from student where 1=1 ";		
			if(s.getSsex() != null) {
				sql += " and ssex = #{ssex}";
			}	
			if(s.getClassid() != 0) {
				sql += " and classid = #{classid}";
			}
			return sql;
		}

		
		//构造器
		public String getGZQSelectStudentSql(Student s) {
			return new SQL() {
				{
					//字段名
					SELECT("sid,sname");
					SELECT("birthday");
					SELECT("ssex,classid");
					//表
					FROM("student");
					//条件
					if(s.getSsex()!=null) {
						WHERE("ssex=#{ssex}");
					}
					if(s.getClassid()!=0) {
						WHERE("classid=#{classid}");
					}
				}
			}.toString();
		}

		public String getGZQAddStudentSql(Student s) {
			return new SQL() {
				{
					INSERT_INTO("student");
					if(s.getSid()!=0) {
						VALUES("sid","#{sid}");
					}
					if(s.getSname()!=null) {
						VALUES("sname","#{sname}");
					}
					if(s.getBirthday()!=null) {
						VALUES("birthday","#{birthday}");
					}
					if(s.getClassid()!=0) {
						VALUES("classid","#{classid}");
					}
				}
			}.toString();
		}
		
		public String getGZQUpdateStudentSql(Student s) {
			return new SQL() {
				
				{
					UPDATE("student");
					if(s.getSname() != null) {
						SET("sname = #{sname}");
					}
					if(s.getBirthday() != null) {
						SET("birthday=#{birthday}");
					}
					if(s.getSsex() != null) {
						SET("ssex= #{ssex}");
					}
					if(s.getClassid() != 0) {
						SET("classid=#{classid}");
					}
					WHERE("sid = #{sid}");
					
				}
				
				
			}.toString();
		}
	
		public String getGZQDeleteStudentSql(int sid) {
			return new SQL() {
				
				{
					DELETE_FROM("student");
					WHERE("sid = #{v}");
				}
				
			}.toString();
		}
	

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部