前面已经实现了SSM的整合, 但是在实际开发中,很多模块都会有增删改查操作.将这些共用操作抽取个公用的出来,能少很多事.
1.实现baseDao
2.实现baseService
3.具体模块实现
4.测试
1.实现baseDao
mybatis中,实现sql都是用namespace+上sql的id, 而namespace一般都是用model的路径+名称.
所以baseDao的重点就是找到需要调用的sql的id.也就是找到调用的model.
这里就要用到泛型和发射来获得.
1) 先定义baseDao,定义2个方法.
package com.dingcheng.common.dao;
import java.io.Serializable;
public interface BaseDao {
public T get(Serializable id);
public int insert(T model);
}
package com.dingcheng.common.dao.impl;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.dingcheng.common.dao.BaseDao;
import com.dingcheng.common.exception.MySqlException;
import com.dingcheng.common.util.GenericsUtils;
public class BaseDaoImpl implements BaseDao {
private Logger logger = Logger.getLogger(BaseDaoImpl.class);
//从spring注入原有的sqlSessionTemplate
@Autowired
private SqlSessionTemplate sqlSession;
protected Class entityClass;
protected String sqlMapNamespace;
public static final String POSTFIX_SELECT = ".select";
public static final String POSTFIX_INSERT = ".insert";
public String getSqlMapNamespace() {
return sqlMapNamespace;
}
protected SqlSessionTemplate getSqlSession(){
return sqlSession;
}
/**
* 在构造函数中将泛型T.class赋给entityClass.
* sqlNamespace 带包名
*/
@SuppressWarnings("unchecked")
public BaseDaoImpl() {
entityClass = GenericsUtils.getSuperClassGenricType(getClass());
sqlMapNamespace = entityClass.getName();
}
@Override
public T get(Serializable id) {
if(id==null) return null;
return sqlSession.selectOne(sqlMapNamespace+POSTFIX_SELECT,id);
}
@Override
public int insert(T o) {
return sqlSession.insert(sqlMapNamespace+POSTFIX_INSERT, o);
}
}
public static Class getSuperClassGenricType(Class clazz) {
Type genType = clazz.getGenericSuperclass();
if (!(genType instanceof ParameterizedType)) {
return Object.class;
}
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
if (!(params[0] instanceof Class)) {
return Object.class;
}
return (Class) params[0];
}
2.实现baseService
baseService很简单,只用调用对应的dao层的方法即可. 所以只需要有对应的dao对象即可. 但是用注解的话,没法直接注入. 所以只有把getBaseDao放到具体的service中实现.
package com.dingcheng.common.service;
import com.dingcheng.common.dao.BaseDao;
public abstract class BaseService {
public BaseService(){}
/**
* 由业务类实现
* @return
*/
public abstract BaseDao getBaseDao();
/**
* 根据id,查询单个
* @param id
* @return
*/
public T get(Integer id){
return getBaseDao().get(id);
}
/**
* 插入单个对象
* @param model
* @return
*/
public int create(T model){
return getBaseDao().insert(model);
}
}
上面公用写完,现在写具体实现.
package com.dingcheng.sys.dao;
import com.dingcheng.common.dao.BaseDao;
import com.dingcheng.sys.model.User;
public interface UserDao extends BaseDao{
}
package com.dingcheng.sys.dao.impl;
import org.springframework.stereotype.Repository;
import com.dingcheng.common.dao.impl.BaseDaoImpl;
import com.dingcheng.sys.dao.UserDao;
import com.dingcheng.sys.model.User;
@Repository("userDaoImpl")
public class UserDaoImpl extends BaseDaoImpl implements UserDao {
}
package com.dingcheng.sys.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dingcheng.common.dao.BaseDao;
import com.dingcheng.common.service.BaseService;
import com.dingcheng.sys.dao.UserDao;
import com.dingcheng.sys.model.User;
@Service("userService")
public class UserService extends BaseService {
@Autowired
private UserDao userDaoImpl;
@Override
public BaseDao getBaseDao() {
return userDaoImpl;
}
}
4.测试
package com.dingcheng.sys.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.dingcheng.sys.model.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application-context.xml"})
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testSave(){
User user = new User();
user.setLoginName("loginName");
user.setPassword("password");
userService.create(user);
}
}
https://code.csdn.net/qq315737546/ssmq/tree/master
里面有更多的公用方法以及quartz的配置.