调用mybatis generator已经生成好的dao来查询例子

时间:2021-12-28 07:56:35
package com.cib.xj.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import com.cib.xj.dao.VAJMapper;
import com.cib.xj.pojo.VAJ;
import com.cib.xj.pojo.VAJExample;
import com.cib.xj.pojo.VAJExample.Criteria; @Controller
public class AJController {
@Resource
private VAJMapper ajMapper; @RequestMapping( "/showAJ" )
public String toIndex( String id, Model model )
{
VAJExample example=new VAJExample();
Criteria criteria = example.createCriteria();
criteria.andCardNoEqualTo(id);
criteria.andCardNoIsNotNull(); List<VAJ> result=ajMapper.selectByExample(example);
model.addAttribute("result" ,result);
return "AJ";
}
}

先定义对应的dao类,用@Resource注释,mybatis配置好的话dao类就可以直接用了.

   @Resource
private VAJMapper ajMapper;

用selectByExample查询数据库的例子

    VAJExample example=new VAJExample();
Criteria criteria = example.createCriteria();
criteria.andCardNoEqualTo(id);
criteria.andCardNoIsNotNull(); List<VAJ> result=ajMapper.selectByExample(example);