1. 程式人生 > >mybatis逆向工程的Example類用法==筆記==【單表操作只需呼叫,多表查詢需要自定義sql+mapper介面方法(待補全)】

mybatis逆向工程的Example類用法==筆記==【單表操作只需呼叫,多表查詢需要自定義sql+mapper介面方法(待補全)】

======上程式碼:

===版本1:

@Service
public class BaseDictServiceImpl implements BaseDictService {
	
	//查詢資料字典表,注入資料字典表mapper介面代理物件
	@Autowired
	private BaseDictMapper baseDictMapper;
	
	/**
	 * 需求:根據類別碼查詢類別資訊
	 * 引數:String typeCode
	 * 返回值:List<BaseDict>
	 * ==========每個實體類【生成的Mapper檔案 ,所有的單表操作,都已經定義好了SQL,只需要新增新增條件即可】
	 * ====對於多表查詢,只有自己 建立 example物件,封裝查詢條件。
	 */
	public List<BaseDict> findBaseDictWithTypeCode(String typeCode) {
		 //===example用來封裝查詢條件
		BaseDictExample example = new BaseDictExample();
		Criteria criteria = example.createCriteria();
		 //=======所有的單表查詢,example裡都有,根據方法名呼叫即可。
		criteria.andDictTypeCodeNotEqualTo(typeCode);
		//===######【以上三行程式碼,實現封裝 單表查詢引數】
		
		
		//使用example查詢物件,執行查詢
		//===mapper的方法也是 生成的,===單表查詢,真的不需要自定義任何方法。
		List<BaseDict> list = baseDictMapper.selectByExample(example);		
			
		return list;
	}

}

===版本2:

@Controller
public class CustomerController {
	
	@Autowired
	BaseDictService baseDictService;
	
	/*
	 	//=============typeCode 配置到配置檔案裡。==【配置檔案讀取+註解在實體類顯示】
	<!-- ====讀取配置檔案裡的 typeCode  -->
	<context:property-placeholder location="classpath:resources.properties"/>
	 
	 * */
	@Value("${CUST_SOURCE_CODE}")
//	final String CUST_SOURCE_CODE;
	 String CUST_SOURCE_CODE;
	
	@Value("${CUST_INDUSTRY_CODE}")
	String CUST_INDUSTRY_CODE;
	
	@Value("${CUST_LEVEL_CODE}")
	String CUST_LEVEL_CODE;//ok
	
	
	/**
	 * 需求:載入客戶列表頁面
	 * 業務需求: 載入客戶列表頁面之前,需要初始化 客戶來源下拉列表,客戶行業下拉列表,客戶級別下拉列表
	 */
	@RequestMapping("list")
	public String showCustomList(Model model){
		//===查詢======####====熟悉BaseDict表結構含義
//		//客戶行業下拉列表,
//		List<BaseDict> industryList = baseDictService.findBaseDictWithTypeCode("001");
//		//初始化 客戶來源下拉列表,
//		List<BaseDict> sourceList = baseDictService.findBaseDictWithTypeCode("002");
//		//客戶級別下拉列表
//		List<BaseDict> levelList = baseDictService.findBaseDictWithTypeCode("006");
		
		//=============typeCode 配置到配置檔案裡。==【配置檔案讀取+註解在實體類顯示】
		//初始化 客戶來源下拉列表,
		List<BaseDict> sourceList = baseDictService.findBaseDictWithTypeCode(CUST_SOURCE_CODE);
		//客戶行業下拉列表,
		List<BaseDict> industryList = baseDictService.findBaseDictWithTypeCode(CUST_INDUSTRY_CODE);
		//客戶級別下拉列表
		List<BaseDict> levelList = baseDictService.findBaseDictWithTypeCode(CUST_LEVEL_CODE);
		
		//=========傳到頁面
		model.addAttribute("fromType", sourceList);
		model.addAttribute("industryType", industryList);
		model.addAttribute("levelType", levelList);
		
		return "list";//邏輯檢視:list.jsp
	}

}