<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.3version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatisplus-spring-boot-starterartifactId>
<version>1.0.5version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plusartifactId>
<version>2.1.8version>
dependency>
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocity-engine-coreartifactId>
<version>2.0version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
# 配置mybatis-plus
mybatis-plus:
# 配置扫描xml
mapper-locations:
- classpath:mapper/*.xml
# 实体扫描,多个package用逗号或者分号分隔
type-aliases-package: com.ahut.entity
global-config:
# 逻辑删除配置
logic-delete-value: 0
logic-not-delete-value: 1
sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector
配置druid过程不变
代码生成
package com.ahut.code;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
/**
*
* @ClassName: CodeGeneration
* @Description: 代码生成器
* @author cheng
* @date 2018年1月25日 下午2:55:14
*/
public class CodeGeneration {
/**
*
* @Title: main
* @Description: 生成
* @param args
*/
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir("E://code");
gc.setFileOverride(true);
gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
gc.setAuthor("cheng");// 作者
// 自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setControllerName("%sAction");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("chengrui");
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/ahutplat");
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setTablePrefix(new String[] { "sys_" });// 此处可以修改为您的表前缀
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
strategy.setInclude(new String[] { "sys_user" }); // 需要生成的表
strategy.setSuperServiceClass(null);
strategy.setSuperServiceImplClass(null);
strategy.setSuperMapperClass(null);
mpg.setStrategy(strategy);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.ahut");
pc.setController("action");
pc.setService("service");
pc.setServiceImpl("serviceImpl");
pc.setMapper("mapper");
pc.setEntity("entity");
pc.setXml("xml");
mpg.setPackageInfo(pc);
// 执行生成
mpg.execute();
}
}
改造得到生成的实体如下
package com.ahut.entity;
import java.io.Serializable;
import java.util.Date;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
/**
*
* @ClassName: User
* @Description: 用户实体类
* @author cheng
* @date 2018年1月25日 下午3:36:06
*/
@TableName("sys_user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@TableId("ID")
private String id;
@TableField("USER_NAME")
private String userName;
@TableField("USER_ACCOUNT")
private String userAccount;
@TableField("PASSWORD")
private String password;
@TableField("SORT_ID")
private Integer sortId;
@TableField("STATUS")
@TableLogic
private String status;
@TableField("CREATE_USER")
private String createUser;
@TableField("CREATE_TIME")
private Date createTime;
@TableField("UPDATE_USER")
private String updateUser;
@TableField("UPDATE_TIME")
private Date updateTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAccount() {
return userAccount;
}
public void setUserAccount(String userAccount) {
this.userAccount = userAccount;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getSortId() {
return sortId;
}
public void setSortId(Integer sortId) {
this.sortId = sortId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getCreateUser() {
return createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getUpdateUser() {
return updateUser;
}
public void setUpdateUser(String updateUser) {
this.updateUser = updateUser;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "User{" + ", id=" + id + ", userName=" + userName + ", userAccount=" + userAccount + ", password="
+ password + ", sortId=" + sortId + ", status=" + status + ", createUser=" + createUser
+ ", createTime=" + createTime + ", updateUser=" + updateUser + ", updateTime=" + updateTime + "}";
}
}
改造得到生成的mapper接口如下
package com.ahut.mapper;
import java.util.Map;
import com.ahut.entity.User;
import com.baomidou.mybatisplus.mapper.BaseMapper;
/**
*
* @ClassName: UserMapper
* @Description: 用户数据访问接口
* @author cheng
* @date 2018年1月25日 下午3:46:22
*/
public interface UserMapper extends BaseMapper {
/**
*
* @Title: selectUserByMap
* @Description: 多条件组合查找用户
* @param userId
* @return
* @throws Exception
*/
User selectUserByMap(Map parameterMap) throws Exception;
}
改造得到生成的xml如下
<mapper namespace="com.ahut.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.ahut.entity.User">
<id column="ID" property="id" />
<result column="USER_NAME" property="userName" />
<result column="USER_ACCOUNT" property="userAccount" />
<result column="PASSWORD" property="password" />
<result column="SORT_ID" property="sortId" />
<result column="STATUS" property="status" />
<result column="CREATE_USER" property="createUser" />
<result column="CREATE_TIME" property="createTime" />
<result column="UPDATE_USER" property="updateUser" />
<result column="UPDATE_TIME" property="updateTime" />
resultMap>
<select id="selectUserByMap" parameterType="map" resultType="com.ahut.entity.User">
SELECT
ID,
USER_NAME,
USER_ACCOUNT,
PASSWORD,
SORT_ID,
STATUS,
CREATE_USER,
CREATE_TIME,
UPDATE_USER,
UPDATE_TIME
FROM
SYS_USER
WHERE STATUS = '1'
<if test="USER_ID != null and USER_ID != ''">
AND ID = #{USER_ID}
if>
<if test="USER_ACCOUNT != null and USER_ACCOUNT != ''">
AND USER_ACCOUNT = #{USER_ACCOUNT}
if>
select>
mapper>
service层
package com.ahut.mapper;
import java.util.Map;
import com.ahut.entity.User;
import com.baomidou.mybatisplus.mapper.BaseMapper;
/**
*
* @ClassName: UserMapper
* @Description: 用户数据访问接口
* @author cheng
* @date 2018年1月25日 下午3:46:22
*/
public interface UserMapper extends BaseMapper {
/**
*
* @Title: selectUserByMap
* @Description: 多条件组合查找用户
* @param userId
* @return
* @throws Exception
*/
User selectUserByMap(Map parameterMap) throws Exception;
}
serviceImpl层
package com.ahut.serviceImpl;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ahut.entity.ReturnBean;
import com.ahut.entity.User;
import com.ahut.mapper.UserMapper;
import com.ahut.service.UserService;
/**
*
* @ClassName: UserServiceImpl
* @Description: 业务逻辑实现
* @author cheng
* @date 2018年1月25日 下午3:58:28
*/
@Service
public class UserServiceImpl implements UserService {
/**
* 日志管理
*/
private static Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
/**
* 用户数据访问接口
*/
@Autowired
private UserMapper userMapper;
/**
*
* @Title: addUser
* @Description: 新增用户
* @param user
* @return
* @throws Exception
*/
public ReturnBean addUser(User user) throws Exception {
log.info("====>>执行addUser方法");
ReturnBean returnBean = new ReturnBean();
// 判断参数
if (user == null) {
returnBean.setFailReturn("传入的参数不合法");
return returnBean;
}
// 添加的结果
int resultNum = 0;
try {
// 这里的insert是BaseMapper中的方法
resultNum = userMapper.insert(user);
} catch (Exception e) {
log.error("依据id查找用户时发生异常", e);
returnBean.setFailReturn("程序内部发生错误");
return returnBean;
}
log.info("添加用户数:" + resultNum);
if (resultNum == 0) {
// 添加失败
returnBean.setFailReturn("添加用户失败");
} else {
returnBean.setSuccessReturn("添加用户成功");
}
return returnBean;
}
/**
*
* @Title: selectUserById
* @Description: 依据id查找用户
* @param userId
* @return
* @throws Exception
*/
public ReturnBean selectUserById(String userId) throws Exception {
log.info("====>>执行selectUserById方法");
ReturnBean returnBean = new ReturnBean();
// 判断参数是否合法
if (userId == null || userId == "") {
returnBean.setFailReturn("传入的参数不合法");
return returnBean;
}
// 查询
User user = null;
Map parameterMap = new HashMap<>();
parameterMap.put("USER_ID", userId);
try {
user = userMapper.selectUserByMap(parameterMap);
} catch (Exception e) {
log.error("依据id查找用户时发生异常", e);
returnBean.setFailReturn("程序内部发生错误");
return returnBean;
}
if (user != null) {
// 查找到了用户信息
returnBean.setSuccessReturn(user);
} else {
returnBean.setFailReturn("没有查找到此用户信息");
}
return returnBean;
}
}
其中ReturnBean为自定义的返回实体类,如下
package com.ahut.entity;
import java.io.Serializable;
/**
*
* @ClassName: ReturnBean
* @Description: 返回实体类
* @author cheng
* @date 2018年1月25日 下午5:24:54
*/
public class ReturnBean implements Serializable {
/**
* 序列化
*/
private static final long serialVersionUID = -2867756409591322259L;
/**
* 返回类型
*/
private String returnType;
/**
* 返回结果
*/
private Object returnValue;
/**
* 成功
*/
private final String SUCCESS = "SUCCESS";
/**
* 失败
*/
private final String FAIL = "FAIL";
/**
* 构造函数
*/
public ReturnBean() {
}
/**
* toString
*/
@Override
public String toString() {
return "ReturnBean [returnType=" + returnType + ", returnValue=" + returnValue + ", SUCCESS=" + SUCCESS
+ ", FAIL=" + FAIL + "]";
}
/**
*
* @Title: setSuccessReturn
* @Description: 设置成功返回
* @param returnValue
*/
public void setSuccessReturn(Object returnValue) {
this.returnType = this.SUCCESS;
this.returnValue = returnValue;
}
/**
*
* @Title: setFailReturn
* @Description: 设置失败返回
* @param returnValue
*/
public void setFailReturn(Object returnValue) {
this.returnType = this.FAIL;
this.returnValue = returnValue;
}
public String getReturnType() {
return returnType;
}
public void setReturnType(String returnType) {
this.returnType = returnType;
}
public Object getReturnValue() {
return returnValue;
}
public void setReturnValue(Object returnValue) {
this.returnValue = returnValue;
}
}