各种工具类

MybatisUntil

public class MybatisUntil {

private static SqlSessionFactory sf = init();
public static SqlSessionFactory init(){
String resource = "mybatis.xml";
try {
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sFactory;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static SqlSessionFactory getsqlsessionfaFactory(){
return sf;
}
}

DateUtil转换

public class DateUtil {
public static Date utilDateToSqlDate(java.util.Date d){
Date date = new Date(d.getTime());
return date;
}
}

HibernateUtil

public class HibernateUtil {

private static final SessionFactory sf = BuidSessionFactory();
private static Configuration cfg;
private static SessionFactory BuidSessionFactory(){
cfg = new Configuration().configure("hibernate.cfg.xml");
ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
return cfg.buildSessionFactory(sr);
}
public static SessionFactory getSessionFactory(){
return sf;
}
public static void export(){
SchemaExport se = new SchemaExport(cfg);
se.create(true, true);
}

}

上传图片工具类

public class PhotoUtil {

//封装上传方法myfile
public static String copy(File photo,String photoFileName){

//生成新的文件名(使用uuid)
String newphotoFileName = UUIDUtil.getUUID()+photoFileName.substring(photoFileName.lastIndexOf("."));
//指定上传的位置
String path = ServletActionContext.getServletContext().getRealPath("upload");
//上传文件的位置
String filepath = path+File.separator+newphotoFileName;
//构建新文件
File newfile = new File(filepath);

//读入写出 从旧文件读内容到新文件
FileInputStream fis = null;
FileOutputStream fos = null;

try {
//将旧文件封装到输入流
fis = new FileInputStream(photo);
//将新文件封装到输出流
fos = new FileOutputStream(newfile);
//设置一个字节数组缓冲内容
byte [] bt = new byte[1024];
int len = 0;
/**
* 循环读取缓冲区的内容
* 输入流不断的将字节读入到缓冲区(旧文件到缓冲区)
* 输出流不断的将字节写出到新文件(缓冲区到新文件)
*/
while((len = fis.read(bt))!=-1){
fos.write(bt, 0, len);
}
fos.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newphotoFileName;
}

public static boolean delAllFile(String photoFileName) {
String path = ServletActionContext.getServletContext().getRealPath("upload");
String path1 = path+File.separator+photoFileName;
boolean flag = false;
File file = new File(path1);
if (!file.exists()) {
return flag;
}else if (file.exists()) {
file.delete();
}
return flag;

}
public void method(File f)
{
File[] FList = f.listFiles();
for (int i = 0; i < FList.length; i++)
{
if (FList[i].isFile()==true)
{
method(FList[i]);
}
else
{
System.out.println(FList[i].getAbsolutePath());
}
}
}
}

 

public class ToolsUtil {

//每页显示的记录数
public static final int NUM_PER_PAGE=5;
分页
public static String getPageSQL(String tableName,String orderedColumn){
return "select * from (select t2.*,rownum r from "
+ "(select * from "+tableName+" t1 order by "+orderedColumn+" desc) t2) t3 where r>? and r<=?";
}
//获取总页数
public static int getPageCount(int count,int NumPerPage){
return (count%NumPerPage==0)?(count/NumPerPage):(count/NumPerPage+1);
}

//获取总条数据
public static String PageCountSQL(String tableName){
return "select count(*) from "+tableName;
}
//检查字符串是否为空
public static boolean stringNotEmpty(String str){
if(str!=null&&!"".equals(str)&&str.length()>0&&!str.matches("\\s*")){
return true;
}else{
return false;
}
}

/*
* 字符串根据分隔符,转数组
*/
public static String[] stringToArr(String str,String split){
if(stringNotEmpty(str)){
return str.split(split);
}
return null;
}

/*
* 数组转根据分隔符,拼接字符串
*/
public static String arrToString(String[] arr,String split){
String str = "";
if(arr!=null){
for(String ele:arr){
str += ele + split;
}
str = str.substring(0, str.lastIndexOf('_'));
return str;
}
return null;
}
}

uuid

public class UUIDUtil {

public static String getUUID() {
return UUID.randomUUID().toString().replaceAll("-", "");
}

}

 

posted @ 2017-12-04 17:45  但为君故L  阅读(185)  评论(0编辑  收藏  举报