您的位置:首页 > 编程语言 > Java开发

Spring+Hibernate 零散知识点

2016-08-30 09:29 489 查看

MySQL

创建database 时设定字符串
create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;


查询字符集show variables like 'character%';

修改字符集,修改配置文件my.ini
在[client]中加入。# Here follows entries for some specificprogramsdefault-character-set= utf8在[mysqld]中加入character-set-server = utf8

Hibernate

自动创建表的设置:(两个方法)
方法1.在配置文件中加上
 <property name="hibernate.hbm2ddl.auto">create</property>
方法2: 
Configuration conf=new Configuration();
  conf.configure("/hibernate.cfg.xml");
  SchemaExport dbExport=new SchemaExport(conf);
  dbExport.create(true, true);

Hibernate4和Mysql5.1以上版本创建表出错 type=InnDB

需要修改配置:
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
修改为:
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

tools

安装Eclipse 的hibernate Tools 
help --> Eclipse Marketplace
Find --> Hibernate Tools
Install

web

request.getInputStream()

获取form 里post 的参数

spring

@RequestMapping(value = "/produces", produces = "application/json"):表示将功能处理方法将生产json格式的数据

consumes="application/json"

方法仅处理request Content-Type为“application/json”类型的请求。

produces="application/json"

方法仅处理request请求中Accept头中包含了"application/json"的请求,同时暗示了返回的内容类型为application/json;

文件上传
<!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver -->
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="UTF-8"
p:maxUploadSize="5400000"
p:uploadTempDir="fileUpload/temp"
>
</beans:bean>
<body>
<h2>文件上传实例</h2>

<form action="fileUpload.html" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="file">
<input type="submit" value="提交">
</form>

</body>

//通过Spring的autowired注解获取spring默认配置的request
@Autowired
private HttpServletRequest request;

/***
* 上传文件 用@RequestParam注解来指定表单上的file为MultipartFile
*
* @param file
* @return
*/
@RequestMapping("fileUpload")
public String fileUpload(@RequestParam("file") MultipartFile file) {
// 判断文件是否为空
if (!file.isEmpty()) {
try {
// 文件保存路径
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"
+ file.getOriginalFilename();
// 转存文件
file.transferTo(new File(filePath));
} catch (Exception e) {
e.printStackTrace();
}
}
// 重定向
return "redirect:/list.html";
}

/***
* 读取上传文件中得所有文件并返回
*
* @return
*/
@RequestMapping("list")
public ModelAndView list() {
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/";
ModelAndView mav = new ModelAndView("list");
File uploadDest = new File(filePath);
String[] fileNames = uploadDest.list();
for (int i = 0; i < fileNames.length; i++) {
//打印出文件名
System.out.println(fileNames[i]);
}
return mav;
}


Java

Method m = o.getClass().getDeclaredMethod(cmd, pe.getClass(),String.class);
m.setAccessible(true);
pe = (PhoneEntity) m.invoke(o, pe,json);

MYSQL

Error Code: 1175. You are using safe update

where 中使用非主键
解决方法 SET SQL_SAFE_UPDATES = 0;

前言

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: