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

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

<!-- 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);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值