Spring MVC 注释 + iBATIS 整合(4)

sql-map-config.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
 
 <sqlMapConfig>
  <settings useStatementNamespaces="true"/>
   <sqlMap resource="ibates.xml"/>
 </sqlMapConfig>

Ibatis.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 namespace="ibates11">
    <select resultClass="String">
 select top 1 PINGNAME from PingDao
    </select>
    <insert parameterClass="map" >
        insert into PingDao values (#name#)
    </insert>
</sqlMap>

首先需要定义个dao基类,用来配置dao层的数据库连接。代码如下:

package com.zy.ywyd.gameoperators.dao;

//import Java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.ibatis.sqlmap.client.SqlMapClient;

@SuppressWarnings("deprecation")
public class Dao  extends  SqlMapClientDaoSupport {
    @Resource(name = "sqlMapClient")
    public SqlMapClient sqlMapClient;

@PostConstruct
    public void initSqlMapClient() {
        super.setSqlMapClient(sqlMapClient);
    }
/*
 获取查询的对象
 public Object selectObject(Map<String,Object> parameter){
  return null;
 };
 获取查询的结果集
 public Object selectList(Map<String,Object> parameter){
  return null;
 }
 插入数据
 public Object insert(Map<String,Object> parameter){
  return null;
 }
 更新数据
 public Object update(Map<String,Object> parameter){
  return null;
 }
 删除数据
 public Object delete(Map<String,Object> parameter){
  return null;
 }*/
}

然后定义分类 TestDao 代码如下:

package com.zy.ywyd.gameoperators.dao;

import org.springframework.stereotype.Component;

@Component
public class TestDao extends Dao {


 @SuppressWarnings("deprecation")
 public void getMsg(){
     System.out.println("123aaaaaaaaaaaaaa");
     System.out.println("TestDao-ibatis.test");
     String msg = (String) this.getSqlMapClientTemplate().queryForObject("TestDao-ibatis.test");
     System.out.println(msg);
    }
}

定义对应的xml 文件

TestDao-ibatis.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 namespace="TestDao-ibatis">
 <select resultClass="String">
    SELECT test FROM test limit 1,1
 </select>
</sqlMap>

最后就是中间的service 进行action和dao 的衔接。

package com.zy.ywyd.gameoperators.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zy.ywyd.gameoperators.dao.TestDao;

@Service
public class TestService {
 @Autowired
 private TestDao testD;
 
 @Autowired
 public String test(){
  System.out.println("test service");
  System.out.println("testD = " +testD);
  testD.getMsg();
  return "123456";
 }
}

以上是我在项目总,自己总结的。有不对的地方,还请指教,希望能给大家带来帮助。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/a79026478b068a621e9bbd28466022e4.html