MyBatis学习笔记-1

MyBatis与jpa的比较:

mybatis的优势在于SQL的自由度上,SQL优化和返回对象的大小都是可控的。spring-data-JPA则在开发效率上有优势。

mybatis在springboot中开启驼峰式命名:

mybatis.configuration.map-underscore-to-camel-case=true

MyBatis的映射:

普通映射

@Select("select * from mybatis_Student where id=#{id}")

public Student getStudent(int id);

@Insert("insert into mybatis_Student (name, age, remark, pic,grade_id,address_id) values (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id})")

public int insert(Student student);

@Update("update mybatis_Student set name=#{name},age=#{age} where id=#{id}")

public int update(Student student);

@Delete("delete from mybatis_Student where id=#{id}")

public int delete(int id)

结果集映射

@Select("select * from mybatis_Student")

@Results({

@Result(id=true,property="id",column="id"),

@Result(property="name",column="name"),

@Result(property="age",column="age")

})

public List<Student> getAllStudents();

关系映射

a.一对一

@Select("select * from mybatis_Student")

@Results({

@Result(id=true,property="id",column="id"),

@Result(property="name",column="name"),

@Result(property="age",column="age"),

@Result(property="address",column="address_id",one=@One(select="com.skymr.mybatis.mappers.AddressMapper.getAddress"))

})

public List<Student> getAllStudents();

b.一对多

@Select("select * from mybatis_grade where id=#{id}")

@Results({

@Result(id=true,column="id",property="id"),

@Result(column="grade_name",property="gradeName"),

@Result(property="students",column="id",many=@Many(select="com.skymr.mybatis.mappers.Student2Mapper.getStudentsByGradeId"))

})

public Grade getGrade(int id);

 

利用@Provider实现动态SQL

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值