终于有一个功能是自己的思路,自己解决每一个问题,然后最终调试成功了。
一、确定需求
回学校了,手有点生疏了,但是,还是非常想找师兄讨论一下接下来的项目需求。将近用了快一个小时的时间,确定下来,这次核心功能是这样的:根据不同的条件来选择性的生成并发布webservice。也就是说可以通过界面操作的方式来选择webservice的返回值有哪些。再说的具体一点就是:比如,有三个表,分别存放了学生的个人资料表,学院表,系别表,用户界面中要求有这三个表的列名,用户通过操作选择服务端发布哪几个列的webservice。
二、确定思路
思路的整理是一个比较难搞定的问题。最初的最初,想的是生成两个java文件,然后改一个配置文件,所以,最初的问题就放在了获取数据库表的列名,这个问题,可以通过java中的反射机制来做。因此,就写了一个程序来获取其中某个表的列名,但是,小叶说,写代码要负责,经过他的开导,我把这个类抽象成了一个抽象类,幸亏听了小叶的,要不后面还要重写。接下来的主要问题就是判断,用户提交后,到底是要哪个表的哪个列,难住了……毕竟是三个表,其实,三个表还好,要是n多个表可是咋整?于是,思路被打断,开始思考……突然想到,为什么不在数据库中建立一个视图呢?于是,联系了一下师兄,得到师兄的允许,改为先建立视图,然后用hibernate操作视图。
现在有点清楚了,摆在面前的有这么几个问题:
1、在数据库中建立视图后,怎样用hibernate映射,和普通的表一样吗?而且,视图中没有主键的。
2、新建文件,主要是对文件的内容写入问题。
3、配置文件的修改,也就是要发布webservice--------用流的方式还是用xml解析
思路定下来了,然后就是解决这些问题。其实,最难解决的我觉得是第一个问题。
三、解决问题
1、视图映射问题
我们都知道,hibernate映射表的时候,如果没有主键,就会生成两个实体类。网上有些人说要在生成后改两个实体类,有的说不用改,直接用。我觉得,没有必要。具体的完成过程:
首先,说一下我的表的结构:
users表:usersid(主键)、username、password、depart(外键)
college表:colid(主键)、colname
department表:deid(主键)、dename、colid(外键)
(1)新建一个视图:
- create view usersinfor as select users.usersid as usersid,users.username as username,
- users.authority as authority,department.dename as department,
- college.colname as colname from users, department,college
- where users.depart=department.deid and department.colid=college.colid
(2)hibernate映射视图
就像普通的表那样,直接映射就可以了,但是,生成的内容是不一样的,因为视图中没有主键,不过不要紧,直接用就行了,唯一要注意的是写法有些不同,比如,要查询,应该这样写:
- "from Usersinfor U where U.id.usersid=?"
要获取这个类中属性内容的时候,写法也不一样:
- String username = new String();
- Usersinfor users = new Usersinfor();
- username = users.getId().getUsername();
2、新建文件并写入
首先,文件名,我想的是用产生随机数的方法来对文件进行命名;读写的时候,要根据提交的checkboxlist的值生成相应内容,当然,要用流的方式。
- package com.usersope.IO;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.util.List;
- import java.util.Random;
- import com.opensymphony.xwork2.ActionSupport;
- import com.usersope.IOUtil.FileOpe;
- import com.usersope.IOUtil.UpString;
- import com.usersope.IOUtil.XmlOpe;
- public class GetUrl extends ActionSupport {
- private String sql = new String();
- private List userslist;
- File fileImpl;
- File Ifile;
- Random rand;
- int i;
- String filenameImpl = new String();
- String filecontentImpl = new String();
- String Ifilename = new String();
- String Ifilecontent = new String();
- String servicename = new String();//配置文件中要用的
- String url = new String();
- public String execute(){
- //产生随机数
- rand =new Random();
- i=rand.nextInt(100);
- //要写入的字符串
- //找到文件,如果文件不存在,则新建.这里是新建文件
- filenameImpl = "Getmes"+i+"Impl";
- servicename = "Getmes"+i;
- Ifilename = "IGetmes"+i;
- fileImpl = new File("F://myProject//userservice//src//com//service//"+filenameImpl+".java");
- Ifile = new File("F://myProject//userservice//src//com//service//"+Ifilename+".java");
- while (fileImpl.exists()|| Ifile.exists()){
- //产生随机数
- i=rand.nextInt(100);
- filenameImpl = "Getmes"+i+"Impl";
- servicename = "Getmes"+i;
- fileImpl = new File("F://myProject//userservice//src//com//service//"+filenameImpl+".java");
- Ifile = new File("F://myProject//userservice//src//com//service//"+Ifilename+".java");
- }
- FileOpe.createFiles(fileImpl);//用到了上面的静态方法
- FileOpe.createFiles(Ifile);
- //以上是建立两个文件
- //下面是写文件
- //接口里面的内容
- Ifilecontent = "package com.service;";
- Ifilecontent = Ifilecontent + "public interface " + Ifilename;
- Ifilecontent = Ifilecontent + "{public String getmes(int userid);}";
- //接口实现里面的内容
- File filemedo1 = new File("F://myProject//userservice//src//com//service//ok1.txt");
- File filemedo2 = new File("F://myProject//userservice//src//com//service//ok2.txt");
- try {
- FileInputStream fis1 = new FileInputStream(filemedo1);
- String s1 = FileOpe.readFileInputStr(fis1);//读取数据,并将读取到的数据存储到数组中
- filecontentImpl = filecontentImpl + s1;
- filecontentImpl = filecontentImpl + " " + filenameImpl +" implements "+Ifilename;
- FileInputStream fis2 = new FileInputStream(filemedo2);
- String s2 = FileOpe.readFileInputStr(fis2);//读取数据,并将读取到的数据存储到数组中
- filecontentImpl = filecontentImpl + s2;
- } catch (FileNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } //创建流对象
- //将字符串的第一个字母变成大写
- for (int i = 0;i<userslist.size();i++){
- String str = (String)userslist.get(i);
- char[] aa = UpString.upcase(str);
- String bb = new String(aa);//字符数组转换为string类型
- filecontentImpl = filecontentImpl + "idmes = idmes +"+"/""+"<" + str + ">"+"/"";
- filecontentImpl = filecontentImpl + "+users.getId().get";
- filecontentImpl = filecontentImpl + bb+"()+";
- filecontentImpl = filecontentImpl +"/""+ "</" + str +">"+"/";"+" ";
- }
- filecontentImpl = filecontentImpl + "idmes = idmes + "+"/""+"</user>"+"/""+";"+" "+"return idmes;}}";
- FileOutputStream fos = null;
- try{
- fos = new FileOutputStream(fileImpl,true);//为true的时候是追加,默认是覆盖
- FileOpe.writeString(filecontentImpl,fos);
- fos = new FileOutputStream(Ifile,true);//为true的时候是追加,默认是覆盖
- FileOpe.writeString(Ifilecontent,fos);
- } catch (FileNotFoundException e){
- }finally{
- try{
- fos.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- XmlOpe.add(servicename);
- //转到显示url的页面
- url = "http://localhost:8080/userservice/services/"+servicename+"?wsdl";
- return "url";
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public List getUserslist() {
- return userslist;
- }
- public void setUserslist(List userslist) {
- this.userslist = userslist;
- }
- }
其中,涉及到的问题有:将一个字符串的首写字母变为大写:
- public class UpString {
- public static char[] upcase(String str) {
- char[] aa = str.toCharArray();
- if(aa[0] >96 && aa[0] < 123)
- aa[0]=(char) (aa[0]-32);//小写换大写
- //System.out.print(aa);
- return aa;
- }
- }
文件的操作等。
3、配置文件的修改,即services.xml文件的修改
征求了小叶的意见,用解析的方式进行,这样也明了。只不过其中只要传递一个参数就ok了。
- import javax.xml.parsers.DocumentBuilderFactory;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import org.w3c.dom.*;
- import org.xml.sax.SAXException;
- import javax.xml.parsers.*;
- import javax.xml.transform.*;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.*;
- import javax.xml.xpath.*;
- public class XmlOpe {
- //参数就是生成的文件名
- public static void add(String servicename){
- DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
- Element service=null;
- Element theElem=null;
- Element root=null ;
- try {
- factory.setIgnoringElementContentWhitespace(true);
- DocumentBuilder db=factory.newDocumentBuilder();
- Document xmldoc=db.parse(new File("F://myProject//userservice//WebServices//services.xml"));
- root=xmldoc.getDocumentElement();
- //--- 新发布一个web service ----
- service=xmldoc.createElement("service");
- theElem=xmldoc.createElement("name");
- theElem.setTextContent(servicename);
- service.appendChild(theElem);
- theElem=xmldoc.createElement("serviceClass");
- theElem.setTextContent("com.service."+"I"+servicename);
- service.appendChild(theElem);
- theElem=xmldoc.createElement("implementationClass");
- theElem.setTextContent("com.service."+servicename+"Impl");
- service.appendChild(theElem);
- theElem=xmldoc.createElement("style");
- theElem.setTextContent("wrapped");
- service.appendChild(theElem);
- theElem=xmldoc.createElement("use");
- theElem.setTextContent("literal");
- service.appendChild(theElem);
- theElem=xmldoc.createElement("scope");
- theElem.setTextContent("application");
- service.appendChild(theElem);
- System.out.print(1);
- root.appendChild(service);
- saveXml("F://myProject//userservice//WebServices//services.xml", xmldoc);
- }catch (ParserConfigurationException e){
- e.printStackTrace();
- } catch (SAXException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //将Document输出到文件
- public static void saveXml(String fileName, Document doc) {
- TransformerFactory transFactory=TransformerFactory.newInstance();
- try {
- Transformer transformer = transFactory.newTransformer();
- transformer.setOutputProperty("indent", "yes");
- DOMSource source=new DOMSource();
- source.setNode(doc);
- StreamResult result=new StreamResult();
- result.setOutputStream(new FileOutputStream(fileName));
- transformer.transform(source, result);
- } catch (TransformerConfigurationException e){
- e.printStackTrace();
- } catch (TransformerException e) {
- e.printStackTrace();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
四、调试并发现问题
调试也花了一些时间,最终还是调试成功了,但是,其中有一个问题又出现了:每次生成webservice,都必须要求服务器重新启动才能生效。小叶曾经说过可以用监听器,不过,我没有试过,不知道行不行。暂时这个先做到这吧。
大家见笑了……