一、搭建SpringMVC框架
Eclipse先建一个Dynamic Web Project;新手可参考web project的创建步骤:新建web project步骤
环境备注:Eclipse neon,Tomcat8.0.41_x64,JDK1.8.0_91_X64;
spring为spring-4.3.4
1.首先引入SpringMVC必须的JAR
Spring需要引入的基本jar包如上图,
common-logging的jar
mybatis和mybatis与spring整合的jar
mysql对应的jar
druid为使用阿里的数据库连接池使用的jar
standard.jar和jstl.jar为jsp页面需要使用jstl标签
2.MyBatis数据基础访问类 BaseMapper.java
package com.chensan.common.mapper;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Mybatis 数据访问基类.<br>
* T 实体对象类型. <br>
* K 主键类型.
* @author chensan
*/
public class BaseMapper<T, K> {
/**
* 获取Mapper类的真实数据类型
*
* @return
*/
@SuppressWarnings("rawtypes")
private Class getEntityClass() {
Class clazz = (Class) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
return clazz;
}
/**
* 获取数据库操作的完全限定的名称.<br>
* 如:com.chensan.mybatis.sys.UserMedic.queryById
* @param method
* Mapper.xml配置文件中方法名称
* @return 返回完全限定方法名称
*/
protected String getQualifiedMethod(String method) {
String entityClassName = getEntityClass().getName();
String methodName = entityClassName.replace(".entity.", ".mapper.") + "Mapper." + method;
return methodName;
}
protected SqlSessionTemplate sqlSessionTemplate;
/**
* 设置SqlSessionTemplate. <br>
* 子类可以覆写。
* @param sqlSessionTemplate
* SqlSessionTemplate模拟
*/
@Autowired
protected void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
/**
* 新增实体对象.
* @param entity
* 要新增的实体对象
*/
public int insert(T entity) {
return sqlSessionTemplate.insert(getQualifiedMethod("insert"), entity);
}
public int batchInsert(List<T> list) {
return sqlSessionTemplate.insert(getQualifiedMethod("batchInsert"), list);
}
/**
* 根据主键删除实体对象.
* @param id
* 主键id
* @return 返回删除的行数。
*/
public int deleteById(K id) {
return sqlSessionTemplate.delete(getQualifiedMethod("deleteById"), id);
}
/**
* 根据id批量删除.
* @param ids
* 要删除实体对象的主键
* @return 返回删除的行数。
*/
public int deleteByIds(List<K> ids) {
return sqlSessionTemplate.delete(getQualifiedMethod("deleteByIds"), ids);
}
/**
* 根据id更新实体对象.
* @param entity
* 要更新的实体对象
* @return 返回更新的行数。
*/
public int updateById(T entity) {
return sqlSessionTemplate.update(getQualifiedMethod("updateById"), entity);
}
/**
* 根据id进行部分更新.<br>
* 根据id更新不为null的字段。
* @param entity
* 要更新的实体对象.<br>
* new一个新的实体对象,对需要更新的字段和主键赋值,然后传入进行更新。
*/
public int updateSelectiveById(T entity) {
return sqlSessionTemplate.update(getQualifiedMethod("updateSelectiveById"), entity);
}
/**
* 根据id查询.
* @param id
* 主键id
* @return 返回指定id的实体对象,如果不存在则返回null。
*
*/
public T queryById(K id) {
return sqlSessionTemplate.selectOne(getQualifiedMethod("queryById"), id);
}
/**
* 根据多个id查询.
* @param ids
* id列表
* @return 返回指定id的实体对象列表。
*/
public List<T> queryByIds(List<K> ids) {
return sqlSessionTemplate.selectList(getQualifiedMethod("queryByIds"), ids);
}
/**
* 查询数据库中记录总条数.
* @param where
* 查询条件。如id=1 and name like '%jhon'
* @return 返回满足查询条件的记录总条数。
*
*/
public Long queryCount(String where) {
return sqlSessionTemplate.selectOne(getQualifiedMethod("queryCount"), where);
}
/**
* 查询数据库表中所有记录.
* @return 返回表中所有记录。
*/
public List<T> queryAll() {
return sqlSessionTemplate.selectList(getQualifiedMethod("queryAll"));
}
}
3.实体类接口的泛型接口 IBaseService
package com.chensan.common.service;
import java.util.List;
public interface IBaseService<T, K> {
/**
* 新增实体对象.
* @param entity
* 要新增的实体对象
* @return 返回受影响行数,插入成功返回1。
*/
public int insert(T entity);
/**
* 批量插入实体对象.
* @param list
* 要插入的实体对象集合
* @return 返回受影响行数,成功则返回插入数量。
*/
public int batchInsert(List<T> list);
/**
* 部分更新.
* @param entity
* 要更新的实体对象.
*/
public int updateById(T entity);
/**
* 部分更新.
* @param entity
* 要更新的实体对象.<br>
* 一般使用方法:new一个新的实体对象,对需要更新的字段和主键赋值,然后传入进行更新。
*/
public int updateSelectiveById(T entity);
/**
* 根据id主键删除实体对象.
* @param id
* 主键id
* @return 返回删除的行数。
*/
public int deleteById(K id);
/**
* 根据id批量删除.
* @param ids
* 要删除实体对象的主键
* @return 返回删除的行数。
*/
public int deleteByIds(List<K> ids);
/**
* 根据主键id查询数据.
* @param id
* 主键id
* @return 返回该id对象的数据。
*/
public T queryById(K id);
/**
* 根据多个id查询.
* @param ids
* id列表
* @return 返回指定id的实体对象列表。
*/
public List<T> queryByIds(List<K> ids);
/**
* 查询数数据库中记录总条数.
* @param where
* 查询条件。如id=1 and name like '%jhon'
* @return 返回满足查询条件的记录总条数。
*/
public Long queryCount(String where);
/**
* 返回数据库中所有记录.
* @return 返回数据表中所有记录。
*/
public List<T> queryAll();
}
4.泛型接口实现类 IBaseServiceImpl.java
package com.chensan.common.service;
import java.util.List;
import com.chensan.common.mapper.BaseMapper;
/**
* Service层基类
* @author <a href="mailto:1619427973@qq.com">chenhf</a>
* @param <T>
*/
public abstract class BaseServiceImpl<T, K> implements IBaseService<T, K> {
public abstract BaseMapper<T, K> getMapper();
/**
* 新增实体对象.
* @param entity
* 要新增的实体对象
* @return 返回受影响行数,插入成功返回1。
*/
public int insert(T entity) {
return getMapper().insert(entity);
}
/**
* 批量插入实体对象.
* @param list
* 要插入的实体对象集合
* @return 返回受影响行数,成功则返回插入数量。
*/
public int batchInsert(List<T> list) {
return getMapper().batchInsert(list);
}
/**
* 部分更新.
* @param entity
* 要更新的实体对象.
* @return 返回更新的行数。
*/
public int updateById(T entity) {
return getMapper().updateById(entity);
}
/**
* 部分更新.
* @param entity
* 要更新的实体对象.<br>
* 一般使用方法:new一个新的实体对象,对需要更新的字段和主键赋值,然后传入进行更新。
* @return 返回更新的行数。
*/
public int updateSelectiveById(T entity) {
return getMapper().updateSelectiveById(entity);
}
/**
* 根据id主键删除实体对象.
* @param id
* 主键id
*/
public int deleteById(K id) {
return getMapper().deleteById(id);
}
/**
* 根据id批量删除.
* @param ids
* 要删除实体对象的主键
* @return 返回删除的行数。
*/
public int deleteByIds(List<K> ids) {
return getMapper().deleteByIds(ids);
}
/**
* 根据主键id查询数据.
* @param id
* 主键id
* @return 返回该id对象的数据。
*/
public T queryById(K id) {
return getMapper().queryById(id);
}
/**
* 根据多个id查询.
* @param ids
* id列表
* @return 返回指定id的实体对象列表。
*/
public List<T> queryByIds(List<K> ids) {
return getMapper().queryByIds(ids);
}
/**
* 查询数数据库中记录总条数.
* @param where
* 查询条件。如id=1 and name like '%jhon'
* @return 返回满足查询条件的记录总条数。
*/
public Long queryCount(String where) {
return getMapper().queryCount(where);
}
/**
* 获取数据库表中所有记录.
* @return 返回数据表中所有记录。
*/
public List<T> queryAll() {
return getMapper().queryAll();
}
}
5.实体类 User
package com.chensan.entity.sys;
import java.io.Serializable;
import java.util.Date;
/**
* User 实体类
* @author chensan
*/
public class User implements Serializable {
private static final long serialVersionUID =