最近有用SSM写一些API 感觉大多是重复性的劳动 应该可以抽象出来配置化开发就好了
主要实现2个部分:
1.自动生成对模型(单表)的增删改查接口功能
2.配置化多表多参数功能
目前已经实现了第一步
github地址:https://github.com/dickwin/ICS/
在属性文件里:
application.properties
配置数据库信息和生成代码的地址:
jdbc.properties:
ics.config.driverClassName=com.mysql.jdbc.Driver
ics.config.url=jdbc:mysql://127.0.0.1:3306/ics?characterEncoding=gbk
ics.config.username=root
ics.config.password=root
ics.config.miniPoolSize=1
ics.config.maxPoolSize=20
ics.config.initialPoolSize=1
ics.config.maxIdleTime=25000
ics.config.acquireIncrement=1
ics.config.pakagedir=D:\exworkspace\ICS\src\main\java\com\ics
ics.config.mybatdir=D:\exworkspace\ICS\src\main\resources\mybatis
// 1.生成XXXMapper.java
// 2.生成xxxService.java
// 3.生成XXXController.java
// 4.生成xxxMapper.xml
项目结构图如下:
元数据统一的模板信息如下:
接口模板:
package com.ics.mapper;
import java.util.List;
import java.util.Map;
public interface @ModelMapper {
public int @addModel(Map paramMap);
public int @delModel(Map paramMap);
public int @updateModel(Map paramMap);
public List @getModel(Map paramMap);
}
service类模板:
package com.ics.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ics.mapper.@ModelMapper;
@Service
public class @ModelService {
@Autowired
private @ModelMapper @model;
public int @addModel(Map paramMap){
int ls=@model.@addModel(paramMap);
return ls;
}
public int @delModel(Map paramMap){
int ls= @model.@delModel(paramMap);
return ls;
}
public int @updateModel(Map paramMap){
int ls=@model.@updateModel(paramMap);
return ls;
}
public List @getModel(Map paramMap){
List ls=@model.@getModel(paramMap);
return ls;
}
}
mapper xml配置文件模板:
<?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="com.ics.mapper.@ModelMapper">
<!-- 增加Model -->
<insert id="@addModel" parameterType="java.util.HashMap">
insert into
@tablename
(@insertsqlcolumn)
values
(@insertsqlvalue)
</insert>
<!-- 删除Model -->
<delete id="@delModel" parameterType="java.util.HashMap">
delete from
@tablename
where
<if test="id !=null and id !=''">
id = #{id}
</if>
</delete>
<!-- 更新Model -->
<update id="@updateModel" parameterType="java.util.HashMap">
update
@tablename set @updatesql where id=#{id}
</update>
<!-- 查询Model -->
<select id="@getModel" resultType="java.util.HashMap">
select * from @tablename
</select>
</mapper>
controller模板:
package com.ics.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ics.service.@ModelService;
@Controller
@RequestMapping("/@model")
public class @ModelController extends BaseController{
private static Logger log = LogManager.getLogger(@ModelController.class.getName());
@Autowired
private @ModelService @model;
@RequestMapping(value = "/@getModel", method = RequestMethod.GET)
@ResponseBody
public Object @getModel() {
Map data = new HashMap();
data.put("ret","1");
try {
List rs = @model.@getModel(paramMap);
data.put("data",rs);
data.put("len",rs.size());
data.put("ret","0");
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
}
return data;
}
@RequestMapping(value = "/@addModel", method = RequestMethod.GET)
@ResponseBody
public Object @addModel() {
Map data = new HashMap();
data.put("ret","1");
try {
int rs = @model.@addModel(paramMap);
data.put("data", rs);
data.put("ret","0");
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
}
return data;
}
@RequestMapping(value = "/@delModel", method = RequestMethod.GET)
@ResponseBody
public Object @delModel() {
Map data = new HashMap();
data.put("ret","1");
try {
int rs = @model.@delModel(paramMap);
data.put("data", rs);
data.put("ret","0");
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
}
return data;
}
@RequestMapping(value = "/@updateModel", method = RequestMethod.GET)
@ResponseBody
public Object @updateModel() {
Map data = new HashMap();
data.put("ret","1");
try {
int rs = @model.@updateModel(paramMap);
data.put("data", rs);
data.put("ret","0");
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
}
return data;
}
}