使用spring注解的方式,使用 mybites 中的 SqlSessionDaoSupport
只是测试的例子,表中只有2个字段
1 。继承SqlSessionDaoSupport 的顶级dao 层代码
package com.inf.base;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import com.inf.system.utiles.PropertyUtils;
import com.inf.system.utiles.page.PageBean;
/**
* 顶层接口
* @author zongxin
*/
public class BaseDao extends SqlSessionDaoSupport{
/*
// 使用 SqlSessionTemplate
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Autowired
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate)
{
super.setSqlSessionTemplate(sqlSessionTemplate);
}*/
//使用sqlSessionFactory
@Autowired
private SqlSessionFactory sqlSessionFactory;
@Autowired
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory)
{
super.setSqlSessionFactory(sqlSessionFactory);
}
/**
* 执行insert操作
* @param statement
* @return
*/
public int insert(String statement) {
return getSqlSession().insert(statement);
}
/**
* 执行insert操作
* @param statement
* @param parameter
* @return
*/
public int insert(String statement, Object parameter) {
return getSqlSession().insert(statement, parameter);
}
public int update(String statement) {
return getSqlSession().update(statement);
}
public int update(String statement, Object parameter) {
return getSqlSession().update(statement, parameter);
}
public int delete(String statement) {
return getSqlSession().delete(statement);
}
public int delete(String statement, Object parameter) {
return getSqlSession().delete(statement, parameter);
}
/**
* 获取一个list集合
* @param statement
* @return
*/
public List> selectList(String statement) {
return getSqlSession().selectList(statement);
}
/**
* 根据参数 获取一个list集合
* @param statement
* @param parameter
* @return
*/
public List> selectList(String statement, Object parameter) {
return getSqlSession().selectList(statement, parameter);
}
public Map, ?> selectMap(String statement, String mapKey) {
return getSqlSession().selectMap(statement, mapKey);
}
public Map, ?> selectMap(String statement, Object parameter, String mapKey) {
return getSqlSession().selectMap(statement, parameter, mapKey);
}
/**
* 获取Object对象
* @param statement
* @return
*/
public Object selectOne(String statement) {
return getSqlSession().selectOne(statement);
}
/**
* 获取connection, 以便执行较为复杂的用法
* @return
*/
public Connection getConnection() {
return getSqlSession().getConnection();
}
}
2.实体类 代码:
package com.inf.sys.user.model;
public class TestGener {
private Long generId;
private String generValue;
public Long getGenerId() {
return generId;
}
public void setGenerId(Long generId) {
this.generId = generId;
}
public String getGenerValue() {
return generValue;
}
public void setGenerValue(String generValue) {
this.generValue = generValue;
}
}
3,dao层接口代码:
package com.inf.sys.user.repository;
public interface TestDao {
public Long getTestIds(String generName);
}
4,dao的实现类代码
package com.inf.sys.user.repository.impl;
import org.springframework.stereotype.Repository;
import com.inf.base.BaseDao;
import com.inf.sys.user.repository.TestDao;
@Repository
public class TestDaoImpl extends BaseDao implements TestDao{
public Long getTestIds(String generName){
Object object= this.selectOne("com.inf.sys.user.mapper.TestMapper.getTestIds",generName);
Long reault=Long.valueOf(String.valueOf(object));
return reault;
}
}
com.inf.sys.user.mapper.TestMapper 为对应的mapper接口文件, getTestIds 为对应的参数
package com.inf.sys.user.mapper;
public interface TestMapper {
Long getTestIds(String generName);
}
6.mapper 对应的xml映射文件
package com.inf.sys.user.action;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.inf.sys.user.repository.TestDao;
import com.inf.system.utiles.JsonBean;
//test
@Controller
@RequestMapping("test/")
public class TestCollection {
@Autowired
private TestDao testDao;
@RequestMapping("testGetgen.do")
public void testGetgen(@RequestParam(required=true) String generName,
HttpServletRequest request, HttpServletResponse response) throws Exception{
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
Map result=new HashMap();
Long resulte=testDao.getTestIds(generName);
result.put("success",true);
result.put("msg",resulte);
System.out.println( "this is result======"+result);
PrintWriter out = response.getWriter();
out.print(JsonBean.toJson(result));
}
}
在dao的实现类上增加注解 @Repository
在 顶层dao类 baseDa 中
@Autowired
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory)
{
super.setSqlSessionFactory(sqlSessionFactory);
}
注入sqlSessionFactory 或者是 sqlSessionTemplate
通常使用mybites 直接使用mapper的方式 ,不使用dao层接口和dao的实现类,
applicationContext.xml 中的配置 :