标签:add get system sele cep VID rod ssi mybatis
新增CategoryDynaSqlProvider,提供CRUD对应的SQL语句。
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();
}
}
追加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();
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();
}
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
标签:add get system sele cep VID rod ssi mybatis
原文地址:https://www.cnblogs.com/nyfblog/p/13874545.html