MyBatis注解之动态SQL
准备SqlProvider
新增CategoryDynaSqlProvider,提供CRUD对应的SQL语句。
点击查看完整CategoryDynaSqlProvider
package com.nyf.providers;
import org.apache.ibatis.jdbc.SQL;
public class CategoryDynaSqlProvider {
public String list() {
return new SQL()
.SELECT("*")
.FROM("category_")
.toString();
}
public String get() {
return new SQL()
.SELECT("*")
.FROM("category_")
.WHERE("id=#{id}")
.toString();
}
public String add(){
return new SQL()
.INSERT_INTO("category_")
.VALUES("name", "#{name}")
.toString();
}
public String update(){
return new SQL()
.UPDATE("category_")
.SET("name=#{name}")
.WHERE("id=#{id}")
.toString();
}
public String delete(){
return new SQL()
.DELETE_FROM("category_")
.WHERE("id=#{id}")
.toString();
}
}
修改CategoryMapper
追加add_Dynamic,delete_Dynamic,update_Dynamic,get_Dynamic四个方法:
@InsertProvider(type=CategoryDynaSqlProvider.class,method="add")
public int add_Dynamic(Category category);
@DeleteProvider(type=CategoryDynaSqlProvider.class,method="delete")
public void delete_Dynamic(int id);
@SelectProvider(type=CategoryDynaSqlProvider.class,method="get")
public Category get_Dynamic(int id);
@UpdateProvider(type=CategoryDynaSqlProvider.class,method="update")
public int update_Dynamic(Category category);
@SelectProvider(type=CategoryDynaSqlProvider.class,method="list")
public List<Category> list_Dynamic();
点击查看完整CategoryMapper
package com.nyf.mappers;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.annotations.Many;
import com.nyf.pojo.Category;
import com.nyf.providers.CategoryDynaSqlProvider;
public interface CategoryMapper {
@Insert(" insert into category_ ( name ) values (#{name}) ")
public int add(Category category);
@Delete(" delete from category_ where id= #{id} ")
public void delete(int id);
@Select("select * from category_ where id= #{id} ")
public Category get(int id);
@Update("update category_ set name=#{name} where id=#{id} ")
public int update(Category category);
@Select(" select * from category_ ")
public List list();
@Select(" select * from category_ ")
@Results({
@Result(property = "id", column = "id"),
@Result(property = "products", javaType = List.class, column = "id", many = @Many(select = "com.nyf.mappers.ProductMapper.listByCategory") )
})
public List listOneToMany();
@Select(" select * from category_ where id = #{id}")
public Category get2(int id);
@InsertProvider(type=CategoryDynaSqlProvider.class,method="add")
public int add_Dynamic(Category category);
@DeleteProvider(type=CategoryDynaSqlProvider.class,method="delete")
public void delete_Dynamic(int id);
@SelectProvider(type=CategoryDynaSqlProvider.class,method="get")
public Category get_Dynamic(int id);
@UpdateProvider(type=CategoryDynaSqlProvider.class,method="update")
public int update_Dynamic(Category category);
@SelectProvider(type=CategoryDynaSqlProvider.class,method="list")
public List list_Dynamic();
}
编写测试类DynamicSQL
点击查看完整DynamicSQL
package com.nyf.tests;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.nyf.mappers.CategoryMapper;
import com.nyf.pojo.Category;
public class DynamicSQL {
public static void main(String[] args) throws IOException {
String resource = "com/nyf/config/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
CategoryMapper mapper = session.getMapper(CategoryMapper.class);
add(mapper);
delete(mapper);
get(mapper);
update(mapper);
session.commit();
session.close();
}
private static void add(CategoryMapper mapper) {
System.out.println("执行add方法:");
Category c = new Category();
c.setName("新增加的Category");
mapper.add(c);
listAll(mapper);
}
private static void delete(CategoryMapper mapper) {
System.out.println("执行delete方法:");
mapper.delete(2);
listAll(mapper);
}
private static void update(CategoryMapper mapper) {
System.out.println("执行update方法:");
Category c = mapper.get(1);
c.setName("修改了的Category名称");
mapper.update(c);
listAll(mapper);
}
private static void get(CategoryMapper mapper) {
System.out.println("执行get方法:");
Category c = mapper.get(1);
System.out.println(c.getName());
}
private static void listAll(CategoryMapper mapper) {
List cs = mapper.list();
for (Category c : cs) {
System.out.println(c.getName());
}
System.out.println();
}
}
结果验证
- 正确结果应该如下所示:
执行add方法:
category1
新增加的Category
category2
执行delete方法:
category1
新增加的Category
执行get方法:
category1
执行update方法:
修改了的Category名称
新增加的Category