1,basedao
public interface BaseDao<T> {
public void save(T entity);// 儲存
public void delete(T entity);// 删除使用者
public void update(T entity);// 更新使用者
public T findById(Serializable id);// 根據主鍵查找使用者
public List<T> getAll();// 檢視所有
}
2,baseDaoImpl
public abstract class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T> {
@Override
@Autowired
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
Class<T> clazz;
ParameterizedType pt;
String clsname;
public BaseDaoImpl() {
pt = (ParameterizedType)this.getClass().getGenericSuperclass(); //cn.tax.nsfw..coredao.impl.BaseDaoImpl<cn.tax.nsfw.user.entity.User>
clazz = (Class<T>)pt.getActualTypeArguments()[0]; //cn.tax.nsfw.user.entity.User
clsname=clazz.getSimpleName();
System.out.println(clsname+"構造");
}
@Override
public void save(T entity) {
System.out.println(clsname+"方法");
getSqlSession().insert("com.dao."+clsname+"Mapper.save",entity);
}
@Override
public void delete(T entity) {
}
@Override
public void update(T entity) {
}
@Override
public T findById(Serializable id) {
return null;
}
@Override
public List<T> getAll() {
return null;
}
}
3,Usermapper’
public interface UserMapper extends BaseDao<User> {
//public int save( String username, String pwd);
public User fintByUsername(String username);
}
4,UserDaoImpl
@Repository("userMapper")
public class UserDaoImpl extends BaseDaoImpl<User> implements UserMapper {
public User fintByUsername(String username){
return getSqlSession().selectOne("com.dao.UserMapper.findByUsername",username);
}
}
5,application.properties
mybatis.mapper-locations=classpath:config/*.xml