ibatis的引入——maven搭建spring mvc+ibatis项目(三)

1、spring中引入ibatis配置文件,上章已有相关配置,这里再补充重点的。




classpath:config/ibatis.xml





2、config/ibatis.xml的内容:


PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">





3、config/ibatis_dirCountry.xml的内容:


PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">













4、实体类TbDirCountryEntity.java:

@SuppressWarnings("serial")
public class TbDirCountryEntity implements java.io.Serializable {
/**code*/
private java.lang.String code;
/**name*/
private java.lang.String name;

/**
*方法: 取得java.lang.String
*@return: java.lang.String code
*/
public java.lang.String getCode(){
return this.code;
}

/**
*方法: 设置java.lang.String
*@param: java.lang.String code
*/
public void setCode(java.lang.String code){
this.code = code;
}

/**
*方法: 取得java.lang.String
*@return: java.lang.String name
*/
public java.lang.String getName(){
return this.name;
}

/**
*方法: 设置java.lang.String
*@param: java.lang.String name
*/
public void setName(java.lang.String name){
this.name = name;
}
}


5、Action类:

@Controller
@RequestMapping("dirCountryAction")
public class DirCountryAction{
@Resource
private DirCountryService dirCountryService;

@ResponseBody
@RequestMapping(params="getAllList")
public String getTbDirCountryList(HttpServletRequest req,HttpServletResponse resp){
try {
List list=this.dirCountryService.getDirCountryList();
return JSONUtil.serialize(list);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

@ResponseBody
@RequestMapping(params="getListByParameter")
public String getListByParameter(HttpServletRequest req,HttpServletResponse resp){
try {
Map map=new HashMap();
map.put("code", "2");
map.put("name", "中");
List list=this.dirCountryService.getDirCountryList(map);
return JSONUtil.serialize(list);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}


6、Service接口和实现类:

public interface DirCountryService {
public List getDirCountryList();

public List getDirCountryList(Map map);
}


public class DirCountryServiceImpl implements DirCountryService{

@Resource
private DirCountryDao dirCountryDao;

@Override
public List getDirCountryList(){
return this.dirCountryDao.getDirCountryList();
}

@Override
public List getDirCountryList(Map map) {
// TODO Auto-generated method stub
return this.dirCountryDao.getDirCountryListByParameter(map);
}
}


7、Dao类:

@Repository("dirCountryDao")
public class DirCountryDao extends SqlMapClientDaoSupport {
public List getDirCountryList(){
return (List)this.getSqlMapClientTemplate().queryForList("getAllDirCountry");
}

public List getDirCountryListByParameter(Map map){
return this.getSqlMapClientTemplate().queryForList("getDirCountryByParameter", map);
}
}

你可能感兴趣的:(maven搭建spring,mvc+ibatis)