文章目录
前言
实现 日志管理
基于 MBG 生成 entity,dao,service,serviceImpl,Controller及其xml文件
一、日志表
二、实现步骤
1.创建类 LogHelper,注入 logService
代码如下:
package org.honey.nongle.web.service;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.honey.nongle.core.util.IpUtil;
import org.honey.nongle.db.domain.LitemallAdmin;
import org.honey.nongle.db.domain.NongleLog;
import org.honey.nongle.db.service.NongleLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* 这里的日志类型设计成四种(当然开发者需要可以自己扩展)
* 一般日志:用户觉得需要查看的一般操作日志,建议是默认的日志级别
* 安全日志:用户安全相关的操作日志,例如登录、删除管理员
* 订单日志:用户交易相关的操作日志,例如订单发货、退款
* 其他日志:如果以上三种不合适,可以选择其他日志,建议是优先级最低的日志级别
* <p>
* 当然可能很多操作是不需要记录到数据库的,例如编辑商品、编辑广告品之类。
*/
@Component
public class LogHelper {
public static final Integer LOG_TYPE_GENERAL = 0;
public static final Integer LOG_TYPE_AUTH = 1;
public static final Integer LOG_TYPE_ORDER = 2;
public static final Integer LOG_TYPE_OTHER = 3;
@Autowired
private NongleLogService logService;
public void logGeneralSucceed(String action) {
logAdmin(LOG_TYPE_GENERAL, action, true, "", "");
}
public void logGeneralSucceed(String action, String result) {
logAdmin(LOG_TYPE_GENERAL, action, true, result, "");
}
public void logGeneralFail(String action, String error) {
logAdmin(LOG_TYPE_GENERAL, action, false, error, "");
}
public void logAuthSucceed(String action) {
logAdmin(LOG_TYPE_AUTH, action, true, "", "");
}
public void logAuthSucceed(String action, String result) {
logAdmin(LOG_TYPE_AUTH, action, true, result, "");
}
public void logAuthFail(String action, String error) {
logAdmin(LOG_TYPE_AUTH, action, false, error, "");
}
public void logOrderSucceed(String action) {
logAdmin(LOG_TYPE_ORDER, action, true, "", "");
}
public void logOrderSucceed(String action, String result) {
logAdmin(LOG_TYPE_ORDER, action, true, result, "");
}
public void logOrderFail(String action, String error) {
logAdmin(LOG_TYPE_ORDER, action, false, error, "");
}
public void logOtherSucceed(String action) {
logAdmin(LOG_TYPE_OTHER, action, true, "", "");
}
public void logOtherSucceed(String action, String result) {
logAdmin(LOG_TYPE_OTHER, action, true, result, "");
}
public void logOtherFail(String action, String error) {
logAdmin(LOG_TYPE_OTHER, action, false, error, "");
}
public void logAdmin(Integer type, String action, Boolean succeed, String result, String comment) {
NongleLog log = new NongleLog();
Subject currentUser = SecurityUtils.getSubject();
if (currentUser != null) {
LitemallAdmin admin = (LitemallAdmin) currentUser.getPrincipal();
if (admin != null) {
log.setAdmin(admin.getUsername());
} else {
log.setAdmin("游客");
}
} else {
log.setAdmin("游客");
}
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
if (request != null) {
log.setIp(IpUtil.getIpAddr(request));
}
log.setType(type);
log.setAction(action);
log.setStatus(succeed);
log.setResult(result);
log.setComment(comment);
logService.add(log);
}
}
2.创建 logService,注入 logMapper
代码如下:
package org.honey.nongle.db.service;
import com.github.pagehelper.PageHelper;
import org.honey.nongle.db.dao.NongleLogMapper;
import org.honey.nongle.db.domain.NongleLog;
import org.honey.nongle.db.domain.NongleLogExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class NongleLogService {
@Resource
private NongleLogMapper logMapper;
public void deleteById(Integer id) {
logMapper.logicalDeleteByPrimaryKey(id);
}
public void add(NongleLog log) {
log.setAddTime(LocalDateTime.now());
log.setUpdateTime(LocalDateTime.now());
logMapper.insertSelective(log);
}
public List<NongleLog> querySelective(String name, Integer page, Integer size, String sort, String order) {
NongleLogExample example = new NongleLogExample();
NongleLogExample.Criteria criteria = example.createCriteria();
if (!StringUtils.isEmpty(name)) {
criteria.andAdminLike("%" + name + "%");
}
criteria.andDeletedEqualTo(false);
if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
example.setOrderByClause(sort + " " + order);
}
PageHelper.startPage(page, size);
return logMapper.selectByExample(example);
}
}
3.MBG 生成创建 logMapper.java 接口 及 logMapper.xml
代码如下:
package org.honey.nongle.db.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.honey.nongle.db.domain.NongleLog;
import org.honey.nongle.db.domain.NongleLogExample;
public interface NongleLogMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
long countByExample(NongleLogExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
int deleteByExample(NongleLogExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
int deleteByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
int insert(NongleLog record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
int insertSelective(NongleLog record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
NongleLog selectOneByExample(NongleLogExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
NongleLog selectOneByExampleSelective(@Param("example") NongleLogExample example, @Param("selective") NongleLog.Column ... selective);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
List<NongleLog> selectByExampleSelective(@Param("example") NongleLogExample example, @Param("selective") NongleLog.Column ... selective);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
List<NongleLog> selectByExample(NongleLogExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
NongleLog selectByPrimaryKeySelective(@Param("id") Integer id, @Param("selective") NongleLog.Column ... selective);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
NongleLog selectByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
NongleLog selectByPrimaryKeyWithLogicalDelete(@Param("id") Integer id, @Param("andLogicalDeleted") boolean andLogicalDeleted);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
int updateByExampleSelective(@Param("record") NongleLog record, @Param("example") NongleLogExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
int updateByExample(@Param("record") NongleLog record, @Param("example") NongleLogExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
int updateByPrimaryKeySelective(NongleLog record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
int updateByPrimaryKey(NongleLog record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
int logicalDeleteByExample(@Param("example") NongleLogExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table nongle_log
*
* @mbg.generated
*/
int logicalDeleteByPrimaryKey(Integer id);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.honey.nongle.db.dao.NongleLogMapper">
<resultMap id="BaseResultMap" type="org.honey.nongle.db.domain.NongleLog">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="admin" jdbcType="VARCHAR" property="admin" />
<result column="ip" jdbcType="VARCHAR" property="ip" />
<result column="type" jdbcType="INTEGER" property="type" />
<result column="action" jdbcType="VARCHAR" property="action" />
<result column="status" jdbcType="BIT" property="status" />
<result column="result" jdbcType="VARCHAR" property="result" />
<result column="comment" jdbcType="VARCHAR" property="comment" />
<result column="add_time" jdbcType="TIMESTAMP" property="addTime" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
<result column="deleted" jdbcType="BIT" property="deleted" />
</resultMap>
<sql id="Example_Where_Clause">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
id, `admin`, ip, `type`, `action`, `status`, `result`, `comment`, add_time, update_time,
deleted
</sql>
<select id="selectByExample" parameterType="org.honey.nongle.db.domain.NongleLogExample" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from nongle_log
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByExampleSelective" parameterType="map" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<if test="example.distinct">
distinct
</if>
<choose>
<when test="selective != null and selective.length > 0">
<foreach collection="selective" item="column" separator=",">
${column.aliasedEscapedColumnName}
</foreach>
</when>
<otherwise>
<include refid="Base_Column_List" />
</otherwise>
</choose>
from nongle_log
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
<if test="example.orderByClause != null">
order by ${example.orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List" />
from nongle_log
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByPrimaryKeyWithLogicalDelete" parameterType="map" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List" />
from nongle_log
where id = #{id,jdbcType=INTEGER}
and deleted =
<choose>
<when test="andLogicalDeleted">
1
</when>
<otherwise>
0
</otherwise>
</choose>
</select>
<select id="selectByPrimaryKeySelective" parameterType="map" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<choose>
<when test="selective != null and selective.length > 0">
<foreach collection="selective" item="column" separator=",">
${column.aliasedEscapedColumnName}
</foreach>
</when>
<otherwise>
<include refid="Base_Column_List" />
</otherwise>
</choose>
from nongle_log
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
delete from nongle_log
where id = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="org.honey.nongle.db.domain.NongleLogExample">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
delete from nongle_log
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="org.honey.nongle.db.domain.NongleLog">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into nongle_log (`admin`, ip, `type`,
`action`, `status`, `result`,
`comment`, add_time, update_time,
deleted)
values (#{admin,jdbcType=VARCHAR}, #{ip,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER},
#{action,jdbcType=VARCHAR}, #{status,jdbcType=BIT}, #{result,jdbcType=VARCHAR},
#{comment,jdbcType=VARCHAR}, #{addTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP},
#{deleted,jdbcType=BIT})
</insert>
<insert id="insertSelective" parameterType="org.honey.nongle.db.domain.NongleLog">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into nongle_log
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="admin != null">
`admin`,
</if>
<if test="ip != null">
ip,
</if>
<if test="type != null">
`type`,
</if>
<if test="action != null">
`action`,
</if>
<if test="status != null">
`status`,
</if>
<if test="result != null">
`result`,
</if>
<if test="comment != null">
`comment`,
</if>
<if test="addTime != null">
add_time,
</if>
<if test="updateTime != null">
update_time,
</if>
<if test="deleted != null">
deleted,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="admin != null">
#{admin,jdbcType=VARCHAR},
</if>
<if test="ip != null">
#{ip,jdbcType=VARCHAR},
</if>
<if test="type != null">
#{type,jdbcType=INTEGER},
</if>
<if test="action != null">
#{action,jdbcType=VARCHAR},
</if>
<if test="status != null">
#{status,jdbcType=BIT},
</if>
<if test="result != null">
#{result,jdbcType=VARCHAR},
</if>
<if test="comment != null">
#{comment,jdbcType=VARCHAR},
</if>
<if test="addTime != null">
#{addTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
<if test="deleted != null">
#{deleted,jdbcType=BIT},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="org.honey.nongle.db.domain.NongleLogExample" resultType="java.lang.Long">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select count(*) from nongle_log
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update nongle_log
<set>
<if test="record.id != null">
id = #{record.id,jdbcType=INTEGER},
</if>
<if test="record.admin != null">
`admin` = #{record.admin,jdbcType=VARCHAR},
</if>
<if test="record.ip != null">
ip = #{record.ip,jdbcType=VARCHAR},
</if>
<if test="record.type != null">
`type` = #{record.type,jdbcType=INTEGER},
</if>
<if test="record.action != null">
`action` = #{record.action,jdbcType=VARCHAR},
</if>
<if test="record.status != null">
`status` = #{record.status,jdbcType=BIT},
</if>
<if test="record.result != null">
`result` = #{record.result,jdbcType=VARCHAR},
</if>
<if test="record.comment != null">
`comment` = #{record.comment,jdbcType=VARCHAR},
</if>
<if test="record.addTime != null">
add_time = #{record.addTime,jdbcType=TIMESTAMP},
</if>
<if test="record.updateTime != null">
update_time = #{record.updateTime,jdbcType=TIMESTAMP},
</if>
<if test="record.deleted != null">
deleted = #{record.deleted,jdbcType=BIT},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update nongle_log
set id = #{record.id,jdbcType=INTEGER},
`admin` = #{record.admin,jdbcType=VARCHAR},
ip = #{record.ip,jdbcType=VARCHAR},
`type` = #{record.type,jdbcType=INTEGER},
`action` = #{record.action,jdbcType=VARCHAR},
`status` = #{record.status,jdbcType=BIT},
`result` = #{record.result,jdbcType=VARCHAR},
`comment` = #{record.comment,jdbcType=VARCHAR},
add_time = #{record.addTime,jdbcType=TIMESTAMP},
update_time = #{record.updateTime,jdbcType=TIMESTAMP},
deleted = #{record.deleted,jdbcType=BIT}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="org.honey.nongle.db.domain.NongleLog">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update nongle_log
<set>
<if test="admin != null">
`admin` = #{admin,jdbcType=VARCHAR},
</if>
<if test="ip != null">
ip = #{ip,jdbcType=VARCHAR},
</if>
<if test="type != null">
`type` = #{type,jdbcType=INTEGER},
</if>
<if test="action != null">
`action` = #{action,jdbcType=VARCHAR},
</if>
<if test="status != null">
`status` = #{status,jdbcType=BIT},
</if>
<if test="result != null">
`result` = #{result,jdbcType=VARCHAR},
</if>
<if test="comment != null">
`comment` = #{comment,jdbcType=VARCHAR},
</if>
<if test="addTime != null">
add_time = #{addTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
update_time = #{updateTime,jdbcType=TIMESTAMP},
</if>
<if test="deleted != null">
deleted = #{deleted,jdbcType=BIT},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="org.honey.nongle.db.domain.NongleLog">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update nongle_log
set `admin` = #{admin,jdbcType=VARCHAR},
ip = #{ip,jdbcType=VARCHAR},
`type` = #{type,jdbcType=INTEGER},
`action` = #{action,jdbcType=VARCHAR},
`status` = #{status,jdbcType=BIT},
`result` = #{result,jdbcType=VARCHAR},
`comment` = #{comment,jdbcType=VARCHAR},
add_time = #{addTime,jdbcType=TIMESTAMP},
update_time = #{updateTime,jdbcType=TIMESTAMP},
deleted = #{deleted,jdbcType=BIT}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectOneByExample" parameterType="org.honey.nongle.db.domain.NongleLogExample" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List" />
from nongle_log
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
limit 1
</select>
<select id="selectOneByExampleSelective" parameterType="map" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<choose>
<when test="selective != null and selective.length > 0">
<foreach collection="selective" item="column" separator=",">
${column.aliasedEscapedColumnName}
</foreach>
</when>
<otherwise>
<include refid="Base_Column_List" />
</otherwise>
</choose>
from nongle_log
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
<if test="example.orderByClause != null">
order by ${example.orderByClause}
</if>
limit 1
</select>
<update id="logicalDeleteByExample" parameterType="map">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update nongle_log set deleted = 1
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="logicalDeleteByPrimaryKey" parameterType="java.lang.Integer">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update nongle_log set deleted = 1
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
三、MBG 生成 entity,dao,service,serviceImpl,Controller及其xml文件
1. pom 配置文件
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>nongle-db</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>org.honey</groupId>
<artifactId>nongle</artifactId>
<version>0.1.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
</dependencies>
// Here
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>
mybatis-generator/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
2. MBG 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="mysqlgenerator" targetRuntime="MyBatis3">
<property name="autoDelimitKeywords" value="true"/>
<!--可以使用``包括字段名,避免字段名与sql保留字冲突报错-->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 自动生成toString方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 自动生成equals方法和hashcode方法 -->
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
<!-- 非官方插件 https://github.com/itfsw/mybatis-generator-plugin -->
<!-- 查询单条数据插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"/>
<!-- 查询结果选择性返回插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.SelectSelectivePlugin"/>
<!-- Example Criteria 增强插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.ExampleEnhancedPlugin"/>
<!-- 数据Model属性对应Column获取插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>
<!-- 逻辑删除插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.LogicalDeletePlugin">
<!-- 这里配置的是全局逻辑删除列和逻辑删除值,当然在table中配置的值会覆盖该全局配置 -->
<!-- 逻辑删除列类型只能为数字、字符串或者布尔类型 -->
<property name="logicalDeleteColumn" value="deleted"/>
<!-- 逻辑删除-已删除值 -->
<property name="logicalDeleteValue" value="1"/>
<!-- 逻辑删除-未删除值 -->
<property name="logicalUnDeleteValue" value="0"/>
</plugin>
<commentGenerator>
<property name="suppressDate" value="true"/>
<!--<property name="suppressAllComments" value="true"/>-->
</commentGenerator>
<!--数据库连接信息-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/nongle?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&verifyServerCertificate=false&useSSL=false&nullCatalogMeansCurrent=true"
userId="nongle"
password="nongle123456"/>
<javaTypeResolver>
<property name="useJSR310Types" value="true"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="org.honey.nongle.db.domain" targetProject="src/main/java"/>
<sqlMapGenerator targetPackage="org.honey.nongle.db.dao" targetProject="src/main/resources"/>
<javaClientGenerator type="XMLMAPPER" targetPackage="org.honey.nongle.db.dao"
targetProject="src/main/java"/>
<!--表名-->
<table tableName="nongle_ad">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_address">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_admin">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
<columnOverride column="role_ids" javaType="java.lang.Integer[]"
typeHandler="org.honey.nongle.db.mybatis.JsonIntegerArrayTypeHandler"/>
</table>
<table tableName="nongle_brand">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_cart">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
<columnOverride column="specifications" javaType="java.lang.String[]"
typeHandler="org.honey.nongle.db.mybatis.JsonStringArrayTypeHandler"/>
</table>
<table tableName="nongle_category">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_collect">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_comment">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
<columnOverride column="pic_urls" javaType="java.lang.String[]"
typeHandler="org.honey.nongle.db.mybatis.JsonStringArrayTypeHandler"/>
</table>
<table tableName="nongle_feedback">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
<columnOverride column="pic_urls" javaType="java.lang.String[]"
typeHandler="org.honey.nongle.db.mybatis.JsonStringArrayTypeHandler"/>
</table>
<table tableName="nongle_footprint">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_goods">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
<columnOverride column="gallery" javaType="java.lang.String[]"
typeHandler="org.honey.nongle.db.mybatis.JsonStringArrayTypeHandler"/>
</table>
<table tableName="nongle_goods_attribute">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_goods_specification">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_goods_product">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
<columnOverride column="specifications" javaType="java.lang.String[]"
typeHandler="org.honey.nongle.db.mybatis.JsonStringArrayTypeHandler"/>
</table>
<table tableName="nongle_issue">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_keyword">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_order">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_order_goods">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
<columnOverride column="specifications" javaType="java.lang.String[]"
typeHandler="org.honey.nongle.db.mybatis.JsonStringArrayTypeHandler"/>
<columnOverride column="comments" javaType="java.lang.Integer[]"
typeHandler="org.honey.nongle.db.mybatis.JsonIntegerArrayTypeHandler"/>
</table>
<table tableName="nongle_region">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_search_history">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_storage">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_topic">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
<columnOverride column="goods" javaType="java.lang.Integer[]"
typeHandler="org.honey.nongle.db.mybatis.JsonIntegerArrayTypeHandler"/>
</table>
<table tableName="nongle_user">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_system">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_groupon_rules">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_groupon">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_coupon">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
<columnOverride column="goods_value" javaType="java.lang.Integer[]"
typeHandler="org.honey.nongle.db.mybatis.JsonIntegerArrayTypeHandler"/>
</table>
<table tableName="nongle_coupon_user">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_role">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_permission">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_log">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_notice">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_notice_admin">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
<table tableName="nongle_aftersale">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
<columnOverride column="pictures" javaType="java.lang.String[]"
typeHandler="org.honey.nongle.db.mybatis.JsonStringArrayTypeHandler"/>
</table>
</context>
</generatorConfiguration>
3. 运行插件
IntelliJ IDEA,可以在Maven 面板双击mybatis-generator:gennerate,就会自动生成mybatis相关文件。