好久没写这个web的代码了,再不写估计以前的也忘记的差不多了,赶紧复习下,顺便把自己的想法慢慢实现了。

  最近在公司发现以前的项目有用easyUI搭建的。赶紧copy回来学习下。

  感觉写日志真的很有必要,自己总结的肯定要比每次从网上查阅来的靠谱。每次搭建一个基本的web项目都都要查ssh的配置真的很忧伤啊,有木有!!!

  把jar包的截图截了一下,要不总是记不清该用到的基本jar有啥。

  

微服务做增删改查 增删改查功能实现_微服务做增删改查

  其中Struts中用到的jar有:commons-fileupload.jar  commons-io.jar  freemarker.jar  ognl.jar  struts2-core.jar  xwork-core.jar

  因为用到的是Struts的注解形式,所以加入了Struts的插件jar:struts2-convention-plugin.jar

  hibernate中用到的jar有:hibernate核心包:hibernate.jar  hibernate必须包:antlr.jar  commons-collection.jar dom4j.jar  javassist.jar  jta.jar  slf-4j.jar  数据库连接池:c3p0  数据库驱动jdbc:mysql-connector.jar 

  spring中用到的jar有:核心包:spring.jar  AOP:aspectjrt.jar  aspectjweaver.jar 动态代理(和aop也有关)cglib.jar  日志有关commons-logging.jar

  其他的插件包需要的时候一个一个添加,上面的是最基本的。

  好了,这回基本jar包也记录下来了。下面开始好好研究下公司项目,实现最基本的用户增删改查了。

  现在最麻烦的就是项目里的基础通用类不知道该怎么部署,啊啊啊啊啊!!!!

  

  马丹,把项目拷到公司电脑上之后包了个这个错误,我也是醉了好不,明明都是一样的MySQL和Eclipse,为啥就不行了呢。荒废了我一下午也知道错在哪了。网上说事MySQL版本太低了,或是dialect没写对,可是改了也是不行啊,真心不知道哪里错了,只能回家看看原先的数据库是设置的什么字符集了,或许是MyEclipse中的某些字符集设置导致的问题吧,啊啊啊蛋疼。。。

17:33:20,483  WARN JDBCExceptionReporter:233 - SQL Error: 0, SQLState: null
17:33:20,486 ERROR JDBCExceptionReporter:234 - Cannot create PoolableConnectionFactory (Unknown character set: 'utf8mb4')
17:33:20,488  WARN SettingsFactory:147 - Could not obtain connection to query metadata
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unknown character set: 'utf8mb4')
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
    at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:82)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:114)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:814)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:732)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4334)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
    at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:516)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown character set: 'utf8mb4'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618)
    at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1880)
    at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3499)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2384)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
    at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
    at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)
    ... 50 more
2015-6-15 17:33:22 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2015-6-15 17:33:22 org.apache.jk.common.ChannelSocket init
信息: Port busy 8009 java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind
2015-6-15 17:33:22 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8010
2015-6-15 17:33:22 org.apache.jk.server.JkMain start
信息: Jk running ID=1 time=0/11  config=null
2015-6-15 17:33:22 org.apache.catalina.startup.Catalina start
信息: Server startup in 4954 ms

 蛋疼的,回家这个项目根本没有数据库字符集的问题。先不管这个问题了。这章可是要写实现数据的增删改查啊。

项目中用到了easyUI前台框架,写一下后台action向前台返回jason格式的代码:

1 package cn.itcast.oa.action;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.apache.struts2.convention.annotation.Action;
 6 import org.apache.struts2.convention.annotation.Namespace;
 7 import org.apache.struts2.convention.annotation.ParentPackage;
 8 import org.apache.struts2.convention.annotation.Result;
 9 import org.apache.struts2.convention.annotation.Results;
10 
11 import cn.itcast.oa.base.BaseAction;
12 import cn.itcast.oa.domain.EasyGridData;
13 import cn.itcast.oa.domain.Role;
14 import cn.itcast.oa.service.RoleService;
15 
16 @Namespace("/jsp/role")
17 @ParentPackage("json-default")
18 @Action(value = "/roleAction")
19 @Results({
20     @Result(name = "success", type = "json",params = { "root", "gridData" })
21 })
22 public class RoleAction extends BaseAction<Role> {
23 
24     private static final long serialVersionUID = 1L;
25 
26     @Resource
27     private RoleService roleService;
28     
29     private EasyGridData<Role> gridData = new EasyGridData<Role>();
30     @Override
31     public String execute() throws Exception {
32 //         roleList = roleService.findAll();
33         gridData.setRows(roleService.findAllByPage(this.page,this.rows)) ;
34         gridData.setTotal( roleService.findAll().size());
35 //        ActionContext.getContext().put("roleList", roleList);
36         return "success";
37 
38     }
39 
40     public EasyGridData<Role> getGridData() {
41         return gridData;
42     }
43     public void setGridData(EasyGridData<Role> gridData) {
44         this.gridData = gridData;
45     }
46 
47 }

其中,封装了一个EasyGridData类,该类是为了实现easyUI数据表格分页。EasyGridData代码如下:

1 package cn.itcast.oa.domain;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 /**
 6  * 实现easyUI数据表格分页
 7  * @author haojiahong
 8  *
 9  * @param <T>
10  */
11 public class EasyGridData<T> {
12     private int total=0;
13     private List<T> rows = new ArrayList<T>();
14     private String msg;
15     public List<T> getRows() {
16         return rows;
17     }
18 
19     public void setRows(List<T> rows) {
20         this.rows = rows;
21     }
22 
23     
24     public int getTotal() {
25         return total;
26     }
27 
28     
29     public void setTotal(int total) {
30         this.total = total;
31     }
32 
33     
34     public String getMsg() {
35         return msg;
36     }
37 
38     
39     public void setMsg(String msg) {
40         this.msg = msg;
41     }
42 }

  为什么要封装一个这样的类呢,因为在一开始没有分页的情况时,我直接返回roleList,easyUI表格照样可以解析数据,将返回到前台的jason串显示出来。可是当加入分页后,返回到前台的数据串编程一个类似二维数组的东西,roleList里面的数据,easyUI显示不出来了,封装这个类之后吧List中的数据放到了rows中,easyUI又可以显示出来了,说明,在前台esayUI中,只能识别固定形式的json串。

微服务做增删改查 增删改查功能实现_bc_02

通过以上,实现了easyUI分页显示数据。实现了基本的查询功能。