生成SpringBoot和前端代码工具: CodeMan

引言

我们在SpringBoot的开发过程中,有的时候,增加了一些新表,我们需要生成新的Entity Java类和DAO以及Service;那么有没有一种工具,能够帮我们自动生成代码?笔者亲自测试了一下,感觉下面这款工具还挺不错的!现推荐给大家!

CodeMan的使用

代码地址,https://gitee.com/zrxjava/codeMan;项目名称CodeMan,作者为小螺旋丸。
有Window和Mac的安装版本,其安装版本可以到下面的地址下载;
https://gitee.com/zrxjava/code_generator_v201
在这里插入图片描述
下载完后,可以进行安装;安装的时候其会要求输入激活码,大家请关注下面的公众号,然后在公众号里面留言:“激活码获取” ,则公众号会自动给你发来激活码!
在这里插入图片描述
以Windows版本为例子,解压缩后,直接双击“codeMan.exe”,就可以生成代码了!
在这里插入图片描述

在这里插入图片描述
Step1. 配置你的数据连接
在这里插入图片描述
Step2 填写项目名称和选择配置:SpringBoot程序,前后端分离,Vue前端
在这里插入图片描述
Step3 选择要生成Entity和DAO类的表和字段,包括主键字段
在这里插入图片描述
Step4 点击生成代码
在这里插入图片描述
在这里插入图片描述
Step5 导入生成的代码
在这里插入图片描述
Step6 查看代码
生成代码的还是很规范的,Controller层,Service层,DAO层,Entity层

在这里插入图片描述
以DemoRoleServiceImpl.java为例子,其会生成基本的CRUD的代码

package helloworlddemo.service.impl;

import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import helloworlddemo.dao.DemoRoleDao;
import helloworlddemo.service.DemoRoleService;
import helloworlddemo.entity.PageData;
import helloworlddemo.utils.ExcelUtil;
import helloworlddemo.utils.PageUtil;
import java.util.LinkedHashMap;
import helloworlddemo.entity.DemoRoleEntity;
import java.util.List;
import java.util.Map;

@Service
public class DemoRoleServiceImpl implements DemoRoleService {
	private final DemoRoleDao dao;
	@Autowired
	public DemoRoleServiceImpl(DemoRoleDao dao) {
		this.dao = dao;
	}

	@Override
	public void add(DemoRoleEntity entity) {
		dao.add(entity);
	}

	@Override
	public void delete(DemoRoleEntity entity) {
		dao.delete(entity);
	}

	@Override
	public void update(DemoRoleEntity entity) {
		dao.update(entity);
	}

	@Override
	public List<DemoRoleEntity> select(DemoRoleEntity entity) {
		return dao.select(entity);
	}

	@Override
	public PageData<DemoRoleEntity> likeSelect(DemoRoleEntity entity) {
		return PageUtil.getPageData(entity, dao);
	}

	@Override
	public void exportExcel(DemoRoleEntity entity, HttpServletResponse response) {

		// 获取头部信息(可以设置为动态)
		String[] headList = new String[] { "id", "role_name", "role_code", "description"};

		String[] headEngList = new String[]{ "id", "roleName", "roleCode", "description"};

		String[] describeList = new String[] { "", "", "", ""};

		//设置头部以及描述信息
        Map<String, String> headAndDescribeMap = new LinkedHashMap<>();
        for (int i = 0; i < headEngList.length; i++) {
            headAndDescribeMap.put(headEngList[i], describeList[i]);
        }

		ExcelUtil.exportExcel(entity, response, dao, headList, headAndDescribeMap);
	}
}

DAO类: DemoRoleDao 的代码如下:

package helloworlddemo.dao;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import helloworlddemo.entity.DemoRoleEntity;
@Mapper
@Repository
public interface DemoRoleDao extends BaseDao<DemoRoleEntity> {

}

BaseDao的代码如下:

package helloworlddemo.dao;

import java.util.List;


public interface BaseDao<E> {

    void add(E map);

    void delete(E map);

    void update(E map);

    List<E> select(E map);

    List<E> likeSelect(E entity);

    Long likeSelectCount(E entity);

}

Role的SQL Mapper代码如下:

<?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="helloworlddemo.dao.DemoRoleDao">

    <!--添加-->
	<insert id="add" parameterType="helloworlddemo.entity.DemoRoleEntity">
		insert into demo.role
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="roleName != null">
                role_name,
            </if>
            <if test="roleCode != null">
                role_code,
            </if>
            <if test="description != null">
                description,
            </if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
				<if test="id != null">
					#{id},
				</if>
				<if test="roleName != null">
					#{roleName},
				</if>
				<if test="roleCode != null">
					#{roleCode},
				</if>
				<if test="description != null">
					#{description},
				</if>
		</trim>
	</insert>

    <!--删除-->
	<delete id="delete" parameterType="helloworlddemo.entity.DemoRoleEntity">
		delete from demo.role
		<where>
            <if test="id != null">
                and id=#{id}
            </if>
            <if test="id == null">
                and 1 = 0
            </if>
		</where>
	</delete>

    <!--更新-->
	<update id="update" parameterType="helloworlddemo.entity.DemoRoleEntity">
		update demo.role
		<trim prefix="set" suffixOverrides=",">
				<if test="id != null">
					id=#{id},
				</if>
				<if test="roleName != null">
					role_name=#{roleName},
				</if>
				<if test="roleCode != null">
					role_code=#{roleCode},
				</if>
				<if test="description != null">
					description=#{description},
				</if>
		</trim>
		<where>
            <if test="id != null">
                and id=#{id}
            </if>
            <if test="id == null">
                and 1 = 0
            </if>
		</where>
	</update>

    <!--固定条件查询-->
	<select id="select" parameterType="helloworlddemo.entity.DemoRoleEntity"
		resultType="helloworlddemo.entity.DemoRoleEntity">
			select
				id as "id",
				role_name as "roleName",
				role_code as "roleCode",
				description as "description"
            from demo.role
			<where>
					<if test="id != null">
						and id=#{id}
					</if>
					<if test="roleName != null">
						and role_name=#{roleName}
					</if>
					<if test="roleCode != null">
						and role_code=#{roleCode}
					</if>
					<if test="description != null">
						and description=#{description}
					</if>
			</where>
			<if test="orderStr != '' and orderStr != null">
				order by ${orderStr}
			</if>
            <if test="start != null and pageSize != null">
                limit #{pageSize} offset #{start}
            </if>


	</select>

    <!--分页(模糊查询的公共条件)-->
    <sql id="likeSelectConditions">
                <if test="id != null and id != '' ">
                and id <![CDATA[=]]> #{id}
                </if>
                <if test="roleName != null and roleName != '' ">
                and role_name <![CDATA[=]]> #{roleName}
                </if>
                <if test="roleCode != null and roleCode != '' ">
                and role_code <![CDATA[=]]> #{roleCode}
                </if>
    </sql>

    <!--分页(模糊)查询-->
	<select id="likeSelect" parameterType="helloworlddemo.entity.DemoRoleEntity"
		resultType="helloworlddemo.entity.DemoRoleEntity">
			select
				id as "id",
				role_name as "roleName",
				role_code as "roleCode",
				description as "description"

                from demo.role

			<where>
                <include refid="likeSelectConditions"/>
			</where>
			<if test="orderStr != '' and orderStr != null">
				order by ${orderStr}
			</if>
            <if test="start != null and pageSize != null">
                limit #{pageSize} offset #{start}
            </if>


	</select>

    <!--分页(模糊)查询条数-->
	<select id="likeSelectCount" parameterType="helloworlddemo.entity.DemoRoleEntity"
		resultType="java.lang.Long">
        select
            count(1)
        from demo.role

		<where>
            <include refid="likeSelectConditions"/>
		</where>
	</select>

</mapper>

Step7 启动项目

Step8. 打开Swagger的UI
http://127.0.0.1:8080/helloworlddemo/swagger-ui.html
在这里插入图片描述
Step9. 打开登录页面 http://127.0.0.1:8080/helloworlddemo/views/login.html
在这里插入图片描述
很可惜登录不进来!那么原因是什么呢? 因为其默认需要在数据表里面有一个user表;
user表里面有两个字段name和password;回到数据库,插入上面的表;

CREATE TABLE demo."user" (
	id int8 NULL,
	"name" varchar NULL,
	"password" varchar NULL
);

插入一条数据
在这里插入图片描述
另外如果你的数据看是PostgreSQL,在源代码的loginMapper.xml文件里面加入namespace的前缀,比如笔者的namespace前缀是demo,然后重新启动!
在这里插入图片描述
Step10. 恭喜你,登录成功!
在这里插入图片描述

其他工具

其他生成代码的工具如下,仅仅供参考:

  1. SPTools: SPAdmin 目前有1.4k的star
    https://gitee.com/52itstyle/SPTools
    在这里插入图片描述

  2. renren 代码生成
    https://gitee.com/renrenio/renren-security
    在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值