Mybatis学习
- 以注解的方式编写SQL语句@Insert注解
- 表名数据库属性与实体类映射注解@TableName、@TableId、@TableField
- @InsertProvider @DeleteProvider @UpdateProvider @SelectProvider注解
- sql语句返回值映射至对象类中
- sql语句返回值映射至集合中
- jdbcType=varchar语句的作用
- MyBatis-plus工具之EntityWrapper(SQL语句构造器)
- MyBatisPlus的ActiveRecord实现CRUD
- Mybatis中foreach标签和选择标签
- prepend = "and" 标签
- 使用merge into(仅oracle支持)
以注解的方式编写SQL语句@Insert注解
以插入语句为例
@Insert(“insert into mp_prize_issue prize_type,apply_no values (#{prizeType},#{applyNo})”)
void issueFailureReason (@param(“prizeTpe”)String prizeType,@param(“applyNo”)String applyNo)
当注解插入<script>标签时
@Insert({"<script>insert into mp_prize_issue (prize_type)<if test = ‘applyNo != null’>apply_no</if>values (#{prizeType})<if test = ‘applyNo != null’>,#{applyNo}</if></script>"})
当插入<script>标签时,必须加上中括号{}。同时逗号的位置。
表名数据库属性与实体类映射注解@TableName、@TableId、@TableField
@TableName("t_student")//关联数据库的那张表
public class Student{
@TableId("id")//关联数据库中的主键
private String ID;
@TableField("name")//关联数据库中的字段
private String name;
}
@InsertProvider @DeleteProvider @UpdateProvider @SelectProvider注解
此类注解可以自动生成sql语句
// 动态插入部门,type表示那个类,metjod表示取那个实体类的方法
@InsertProvider(type=DeptDynaSqlProvider.class,method="insertDept")
void save(Dept dept);
// 动态修改用户
@SelectProvider(type=DeptDynaSqlProvider.class,method="updateDept")
void update(Dept dept);
// 动态插入
public class DeptynaSqlProvider{
public String insertDept(Dept dept){
return new SQL(){
{
INSERT_INTO(DEPTTABLE);
if(dept.getName() != null && !dept.getName().equals("")){
VALUES("name", "#{name}");
}
if(dept.getRemark() != null && !dept.getRemark().equals("")){
VALUES("remark", "#{remark}");
}
}
}.toString();
}
// 动态更新
public String updateDept(Dept dept){
return new SQL(){
{
UPDATE(DEPTTABLE);
if(dept.getName() != null){
SET(" name = #{name} ");
}
if(dept.getRemark() != null){
SET(" remark = #{remark} ");
}
WHERE(" id = #{id} ");
}
}.toString();
}
}
sql语句返回值映射至对象类中
@Select("select id,name from t_student")
@Results({
@Result(property = "ID",column="id"),
@Result(property = "name",column="name")
})
sql语句返回值映射至集合中
Map<String,object> queryAllStudentInfo();
@Select("select id,name from t_student")
@Results({
@Result(property = "ID",column="id"),
@Result(property = "name",column="name")
})
可以将查询的结果直接映射至map集合中。当最好select语句中,选择一个数据库一定不为空的字段。
jdbcType=varchar语句的作用
Mybatis中,当插入空值时会报错,sql语句写成
@Insert(#{name,jdbcType = varchar})此时name的值为空值时,不会报错
MyBatis-plus工具之EntityWrapper(SQL语句构造器)
注意事项:
1)使用必须配合表名数据库属性与实体类映射注解一起使用,否则无法找到对应的数据表和字段。
2)Mapper层必须继承BaseMapper
public interface BaseMapper<T>{
/*
向数据库插入一条数据,其entity为实体类,映射至数据库中
如Student类,属性Id
需加上Mybatis相应的注解
*/
Integer insert(T entity);
}
3)使用代码(其中wrapper有很多方法,请自行研究)
Wrapper<Student> wrapper = new EntityWrapper<>();
wrapper.eq("name","张三");
int result = studentMapper.insert(wrapper);//向数据库中插入名字叫作张三的记录
MyBatisPlus的ActiveRecord实现CRUD
自己编写的实体类增加注解并继承Model类,即可通过构造实体类完成增删改查
@TableName("t_student")//关联数据库的那张表
public class Student extends Model{
@TableId("id")//关联数据库中的主键
private String ID;
@TableField("name")//关联数据库中的字段
private String name;
}
使用类代码:
public void testARInsert() {
Student student= new Student();
employee.setName("ARinsert测试");
employee.setId(23);
Boolean result = stuent.insert();//即可在数据库中插入此条数据
}
Mybatis中foreach标签和选择标签
@Select("<script>" +
"select * from (select ar.name as \"custName\",ar.mobile as \"mobile\",to_char(ar.date_appoint," +
"'yyyy.MM.dd') as\"appointDate\" from csp_appoint_register ar where not exists(select s.appoint_no\n" +
"from csp_el_city_channel_info s where s.appoint_no = ar.appoint_no) ar.um = #{map.salesManCode}\n" +
"<if test = \"map.mainLoanCode != null\">" +
"and ar.main_loan_code = #{mainLoanCode}" +
"</if>)" +
"and ar.agent_dept_no in\n" +
"<foreach collection=\"map.deptCode\" item=\"item\" index=\"index\" open =\"(\" close=\")\" separator=\",\">" +
"#{item}" +
"</foreach> ) tab2 where 1=1\n" +
"<choose>" +
"<when test=\"'desc'.toString() == map.orderByTime\">" +
"order by tab2.\"appointDate\" desc,tab2.\"appointNo\"" +
"<otherwise>" +
"order by tab2.\"appoimtDate\" asc,tab2.\"appointNo\"" +
"</otherwise>" +
"</choose>" +
"</script>")
List<Map<String,Object>> queryLifeMap(@Param("map") Map<String,Object> map);
prepend = “and” 标签
<isNotEmpty prepend = "and" property = "applyNo">
</isNotEmpty>
使用merge into(仅oracle支持)
<update id="mergeinfo">
merge into user_type a
using ( select #{name} as name, #{type} as type from dual ) b
on (a.type = b.type)
when not matched then
insert (type,name) values(#{type},#{name})
when matched then
update set name = #{name} where type = #{type}
</update>
解释:
如果不匹配:
执行 :“insert (type,name) values(#{type},#{name})”
如果匹配:
执行:“update set name = #{name} where type = #{type}”
文章参考