1.判断提交方式
if(request.getMethod().equals("POST")){}
2.返回json
@ResponseBody
3.限定请求方式
@RequestMapping(value="/login",method= RequestMethod.POST)
4.session
// String a = "aaaaaa";
// //当前用户的会话对象”为空(第一次访问时)则创建一个新的会话对象返回
// HttpSession session = request.getSession(true);
// System.out.println("session_a:"+session.getAttribute("session_a"));
// // 创建session
// session.setAttribute("session_a",a);
// String b = "bbbbbbb";
// session.setAttribute("session_b" , b);
// // 获取session
// System.out.println("session_a"+session.getAttribute("session_a"));
// System.out.println("session_b"+session.getAttribute("session_b"));
5.controller传参到视图
第一种
@Autowired
private AccountService accountService;
@RequestMapping("/index")
public String index(Model model) {
//调动业务层方法service
List list = accountService.findAll();
//存进数据
model.addAttribute("list",list);
return "admin/login/index";
}
第二种
ModelAndView mv=new ModelAndView();
mv.addObject("userinfos",all);
mv.setViewName("allUser");
return mv
- 实现MySQL自动生成实体类
MySQLGeneratorEntityUtil.java
package com.zf.utiles;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MySQLGeneratorEntityUtil {
//表名
private String tableName;
//列名数组
private String[] colNames;
//列名类型数组
private String[] colTypes;
//列名大小数组
private int[] colSizes;
//列名注释
private Map colNamesComment = new HashMap();
//是否需要导入包java.util.*
private boolean needUtil = false;
//是否需要导入包java.sql.*
private boolean needSql = false;
//是否需要导入包java.math.BigDecimal
private boolean needBigDecimal = false;
//是否创建EntityHelper
private boolean needEntityHelper = true;
private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final String SQL = "SELECT * FROM ";// 数据库操作
// 数据库配置信息
private static final String URL = "jdbc:mysql://127.0.0.1:3306/zfadmin_java";
private static final String NAME = "root";
private static final String PASS = "";
private static final String DRIVER = "com.mysql.jdbc.Driver";
//指定实体生成所在包的路径
private static String basePath = new File("").getAbsolutePath();
//指定包名
private static String packageOutPath = "com.zf.entity";
//作者名字
private String authorName = "子枫";
//指定需要生成的表的表名,全部生成设置为null
private String[] generateTables = null;
//主键
private static String pk;
private MySQLGeneratorEntityUtil() {
}
/**
* @description 生成class的所有内容
*/
private String parse() {
StringBuffer sb = new StringBuffer();
sb.append("package " + packageOutPath + ";\r\n");
sb.append("\r\n");
// 判断是否导入工具包
if (needUtil) {
sb.append("import java.util.Date;\r\n");
}
if (needSql) {
sb.append("import java.sql.*;\r\n");
}
for (int i = 0; i < colNames.length; i++) {
String hasbd = sqlType2JavaType(colTypes[i]);
if(hasbd =="BigDecimal" || "BigDecimal".equals(hasbd)) {needBigDecimal=true;}
}
if(needBigDecimal) {
sb.append("import java.math.BigDecimal;\r\n");
}
// 注释部分
sb.append("/**\r\n");
sb.append(" * table name: " + tableName + "\r\n");
sb.append(" * author name: " + authorName + "\r\n");
sb.append(" * create time: " + SDF.format(new Date()) + "\r\n");
sb.append(" */ \r\n");
// 实体部分
String classExtends = "";
if(needEntityHelper) {
classExtends=" extends EntityHelper";
}
sb.append("public class " + under2camel(tableName, true) + classExtends + "{\r\n\r\n");
processAllAttrs(sb);// 属性
sb.append("\r\n");
processConstructor(sb);//构造函数
processAllMethod(sb);// get set方法
processToString(sb);
if(needEntityHelper) {
processEntityHelper(sb,pk);
}
sb.append("}\r\n");
return sb.toString();
}
/**
* @param sb
* @description 生成所有成员变量及注释
* @author paul
* @version V1.0
*/
private void processAllAttrs(StringBuffer sb) {
for (int i = 0; i < colNames.length; i++) {
if(colNamesComment.get(colNames[i])!=null &&!"".equals(colNamesComment.get(colNames[i]))) {
sb.append("\t/*"+colNamesComment.get(colNames[i])+"*/\r\n");
}
sb.append("\tprivate " + sqlType2JavaType(colTypes[i]) + " " + colNames[i] + ";\r\n");
}
}
/**
* EntityHelper
* @param sb
* @param pk
*/
private void processEntityHelper(StringBuffer sb,String pk) {
sb.append("\t@Override\r\n");
sb.append("\tpublic String getPrimaryKey() {\r\n");
sb.append("\t\treturn \""+pk+"\";\r\n");
sb.append("\t}\r\n");
}
/**
* 重写toString()方法
* @param sb
*/
private void processToString(StringBuffer sb) {
sb.append("\t@Override\r\n\tpublic String toString() {\r\n");
sb.append("\t\treturn \"" +tableName + "[\" + \r\n");
for (int i = 0; i < colNames.length; i++) {
if (i != 0)
sb.append("\t\t\t\", ");
if (i == 0)
sb.append("\t\t\t\"");
sb.append(colNames[i] + "=\" + "
+ colNames[i]).append(" + \r\n");
if (i == colNames.length - 1) {
sb.append("\t\t\t\"]\";\r\n");
}
}
sb.append("\t}\r\n");
}
/**
* 构造函数
* @param sb
*/
private void processConstructor(StringBuffer sb) {
StringBuffer p = new StringBuffer();
StringBuffer v = new StringBuffer();
for(int i = 0; i < colNames.length; i++) {
p.append(sqlType2JavaType(colTypes[i])+" "+colNames[i]);
if(i!=colNames.length-1) {
p.append(",");
}
v.append("\t\tthis."+colNames[i]+"="+colNames[i]+";\r\n");
}
//无参数构造函数
sb.append("\tpublic "+under2camel(tableName,true)+"() {\r\n");
sb.append("\t\tsuper();\r\n");
sb.append("\t}\r\n");
//带参构造函数
sb.append("\tpublic "+under2camel(tableName,true)+"("+p.toString()+") {\r\n");
sb.append(v.toString());
sb.append("\t}\r\n");
}
/**
* @param sb
* @description 生成所有get/set方法
*/
private void processAllMethod(StringBuffer sb) {
for (int i = 0; i < colNames.length; i++) {
sb.append("\tpublic void set" + initCap(colNames[i]) + "(" + sqlType2JavaType(colTypes[i]) + " "
+ colNames[i] + "){\r\n");
sb.append("\t\tthis." + colNames[i] + "=" + colNames[i] + ";\r\n");
sb.append("\t}\r\n");
sb.append("\tpublic " + sqlType2JavaType(colTypes[i]) + " get" + initCap(colNames[i]) + "(){\r\n");
sb.append("\t\treturn " + colNames[i] + ";\r\n");
sb.append("\t}\r\n");
}
}
/**
* @param str 传入字符串
* @return
* @description 将传入字符串的首字母转成大写
*/
private String initCap(String str) {
char[] ch = str.toCharArray();
if (ch[0] >= 'a' && ch[0] <= 'z')
ch[0] = (char) (ch[0] - 32);
return new String(ch);
}
/**
* 功能:下划线命名转驼峰命名
* @param s
* @param fistCharToUpperCase 首字母是否大写
* @author 呐喊
* @return
*/
private String under2camel(String s,boolean fistCharToUpperCase) {
String separator = "_";
String under="";
s = s.toLowerCase().replace(separator, " ");
String sarr[]=s.split(" ");
for(int i=0;i tableNames = new ArrayList<>();
if (generateTables == null) {
//从元数据中获取到所有的表名
ResultSet rs = db.getTables(null, null, null, new String[] { "TABLE" });
while (rs.next()) tableNames.add(rs.getString(3));
} else {
for (String tableName : generateTables) tableNames.add(tableName);
}
if(needEntityHelper) {
EntityHelper();
}
String tableSql;
PrintWriter pw = null;
for (int j = 0; j < tableNames.size(); j++) {
tableName = tableNames.get(j);
tableSql = SQL + tableName;
pStemt = con.prepareStatement(tableSql);
ResultSetMetaData rsmd = pStemt.getMetaData();
ResultSet rsk = con.getMetaData().getPrimaryKeys(con.getCatalog().toLowerCase(), null, tableName);
if (rsk.next()) {
String primaryKey = rsk.getString("COLUMN_NAME");
pk=primaryKey;
}
int size = rsmd.getColumnCount();
colNames = new String[size];
colTypes = new String[size];
colSizes = new int[size];
//获取所需的信息
for (int i = 0; i < size; i++) {
colNames[i] = rsmd.getColumnName(i + 1);
colTypes[i] = rsmd.getColumnTypeName(i + 1);
if (colTypes[i].equalsIgnoreCase("datetime"))
needUtil = true;
if (colTypes[i].equalsIgnoreCase("image") || colTypes[i].equalsIgnoreCase("text"))
needSql = true;
colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
}
//获取字段注释
ResultSet rsComment = pStemt.executeQuery("show full columns from " + tableName);
while (rsComment.next()) {
colNamesComment.put(rsComment.getString("Field"), rsComment.getString("Comment"));
}
//解析生成实体java文件的所有内容
String content = parse();
//输出生成文件
String dirName = MySQLGeneratorEntityUtil.pkgDirName();
String javaPath = dirName + "/" + under2camel(tableName, true) + ".java";
FileWriter fw = new FileWriter(javaPath);
pw = new PrintWriter(fw);
pw.println(content);
pw.flush();
System.out.println("create class 【" + tableName + "】");
}
if (pw != null)
pw.close();
}
public static void main(String[] args) {
MySQLGeneratorEntityUtil instance = new MySQLGeneratorEntityUtil();
//instance.basePath=""; //指定生成的位置,默认是当前工程
try {
instance.generate();
System.out.println("generate Entity to classes successful!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
- idea 端口1099改为其他的,无法进入debug模式
https://www.cnblogs.com/LeoBo...
mac关闭端口 https://blog.csdn.net/aaaaazq...
- session
更多精彩文章请关注 王明昌博客