代码生成器--4.30课程

博客内容主要是继续上节课未完成的工作,生成controller和model的代码,同时使用jdbc连接数据库,还提到在Java代码中运行select查询语句时要更改表名并灵活运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、继续上节课未完成生成controller和model的代码

package com.zq.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
#if(${modelName} == 'menu')
import com.zq.model.${modelClass};
import com.zq.service.${modelClass}Service;
#else
import com.zq.model.${modelClass};
import com.zq.service.${modelClass}Service;
import com.zq.model.Menu;
import com.zq.service.MenuService;
#end


@RequestMapping("/${modelName}")
@Controller
public class ${modelClass}Controller {
	
#if(${modelName} == 'menu')
	@Autowired
	private ${modelClass}Service ${modelName}Service;
#else
	@Autowired
	private ${modelClass}Service ${modelName}Service;
	@Autowired
	private MenuService menuService;
	
#end
	
	@ResponseBody
	@RequestMapping("/delete.do")
	public boolean delete(Integer id){
		try{
			${modelName}.delete(id);
		}catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
		return true;
	}
	
	@ResponseBody
	@RequestMapping("/findById.do")
	public ${modelClass} findById(Integer id){
		return ${modelName}.findById(id);
	}
	
	@ResponseBody
	@RequestMapping("/create.do")
	public boolean create(${modelClass} ${modelName}){
		try{
			${modelName}.create(${modelName});
		}catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
		return true;
	}
	
	@RequestMapping("/list.do")
	public String list(${modelClass} ${modelName},Model model,
			@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum,
			@RequestParam(required=true,value="pageSize",defaultValue="3") Integer pageSize
			){
		PageInfo<${modelClass}> ${modelClass}s  = ${modelName}.list(pageNum,pageSize,${modelName);
		model.addAttribute("pageInfo", ${modelName}s);
		
		List<Menu> menuList = menuService.list(null);
		model.addAttribute("menuList", menuList);
		
		return "${modelName}";
	}
	
	@ResponseBody
	@RequestMapping("/listData.do")
	public List<${modelClass}> list(${modelClass} ${modelName},Model model){
		return ${modelName}.list(${modelName);
	}
}

//controller的
		Template controllerVm = ve.getTemplate("/WebContent/WEB-INF/vm/controller.vm");
		CodeBuilder.merge(controllerVm, ctx, rootPath +"src/com/zq/controller/" + modelClass +"Controller.java");	
		
		//model
		//从数据库中查询表的字段:字段的名称 字段的类型 字段的注释 good_name GoddName
		List<CodeBean> list = selectColumn(modelName);
		ctx.put("columnList",list);
		Template modelVm = ve.getTemplate("/WebContent/WEB-INF/vm/model.vm");
		CodeBuilder.merge(modelVm, ctx, rootPath +"src/com/zq/model/" + modelClass +".java");	

2、使用jdbc连接数据库

select查询语句为

select t.COLLATION_NAME,t.DATA_TYPE,t.COLUMN_COMMENT
from information_schema.`COLUMNS` t
where t.TABLE_NAME='menu'
and t.TABLE_SCHEMA='shop'

但是要注意在Java代码中运行时要讲表名改了,灵活运用

代码如下

//jdbc连接数据库
	private static List<CodeBean> selectColumn(String modelName) {
		List<CodeBean> list = new ArrayList<CodeBean>();
		
		//java.sql包里面的类
		Connection  conn = null;
		PreparedStatement ps = null;
		ResultSet rs =null;
		
		try{
			//
			Class.forName("com.mysql.jdbc.Driver");
			//创建数据库的连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop","root","");
			//sql 语句
			String sql = "select t.COLLATION_NAME,t.DATA_TYPE,t.COLUMN_COMMENT "
					+" from information_schema.`COLUMNS` t "
						+" where t.TABLE_NAME='"+modelName+"' "
							+" and t.TABLE_SCHEMA='shop' ";
			//执行sql语句
			ps = conn.prepareStatement(sql);
			//获取查询结果
			rs = ps.executeQuery();
			//循环遍历查询,塞到columnList里面
			while(rs.next()){
				CodeBean bean = new CodeBean();
				bean.setColumnName(rs.getString(1));
				bean.setColumnComment(rs.getString(3));
				
				//类型要做判断
				String type = rs.getString(2);
				if(type.equalsIgnoreCase("varchar") || type.equalsIgnoreCase("varchar2")){//是不是字符串
					bean.setColumnType("String");
				}else if(type.equalsIgnoreCase("int") || type.equalsIgnoreCase("integer")){//整数
					bean.setColumnType("Integer");
				}else if(type.equalsIgnoreCase("float") ||
						type.equalsIgnoreCase("double") ||
						type.equalsIgnoreCase("decimal")){//小数float double decimal
					bean.setColumnType("Double");
				}else{
					bean.setColumnType("String");
				}
			
				//good_name goodName GoodName
				bean.setAttrName(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, bean.getColumnName()));
				bean.setAttrName(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, bean.getColumnName()));
				
				list.add(bean);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				rs.close();
				ps.close();
				conn.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return list;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值