码迷,mamicode.com
首页 > 数据库 > 详细

MyBatis注解之动态SQL

时间:2020-10-26 11:29:09      阅读:38      评论:0      收藏:0      [点我收藏+]

标签:add   get   system   sele   cep   VID   rod   ssi   mybatis   

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

MyBatis注解之动态SQL

标签:add   get   system   sele   cep   VID   rod   ssi   mybatis   

原文地址:https://www.cnblogs.com/nyfblog/p/13874545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!