1、spring中引入ibatis配置文件,上章已有相关配置,这里再补充重点的。
2、config/ibatis.xml的内容:
3、config/ibatis_dirCountry.xml的内容:
4、实体类TbDirCountryEntity.java:
5、Action类:
6、Service接口和实现类:
7、Dao类:
<!-- spring的ibatis 配制 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:config/ibatis.xml</value>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
2、config/ibatis.xml的内容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="config/ibatis_dirCountry.xml" />
</sqlMapConfig>
3、config/ibatis_dirCountry.xml的内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap>
<typeAlias type="com.bluedon.common.entity.TbDirCountryEntity" alias="dirCountry"/>
<resultMap class="dirCountry" id="dirCountry">
<result property="code" column="code" />
<result property="name" column="name" />
</resultMap>
<select id="getAllDirCountry" resultMap="dirCountry">
select * from tb_dir_country
</select>
<select id="getDirCountryByParameter" parameterClass="java.util.Map" resultMap="dirCountry">
select * from tb_dir_country
<dynamic prepend="where">
<isNotNull prepend="and" property="code">
code = #code#
</isNotNull>
<isNotNull prepend="and" property="name">
name like '%'||#name#||'%'
</isNotNull>
</dynamic>
</select>
</sqlMap>
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<TbDirCountryEntity> 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<String,Object> map=new HashMap<String,Object>();
map.put("code", "2");
map.put("name", "中");
List<TbDirCountryEntity> 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<TbDirCountryEntity> getDirCountryList();
public List<TbDirCountryEntity> getDirCountryList(Map<String,Object> map);
}
public class DirCountryServiceImpl implements DirCountryService{
@Resource
private DirCountryDao dirCountryDao;
@Override
public List<TbDirCountryEntity> getDirCountryList(){
return this.dirCountryDao.getDirCountryList();
}
@Override
public List<TbDirCountryEntity> getDirCountryList(Map<String, Object> map) {
// TODO Auto-generated method stub
return this.dirCountryDao.getDirCountryListByParameter(map);
}
}
7、Dao类:
@Repository("dirCountryDao")
public class DirCountryDao extends SqlMapClientDaoSupport {
public List<TbDirCountryEntity> getDirCountryList(){
return (List<TbDirCountryEntity>)this.getSqlMapClientTemplate().queryForList("getAllDirCountry");
}
public List<TbDirCountryEntity> getDirCountryListByParameter(Map<String, Object> map){
return this.getSqlMapClientTemplate().queryForList("getDirCountryByParameter", map);
}
}