基于上一篇的封装,实现一个UserDAO
接口:IUserDAO,也是在mybatis配置中对应的一个mapper接口
具体实现,继承了AbstractBatisDAO,供业务逻辑层调用。
最后是一个调用的例子:
SessionMgr,DAO的实现类都是线程安全,可以通过spring IOC将其单例化管理。
接口:IUserDAO,也是在mybatis配置中对应的一个mapper接口
package example.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import example.model.User;
public interface IUserDAO {
public User findById(int id);
public User findByName(String name);
public void addUser(User user);
public List<User> findUsers(@Param("start")int start, @Param("size")int size);
public Map<Integer, User> findUserByIds(List<Integer> list);
}
具体实现,继承了AbstractBatisDAO,供业务逻辑层调用。
package example.dao.impl;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import example.dao.AbstractBatisDAO;
import example.dao.IUserDAO;
import example.model.User;
public class UserDAO extends AbstractBatisDAO implements IUserDAO {
@Override
public User findById(final int id) {
User user = (User) super.process(new AbstractBatisDAO.SqlCaller() {
public Object doWith(SqlSession session) {
IUserDAO mapper = session.getMapper(IUserDAO.class);
return mapper.findById(id);
}
});
return user;
}
@Override
public User findByName(final String name) {
User user = (User) super.process(new AbstractBatisDAO.SqlCaller() {
public Object doWith(SqlSession session) {
IUserDAO mapper = session.getMapper(IUserDAO.class);
return mapper.findByName(name);
}
});
return user;
}
@Override
public void addUser(final User user) {
// TODO Auto-generated method stub
super.process(new AbstractBatisDAO.SqlCaller() {
public Object doWith(SqlSession session) {
IUserDAO mapper = session.getMapper(IUserDAO.class);
mapper.addUser(user);
return null;
}
});
}
@Override
public List<User> findUsers(@Param("start")final int start,
@Param("size")final int size) {
List<User> users = (List<User>)super.process(new AbstractBatisDAO.SqlCaller() {
public Object doWith(SqlSession session) {
IUserDAO mapper = session.getMapper(IUserDAO.class);
return mapper.findUsers(start, size);
}
});
return users;
}
@Override
public Map<Integer, User> findUserByIds(final List<Integer> list) {
Map<Integer, User> users = (Map<Integer, User>)super.process(new AbstractBatisDAO.SqlCaller() {
public Object doWith(SqlSession session) {
IUserDAO mapper = session.getMapper(IUserDAO.class);
return mapper.findUserByIds(list);
}
});
return users;
}
}
最后是一个调用的例子:
package example;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import example.dao.impl.UserDAO;
import example.model.User;
public class SimpleBatis {
public static void main(String[] args) throws Exception {
MybatisSessionMgr sessionMgr = new MybatisSessionMgr();
sessionMgr.init();
UserDAO userDao = new UserDAO();
userDao.setSessionMgr(sessionMgr);
User user = null;
user = new User();
user.setName("terry");
user.setSex((byte)1);
user.setAge(30);
userDao.addUser(user);
System.out.println("sleeping 2 seconds...");
user = userDao.findByName("terry");
if(user != null) {
System.out.println("find user:"+user.getName()+", id="+user.getId());
} else {
System.out.println("user not found");
}
List<Integer> ids = new ArrayList<Integer>(2);
ids.add(1);
ids.add(2);
Map<Integer, User> map = userDao.findUserByIds(ids);
if(map != null) {
for(Integer i : map.keySet()) {
System.out.println("key from map:"+i);
User u = map.get(i);
System.out.println(u.getId()+"-"+u.getName()+", sex:"+u.getSex()+", age:"+u.getAge());
}
}
}
}
SessionMgr,DAO的实现类都是线程安全,可以通过spring IOC将其单例化管理。