├── .classpath
├── .gitignore
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── LICENSE
├── README.md
├── WEB-INF
├── classes
│ ├── .gitignore
│ └── log4j.xml
├── lib
│ ├── asm-3.3.1.jar
│ ├── cglib-2.2.2.jar
│ ├── commons-lang-2.6.jar
│ ├── commons-logging-1.1.1.jar
│ ├── ibatis-2.3.4.726.jar
│ ├── javassist-3.11.0.GA.jar
│ ├── jcl-over-slf4j-1.7.7.jar
│ ├── jzlib-1.1.3.jar
│ ├── log4j-1.2.16.jar
│ ├── mina-core-2.0.9.jar
│ ├── mysql-connector-java-5.0.6-bin.jar
│ ├── slf4j-api-1.7.7.jar
│ └── slf4j-log4j12-1.6.2.jar
└── web.xml
├── build.xml
├── ibatis-generator-config
└── roledata
│ └── t_user.xml
├── resources
├── ibatis-gamedata-config.xml
├── ibatis-roledata-config.xml
└── log4j.xml
├── src
├── com
│ └── dyz
│ │ ├── gameserver
│ │ ├── bootstrap
│ │ │ ├── GameServer.java
│ │ │ └── GamserServerStartupAndStop.java
│ │ ├── commons
│ │ │ ├── message
│ │ │ │ ├── ClientRequest.java
│ │ │ │ ├── MsgBodyWrap.java
│ │ │ │ ├── MsgDispatcher.java
│ │ │ │ ├── ResponseMsg.java
│ │ │ │ └── ServerResponse.java
│ │ │ ├── session
│ │ │ │ └── GameSession.java
│ │ │ └── tool
│ │ │ │ └── ServerThreadFactory.java
│ │ ├── context
│ │ │ ├── ExecutorServiceManager.java
│ │ │ └── GameServerContext.java
│ │ ├── msg
│ │ │ ├── processor
│ │ │ │ ├── common
│ │ │ │ │ ├── CharacterMsgProcessor.java
│ │ │ │ │ ├── INotAuthProcessor.java
│ │ │ │ │ ├── MsgProcessor.java
│ │ │ │ │ └── MsgProcessorRegister.java
│ │ │ │ └── login
│ │ │ │ │ ├── LoginMsgProcessor1003.java
│ │ │ │ │ ├── OpenAppMsgProcessor1001.java
│ │ │ │ │ └── SignUpMsgProcessor1005.java
│ │ │ └── response
│ │ │ │ └── login
│ │ │ │ ├── LoginResponse1004.java
│ │ │ │ ├── OpenAppResponse1002.java
│ │ │ │ └── SignUpResponse1006.java
│ │ ├── net
│ │ │ ├── MinaMsgHandler.java
│ │ │ ├── NetManager.java
│ │ │ └── codec
│ │ │ │ ├── GameMsgDecoder.java
│ │ │ │ ├── GameMsgEncoder.java
│ │ │ │ ├── GameProtocolcodecFactory.java
│ │ │ │ └── MsgProtocol.java
│ │ └── sprite
│ │ │ ├── Character.java
│ │ │ ├── base
│ │ │ └── GameObj.java
│ │ │ └── tool
│ │ │ └── AsyncTaskQueue.java
│ │ └── persist
│ │ ├── gamedata
│ │ └── common
│ │ │ └── IGameDataCach.java
│ │ ├── roledata
│ │ └── user
│ │ │ ├── User.java
│ │ │ ├── UserDAO.java
│ │ │ ├── UserDAOImpl.java
│ │ │ ├── UserExample.java
│ │ │ └── UserService.java
│ │ └── util
│ │ └── DBUtil.java
└── dbsqlmapper
│ └── roledata
│ └── t_user_SqlMap.xml
└── 消息协议.xls
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Mobile Tools for Java (J2ME)
4 | .mtj.tmp/
5 |
6 | # Package Files #
7 | *.war
8 | *.ear
9 | /.classpath
10 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
11 | hs_err_pid*
12 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | GameServer
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6 | org.eclipse.jdt.core.compiler.compliance=1.6
7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12 | org.eclipse.jdt.core.compiler.source=1.6
13 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # GameServer
2 | 一个服务器处理框架,包括 协议处理,消息处理,持久层数据处理
3 |
4 | 目录结构介绍:
5 | src ---源程序目录
6 | |-dbsqlmapper ---mybaits sqlmap文件目录
7 | resource ---源目录,存放配置文件
8 | ibatis-generator-config ---ibatis-generator的配置文件
9 | Tomcat-config ---tomcat的配置文件,配置了context以及数据源
10 | WEB-INF ---项目的部署目录
11 |
12 | 架构思路:
13 | 使用Tomcat做启动
14 | 使用tomcat提供的数据源
15 | 使用tomcat提供的热加载机制
16 | 后期方便添加web界面功能,来对服务器的管理
17 | 使用ibatis做持久层
18 | 采用ibator做自动代码生成工具,减少开发中对数据库编码的工作量
19 | 使用mina2.0.9做网络通信
20 | 提供网络通信的性能和稳定
--------------------------------------------------------------------------------
/WEB-INF/classes/.gitignore:
--------------------------------------------------------------------------------
1 | /com/
2 | /dbsqlmapper/
3 | /ibatis-gamedata-config.xml
4 | /ibatis-roledata-config.xml
5 | /log4j.xml
6 |
--------------------------------------------------------------------------------
/WEB-INF/classes/log4j.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
33 |
37 |
--------------------------------------------------------------------------------
/WEB-INF/lib/asm-3.3.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/asm-3.3.1.jar
--------------------------------------------------------------------------------
/WEB-INF/lib/cglib-2.2.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/cglib-2.2.2.jar
--------------------------------------------------------------------------------
/WEB-INF/lib/commons-lang-2.6.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/commons-lang-2.6.jar
--------------------------------------------------------------------------------
/WEB-INF/lib/commons-logging-1.1.1.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/commons-logging-1.1.1.jar
--------------------------------------------------------------------------------
/WEB-INF/lib/ibatis-2.3.4.726.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/ibatis-2.3.4.726.jar
--------------------------------------------------------------------------------
/WEB-INF/lib/javassist-3.11.0.GA.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/javassist-3.11.0.GA.jar
--------------------------------------------------------------------------------
/WEB-INF/lib/jcl-over-slf4j-1.7.7.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/jcl-over-slf4j-1.7.7.jar
--------------------------------------------------------------------------------
/WEB-INF/lib/jzlib-1.1.3.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/jzlib-1.1.3.jar
--------------------------------------------------------------------------------
/WEB-INF/lib/log4j-1.2.16.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/log4j-1.2.16.jar
--------------------------------------------------------------------------------
/WEB-INF/lib/mina-core-2.0.9.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/mina-core-2.0.9.jar
--------------------------------------------------------------------------------
/WEB-INF/lib/mysql-connector-java-5.0.6-bin.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/mysql-connector-java-5.0.6-bin.jar
--------------------------------------------------------------------------------
/WEB-INF/lib/slf4j-api-1.7.7.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/slf4j-api-1.7.7.jar
--------------------------------------------------------------------------------
/WEB-INF/lib/slf4j-log4j12-1.6.2.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/WEB-INF/lib/slf4j-log4j12-1.6.2.jar
--------------------------------------------------------------------------------
/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 | com.dyz.gameserver.bootstrap.GamserServerStartupAndStop
9 |
10 |
11 | index.html
12 | index.htm
13 | index.jsp
14 |
15 |
--------------------------------------------------------------------------------
/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/ibatis-generator-config/roledata/t_user.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
10 |
11 |
12 |
14 |
16 |
18 |
21 |
22 |
--------------------------------------------------------------------------------
/resources/ibatis-gamedata-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/resources/ibatis-roledata-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/resources/log4j.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
33 |
37 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/bootstrap/GameServer.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.bootstrap;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.dyz.gameserver.commons.message.MsgDispatcher;
7 | import com.dyz.gameserver.context.ExecutorServiceManager;
8 | import com.dyz.gameserver.net.MinaMsgHandler;
9 | import com.dyz.gameserver.net.NetManager;
10 | import com.dyz.persist.util.DBUtil;
11 |
12 | public class GameServer {
13 |
14 | private static final Logger logger = LoggerFactory.getLogger(GameServer.class);
15 |
16 | private int port = 1101;
17 |
18 | private static GameServer instance=new GameServer();
19 |
20 | public static MsgDispatcher msgDispatcher = new MsgDispatcher();;
21 |
22 | private NetManager netManager;
23 |
24 | private GameServer(){
25 | netManager = new NetManager();
26 | }
27 |
28 | public static GameServer getInstance(){
29 | return instance;
30 | }
31 |
32 | public void startUp(){
33 | try {
34 | logger.info("开始启动服务器 ...");
35 | ExecutorServiceManager.getInstance().initExecutorService();
36 | logger.info("初始化服务器线程池完成");
37 | DBUtil.initAllSqlMapClient();
38 | logger.info("数据库连接初始化完成");
39 | netManager.startListner(new MinaMsgHandler(), port);
40 | logger.info("服务器监听端口:{}完成",port);
41 | logger.info("game server started...");
42 | } catch (Exception e) {
43 | logger.error("服务器启动失败");
44 | e.printStackTrace();
45 | }
46 |
47 | }
48 |
49 | public void stop() {
50 | try {
51 | logger.info("准备关闭服务器...");
52 | netManager.stop();
53 | logger.info("服务器停止网络监听");
54 | ExecutorServiceManager.getInstance().stop();
55 | logger.info("服务器线程池关闭完成");
56 | logger.info("服务器关闭完成");
57 | } catch (Exception e) {
58 | logger.info("服务器关闭异常");
59 | e.printStackTrace();
60 | }
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/bootstrap/GamserServerStartupAndStop.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.bootstrap;
2 |
3 | import javax.servlet.ServletContextEvent;
4 | import javax.servlet.ServletContextListener;
5 |
6 | public class GamserServerStartupAndStop implements ServletContextListener{
7 |
8 | @Override
9 | public void contextDestroyed(ServletContextEvent arg0) {
10 |
11 | GameServer gameServer = (GameServer) arg0.getServletContext().getAttribute("gameserver");
12 | gameServer.stop();
13 | }
14 |
15 | @Override
16 | public void contextInitialized(ServletContextEvent arg0) {
17 | GameServer gameServer = GameServer.getInstance();
18 | arg0.getServletContext().setAttribute("gameserver", gameServer);
19 | gameServer.startUp();
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/commons/message/ClientRequest.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.commons.message;
2 |
3 | import java.io.IOException;
4 |
5 | /**
6 | * 使用byte数组作为缓冲区,使用{@link MsgBodyWrap}将byte数组转成合适的数据。
7 | *
8 | * @author dyz
9 | *
10 | */
11 | public class ClientRequest{
12 | MsgBodyWrap msgBody = null;
13 | int msgcode;
14 |
15 | public ClientRequest(byte[] array) throws IllegalArgumentException {
16 | if (array == null) {
17 | throw new IllegalArgumentException("消息缓冲区对象为null");
18 | }
19 | if (array.length == 0) {
20 | throw new IllegalArgumentException("消息缓冲区对象大小为0");
21 | }
22 | msgBody = MsgBodyWrap.newInstance4In(array);
23 | try {
24 | msgcode = msgBody.readInt();
25 | } catch (IOException e) {
26 | }
27 | }
28 |
29 | public int getMsgCode() {
30 | return msgcode;
31 | }
32 |
33 | public byte getByte() throws IOException {
34 | return msgBody.readByte();
35 | }
36 |
37 | public short getShort() throws IOException {
38 | return msgBody.readShort();
39 | }
40 |
41 | public int getInt() throws IOException {
42 | return msgBody.readInt();
43 | }
44 |
45 | public long getLong() throws IOException {
46 | return msgBody.readLong();
47 | }
48 |
49 | public float getFloat() throws IOException {
50 | return msgBody.readFloat();
51 | }
52 |
53 | public double getDouble() throws IOException {
54 | return msgBody.readDouble();
55 | }
56 |
57 | public String getString() throws IOException {
58 | return msgBody.readUTF();
59 | }
60 |
61 | public void relese(){
62 | if(msgBody!=null){
63 | msgBody.close();
64 | }
65 | msgBody=null;
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/commons/message/MsgBodyWrap.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.commons.message;
2 |
3 | import java.io.ByteArrayInputStream;
4 | import java.io.ByteArrayOutputStream;
5 | import java.io.DataInputStream;
6 | import java.io.DataOutputStream;
7 | import java.io.IOException;
8 | import java.io.ObjectInputStream;
9 | import java.io.ObjectOutputStream;
10 |
11 | /**
12 | * 封装了消息体(内容,不包括消息号),用于封装从客户端读取到的数据,或者发送到客户端的数据
13 | *
14 | * @author dyz
15 | *
16 | */
17 | public class MsgBodyWrap {
18 | private ByteArrayInputStream in;
19 | private ByteArrayOutputStream out;
20 |
21 | private DataInputStream dataIn;
22 | private DataOutputStream dataOut;
23 |
24 | public static MsgBodyWrap newInstance4In(byte[] buffer) {
25 | return new MsgBodyWrap(buffer);
26 |
27 | }
28 |
29 |
30 | public static MsgBodyWrap newInstance4Out() {
31 | return new MsgBodyWrap();
32 | }
33 |
34 | private MsgBodyWrap(byte[] buffer) {
35 | in = new ByteArrayInputStream(buffer);
36 | dataIn = new DataInputStream(in);
37 | }
38 |
39 | private MsgBodyWrap() {
40 | out = new ByteArrayOutputStream();
41 | dataOut = new DataOutputStream(out);
42 | }
43 |
44 | public byte readByte() throws IOException {
45 | return dataIn.readByte();
46 | }
47 |
48 | public int read() throws IOException {
49 | return dataIn.read();
50 | }
51 |
52 | public short readShort() throws IOException {
53 | return dataIn.readShort();
54 | }
55 |
56 | public int readInt() throws IOException {
57 | return dataIn.readInt();
58 | }
59 |
60 | public long readLong() throws IOException {
61 | return dataIn.readLong();
62 | }
63 |
64 | public float readFloat() throws IOException {
65 | return dataIn.readFloat();
66 | }
67 |
68 | public double readDouble() throws IOException {
69 | return dataIn.readDouble();
70 | }
71 |
72 | public String readUTF() throws IOException {
73 | return dataIn.readUTF();
74 | }
75 |
76 | public void writeByte(int value) throws IOException {
77 | dataOut.writeByte(value);
78 | }
79 |
80 | public void writeBoolean(boolean value) throws IOException {
81 | dataOut.writeBoolean(value);
82 | }
83 |
84 | public void writeBytes(byte[] value) throws IOException {
85 | dataOut.write(value);
86 | }
87 |
88 | public void writeShort(int value) throws IOException {
89 | dataOut.writeShort(value);
90 | }
91 |
92 | public void writeInt(int value) throws IOException {
93 | dataOut.writeInt(value);
94 | }
95 |
96 | public void writeLong(long value) throws IOException {
97 | dataOut.writeLong(value);
98 | }
99 |
100 | public void writeFloat(float value) throws IOException {
101 | dataOut.writeFloat(value);
102 | }
103 |
104 | public void writeDouble(double value) throws IOException {
105 | dataOut.writeDouble(value);
106 | }
107 |
108 | public void writeUTF(String value) throws IOException {
109 | dataOut.writeUTF(value);
110 | }
111 |
112 | public byte[] toByteArray() {
113 | return this.out.toByteArray();
114 | }
115 |
116 | /**
117 | * 关闭缓冲读写数据流
118 | */
119 | public void close() {
120 | if (in != null) {
121 | try {
122 | in.close();
123 | } catch (IOException e) {
124 | e.printStackTrace();
125 | }
126 | }
127 | if (out != null) {
128 | try {
129 | out.close();
130 | } catch (IOException e) {
131 | e.printStackTrace();
132 | }
133 | }
134 | if (dataIn != null) {
135 | try {
136 | dataIn.close();
137 | } catch (IOException e) {
138 | e.printStackTrace();
139 | }
140 | }
141 | if (dataOut != null) {
142 | try {
143 | dataOut.close();
144 | } catch (IOException e) {
145 | e.printStackTrace();
146 | }
147 | }
148 | }
149 |
150 |
151 | }
152 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/commons/message/MsgDispatcher.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.commons.message;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 |
9 | import com.dyz.gameserver.commons.session.GameSession;
10 | import com.dyz.gameserver.msg.processor.common.INotAuthProcessor;
11 | import com.dyz.gameserver.msg.processor.common.MsgProcessor;
12 | import com.dyz.gameserver.msg.processor.common.MsgProcessorRegister;
13 |
14 | /**
15 | * 消息分发器,根据消息号,找到相应的消息处理器
16 | * @author dyz
17 | *
18 | */
19 | public class MsgDispatcher {
20 |
21 | private static final Logger logger = LoggerFactory.getLogger(MsgDispatcher.class);
22 |
23 | private Map processorsMap = new HashMap();
24 |
25 | public MsgDispatcher(){
26 | for(MsgProcessorRegister register :MsgProcessorRegister.values()){
27 | processorsMap.put(register.getMsgCode(), register.getMsgProcessor());
28 | }
29 | logger.info("初始化 消息处理器成功。。。");
30 | }
31 |
32 | public MsgProcessor getMsgProcessor(int msgCode){
33 | return processorsMap.get(msgCode);
34 | }
35 |
36 | public void dispatchMsg( GameSession gameSession,ClientRequest clientRequest) {
37 |
38 | int msgCode = clientRequest.getMsgCode();
39 | if(msgCode == 1000){//客户端请求断开链接
40 | gameSession.close();
41 | }
42 | if(msgCode%2==0){//请求协议号必须是奇数
43 | return;
44 | }
45 | MsgProcessor processor = getMsgProcessor(msgCode);
46 | if(gameSession.isLogin() || processor instanceof INotAuthProcessor){
47 | processor.handle(gameSession, clientRequest);
48 | }
49 |
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/commons/message/ResponseMsg.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.commons.message;
2 |
3 | import org.apache.mina.core.buffer.IoBuffer;
4 |
5 | /**
6 | * 此接口声明了响应消息编码函数,服务端生产的每个响应消息都必须实现此接口。
7 | * 这里声明的函数在protocolEncoder中会被自动调用,编码成byte stream发送到客户端。
8 | *
9 | * @author dyz
10 | *
11 | */
12 | public interface ResponseMsg {
13 |
14 | /**
15 | * 设置消息号
16 | * @param code
17 | */
18 | public void setMsgCode(int code);
19 |
20 | /**
21 | * 返回消息的整体封包
22 | * @return
23 | */
24 | public IoBuffer entireMsg();
25 |
26 | /**
27 | * 释放资源(数据流、对象引用)
28 | */
29 | public void release();
30 | }
31 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/commons/message/ServerResponse.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.commons.message;
2 |
3 | import org.apache.mina.core.buffer.IoBuffer;
4 |
5 | import com.dyz.gameserver.net.codec.MsgProtocol;
6 |
7 | /**
8 | * 服务端发给客户端的消息。 所有返回给客户端的消息都最好继承于它.
9 | * 这里封装了基本的输出字节操作。
10 | *
11 | * @author dyz
12 | *
13 | */
14 | public class ServerResponse implements ResponseMsg {
15 | protected MsgBodyWrap output = MsgBodyWrap.newInstance4Out();
16 | private int msgCode;
17 | /**必须调用此方法设置消息号*/
18 | public ServerResponse(int msgCode) {
19 | setMsgCode(msgCode);
20 | }
21 |
22 | public void setMsgCode(int code) {
23 | msgCode = code;
24 | }
25 |
26 | public IoBuffer entireMsg() {
27 |
28 | byte[] body = output.toByteArray();
29 | /* 标志 byte 长度short */
30 | int length = MsgProtocol.flagSize+MsgProtocol.lengthSize+MsgProtocol.msgCodeSize+ body.length;
31 | IoBuffer buf = IoBuffer.allocate(length);
32 | buf.put(MsgProtocol.defaultFlag);//flag
33 | buf.putInt(body.length+MsgProtocol.msgCodeSize);//lengh
34 | buf.putInt(msgCode);
35 | buf.put(body);
36 | buf.flip();
37 | return buf;
38 | }
39 |
40 | /**
41 | * 释放资源(数据流、对象引用)
42 | */
43 | public void release() {
44 | if (output != null) {
45 | output.close();
46 | }
47 | output = null;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/commons/session/GameSession.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.commons.session;
2 |
3 | import java.net.InetSocketAddress;
4 | import java.net.SocketAddress;
5 |
6 | import org.apache.mina.core.future.WriteFuture;
7 | import org.apache.mina.core.session.AttributeKey;
8 | import org.apache.mina.core.session.IoSession;
9 |
10 | import com.dyz.gameserver.commons.message.ResponseMsg;
11 | /**
12 | * 游戏中的session回话,封装了mina的session
13 | * @author dyz
14 | *
15 | */
16 | public class GameSession {
17 |
18 | private IoSession session;
19 | private String address;
20 |
21 | private Object role;
22 |
23 | private boolean isLogin=false;
24 |
25 | private static final AttributeKey KEY_PLAYER_SESSION = new AttributeKey(GameSession.class, "player.session");
26 |
27 | public GameSession(IoSession session){
28 | this.session = session;
29 | this.session.setAttribute(KEY_PLAYER_SESSION, this);
30 | SocketAddress socketaddress = session.getRemoteAddress();
31 | InetSocketAddress s = (InetSocketAddress) socketaddress;
32 | address = s.getAddress().getHostAddress();
33 | }
34 |
35 | public static GameSession getInstance(IoSession session) {
36 | Object playerObj = session.getAttribute(KEY_PLAYER_SESSION);
37 | return (GameSession) playerObj;
38 | }
39 |
40 | /**
41 | * 发送消息给客户端
42 | * @param msg
43 | * @return
44 | */
45 | public WriteFuture sendMsg(ResponseMsg msg) {
46 | if (session == null || !session.isConnected() || session.isClosing()) {
47 | return null;
48 | }
49 | return session.write(msg);
50 | }
51 |
52 | public String getAddress(){
53 | return this.address;
54 | }
55 |
56 | public void setLogin(boolean isLogin){
57 | this.isLogin=isLogin;
58 | }
59 | public boolean isLogin(){
60 | return this.isLogin;
61 | }
62 |
63 | public void setRole(Object obj){
64 | this.role = obj;
65 | }
66 |
67 | @SuppressWarnings("unchecked")
68 | public T getRole(Class t){
69 | return (T)this.role;
70 | }
71 |
72 | public void close(){
73 | session.close(false);
74 | }
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/commons/tool/ServerThreadFactory.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.commons.tool;
2 |
3 | import java.util.concurrent.ThreadFactory;
4 | import java.util.concurrent.atomic.AtomicInteger;
5 |
6 | public class ServerThreadFactory implements ThreadFactory {
7 | final ThreadGroup group;
8 | final AtomicInteger threadNumber = new AtomicInteger(1);
9 | final String namePrefix;
10 |
11 | public ServerThreadFactory(String name) {
12 | SecurityManager s = System.getSecurityManager();
13 | group = (s != null) ? s.getThreadGroup() : Thread.currentThread()
14 | .getThreadGroup();
15 | namePrefix = name + "-thread-";
16 | }
17 |
18 | public Thread newThread(Runnable r) {
19 | Thread t = new Thread(group, r, namePrefix
20 | + threadNumber.getAndIncrement(), 0);
21 | if (t.isDaemon())
22 | t.setDaemon(false);
23 | if (t.getPriority() != Thread.NORM_PRIORITY)
24 | t.setPriority(Thread.NORM_PRIORITY);
25 | return t;
26 | }
27 | }
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/context/ExecutorServiceManager.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.context;
2 |
3 | import java.util.concurrent.Executors;
4 | import java.util.concurrent.ScheduledThreadPoolExecutor;
5 | import java.util.concurrent.TimeUnit;
6 |
7 | import com.dyz.gameserver.commons.tool.ServerThreadFactory;
8 | /**
9 | * 整个服的线程池工具类
10 | *
11 | * @author daiyongzhi
12 | * @date 2015年1月30日 下午5:37:16
13 | * @version V1.0
14 | */
15 | public class ExecutorServiceManager {
16 |
17 | private static ExecutorServiceManager serviceManager = new ExecutorServiceManager();
18 | private ExecutorServiceManager(){}
19 | public static ExecutorServiceManager getInstance(){
20 | return serviceManager;
21 | }
22 | /**数据库操作使用的线程池*/
23 | private ScheduledThreadPoolExecutor executorServiceForDB;
24 |
25 | public void initExecutorService(){
26 | executorServiceForDB = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(10,new ServerThreadFactory("ExecutorServiceForDB"));
27 | }
28 |
29 | public ScheduledThreadPoolExecutor getExecutorServiceForDB(){
30 | return executorServiceForDB;
31 | }
32 |
33 | public void stop() throws InterruptedException{
34 | executorServiceForDB.shutdown();
35 | executorServiceForDB.awaitTermination(1, TimeUnit.SECONDS);
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/context/GameServerContext.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.context;
2 |
3 | import java.util.Map;
4 | import java.util.concurrent.ConcurrentHashMap;
5 |
6 | import com.dyz.gameserver.sprite.Character;
7 |
8 | /**
9 | * 管理整个服上的玩家
10 | *
11 | * @author daiyongzhi
12 | * @date 2015年1月30日 下午5:36:54
13 | * @version V1.0
14 | */
15 | public class GameServerContext {
16 |
17 | private static Map ALL_ONLINE_PLAYER = new ConcurrentHashMap();
18 |
19 | public static void addCharacter(Character character){
20 | ALL_ONLINE_PLAYER.put(character.getUserId(), character);
21 | }
22 |
23 | public static void removeCharacter(Character character){
24 | ALL_ONLINE_PLAYER.remove(character.getUserId());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/msg/processor/common/CharacterMsgProcessor.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.msg.processor.common;
2 |
3 | import com.dyz.gameserver.commons.message.ClientRequest;
4 | import com.dyz.gameserver.commons.session.GameSession;
5 |
6 | public abstract class CharacterMsgProcessor extends MsgProcessor {
7 |
8 |
9 | @Override
10 | public void process(GameSession gameSession,ClientRequest request) throws Exception {
11 |
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/msg/processor/common/INotAuthProcessor.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.msg.processor.common;
2 | /**
3 | * 标志接口,实现此处理器的processor不需要验证
4 | * @author dyz
5 | *
6 | */
7 | public interface INotAuthProcessor {
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/msg/processor/common/MsgProcessor.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.msg.processor.common;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.dyz.gameserver.commons.message.ClientRequest;
7 | import com.dyz.gameserver.commons.session.GameSession;
8 |
9 | public abstract class MsgProcessor {
10 |
11 | private static final Logger logger = LoggerFactory.getLogger(MsgProcessor.class);
12 |
13 | public void handle(GameSession gameSession,ClientRequest request){
14 | try {
15 | process(gameSession,request);
16 | } catch (Exception e) {
17 | logger.error("消息处理出错,msg code:"+request.getMsgCode());
18 | e.printStackTrace();
19 | }
20 | }
21 |
22 | public abstract void process(GameSession gameSession,ClientRequest request)throws Exception;
23 | }
24 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/msg/processor/common/MsgProcessorRegister.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.msg.processor.common;
2 |
3 | import com.dyz.gameserver.msg.processor.login.LoginMsgProcessor1003;
4 | import com.dyz.gameserver.msg.processor.login.OpenAppMsgProcessor1001;
5 | import com.dyz.gameserver.msg.processor.login.SignUpMsgProcessor1005;
6 |
7 |
8 | /**
9 | * 消息处理器注册类,所有的消息处理器,都在此注册实例化
10 | * @author dyz
11 | *
12 | */
13 | public enum MsgProcessorRegister {
14 | /**用户打开app*/
15 | openApp(1001,new OpenAppMsgProcessor1001()),
16 | /**登陆处理器*/
17 | login(1003,new LoginMsgProcessor1003()),
18 | /**用户注册处理器*/
19 | signUp(1005,new SignUpMsgProcessor1005());
20 |
21 |
22 | private int msgCode;
23 | private MsgProcessor processor;
24 | private MsgProcessorRegister(int msgCode,MsgProcessor processor){
25 | this.msgCode = msgCode;
26 | this.processor = processor;
27 | }
28 |
29 | public int getMsgCode(){
30 | return this.msgCode;
31 | }
32 |
33 | public MsgProcessor getMsgProcessor(){
34 | return this.processor;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/msg/processor/login/LoginMsgProcessor1003.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.msg.processor.login;
2 |
3 | import com.dyz.gameserver.commons.message.ClientRequest;
4 | import com.dyz.gameserver.commons.session.GameSession;
5 | import com.dyz.gameserver.context.GameServerContext;
6 | import com.dyz.gameserver.msg.processor.common.INotAuthProcessor;
7 | import com.dyz.gameserver.msg.processor.common.MsgProcessor;
8 | import com.dyz.gameserver.msg.response.login.LoginResponse1004;
9 | import com.dyz.persist.roledata.user.User;
10 | import com.dyz.persist.roledata.user.UserService;
11 | import com.dyz.gameserver.sprite.Character;
12 |
13 | public class LoginMsgProcessor1003 extends MsgProcessor implements INotAuthProcessor{
14 |
15 | @Override
16 | public void process(GameSession gameSession, ClientRequest request)
17 | throws Exception {
18 | String phone = request.getString();
19 | String passwd = request.getString();
20 |
21 | User user = UserService.getInstance().selectUser(phone, passwd);
22 | if(user==null){
23 | gameSession.sendMsg(new LoginResponse1004(false));
24 | }else{
25 | gameSession.setLogin(true);
26 | Character character = new Character(user);
27 | gameSession.setRole(character);
28 | GameServerContext.addCharacter(character);
29 | gameSession.sendMsg(new LoginResponse1004(true));
30 | }
31 |
32 |
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/msg/processor/login/OpenAppMsgProcessor1001.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.msg.processor.login;
2 |
3 | import com.dyz.gameserver.commons.message.ClientRequest;
4 | import com.dyz.gameserver.commons.session.GameSession;
5 | import com.dyz.gameserver.msg.processor.common.INotAuthProcessor;
6 | import com.dyz.gameserver.msg.processor.common.MsgProcessor;
7 | import com.dyz.gameserver.msg.response.login.OpenAppResponse1002;
8 |
9 | /**
10 | *
11 | *用户打开应用时,做一些初始化操作
12 | * @author daiyongzhi
13 | * @date 2015年1月28日 下午1:15:21
14 | * @version V1.0
15 | */
16 | public class OpenAppMsgProcessor1001 extends MsgProcessor implements INotAuthProcessor{
17 |
18 | @Override
19 | public void process(GameSession gameSession, ClientRequest request)
20 | throws Exception {
21 | gameSession.sendMsg(new OpenAppResponse1002("welecome !"));
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/msg/processor/login/SignUpMsgProcessor1005.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.msg.processor.login;
2 |
3 |
4 | import com.dyz.gameserver.commons.message.ClientRequest;
5 | import com.dyz.gameserver.commons.session.GameSession;
6 | import com.dyz.gameserver.msg.processor.common.INotAuthProcessor;
7 | import com.dyz.gameserver.msg.processor.common.MsgProcessor;
8 | import com.dyz.gameserver.msg.response.login.SignUpResponse1006;
9 | import com.dyz.persist.roledata.user.User;
10 | import com.dyz.persist.roledata.user.UserService;
11 |
12 | public class SignUpMsgProcessor1005 extends MsgProcessor implements INotAuthProcessor{
13 |
14 | @Override
15 | public void process(GameSession gameSession, ClientRequest request)
16 | throws Exception {
17 | String name = request.getString();
18 | String phone = request.getString();
19 | String email = request.getString();
20 | String passwd = request.getString();
21 |
22 | User user = new User();
23 | user.setName(name);
24 | user.setPhonenumber(phone);
25 | user.setEmail(email);
26 | user.setPasswd(passwd);
27 | user.setRegdate(System.currentTimeMillis());
28 |
29 | UserService.getInstance().insertUser(user);
30 | if(user.getId()!=null){
31 | gameSession.sendMsg(new SignUpResponse1006(true));
32 | }else{
33 | gameSession.sendMsg(new SignUpResponse1006(false));
34 | }
35 |
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/msg/response/login/LoginResponse1004.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.msg.response.login;
2 |
3 | import java.io.IOException;
4 |
5 | import com.dyz.gameserver.commons.message.ServerResponse;
6 |
7 | public class LoginResponse1004 extends ServerResponse{
8 |
9 | public LoginResponse1004(boolean isSuccess) {
10 | super(1004);
11 | try {
12 | output.writeBoolean(isSuccess);
13 | } catch (IOException e) {
14 | e.printStackTrace();
15 | }
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/msg/response/login/OpenAppResponse1002.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.msg.response.login;
2 |
3 | import java.io.IOException;
4 |
5 | import com.dyz.gameserver.commons.message.ServerResponse;
6 | /**
7 | *
8 | *
9 | * @author daiyongzhi
10 | * @date 2015年1月28日 下午2:12:34
11 | * @version V1.0
12 | */
13 | public class OpenAppResponse1002 extends ServerResponse{
14 |
15 | public OpenAppResponse1002(String initWord){
16 | super(1002);
17 | try {
18 | output.writeUTF(initWord);
19 | } catch (IOException e) {
20 |
21 | e.printStackTrace();
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/msg/response/login/SignUpResponse1006.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.msg.response.login;
2 |
3 | import java.io.IOException;
4 |
5 | import com.dyz.gameserver.commons.message.ServerResponse;
6 |
7 | public class SignUpResponse1006 extends ServerResponse{
8 |
9 | public SignUpResponse1006(boolean isSuccess) {
10 | super(1006);
11 | try {
12 | output.writeBoolean(isSuccess);
13 | } catch (IOException e) {
14 | e.printStackTrace();
15 | }
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/net/MinaMsgHandler.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.net;
2 |
3 | import org.apache.mina.core.service.IoHandlerAdapter;
4 | import org.apache.mina.core.session.IoSession;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 |
8 | import com.dyz.gameserver.bootstrap.GameServer;
9 | import com.dyz.gameserver.commons.message.ClientRequest;
10 | import com.dyz.gameserver.commons.session.GameSession;
11 | import com.dyz.gameserver.context.GameServerContext;
12 | import com.dyz.gameserver.sprite.Character;
13 |
14 | public class MinaMsgHandler extends IoHandlerAdapter{
15 |
16 | private static final Logger logger = LoggerFactory.getLogger(MinaMsgHandler.class);
17 |
18 | @Override
19 | public void sessionCreated(IoSession session) throws Exception {
20 | new GameSession(session);
21 | logger.info("a session create from ip {}",session.getRemoteAddress());
22 | }
23 |
24 | @Override
25 | public void messageReceived(IoSession session, Object message)
26 | throws Exception {
27 | ClientRequest clientRequest = (ClientRequest) message;
28 | GameSession gameSession = GameSession.getInstance(session);
29 | if (gameSession == null) {
30 | return;
31 | }
32 |
33 | GameServer.msgDispatcher.dispatchMsg(gameSession,clientRequest);
34 | }
35 |
36 | @Override
37 | public void exceptionCaught(IoSession session, Throwable cause)
38 | throws Exception {
39 | logger.error("服务器出错 {}",cause.getMessage());
40 | cause.printStackTrace();
41 | }
42 |
43 | @Override
44 | public void sessionClosed(IoSession session) throws Exception {
45 | GameSession gameSession = GameSession.getInstance(session);
46 | if(gameSession!=null){
47 | Character character = gameSession.getRole(Character.class);
48 | if(character!=null){
49 | character.destroy();
50 | GameServerContext.removeCharacter(character);
51 | }
52 | }
53 | logger.info("a session closed ip:{}",session.getRemoteAddress());
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/net/NetManager.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.net;
2 |
3 | import java.net.InetSocketAddress;
4 | import java.util.concurrent.TimeUnit;
5 |
6 | import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
7 | import org.apache.mina.core.filterchain.IoFilter;
8 | import org.apache.mina.core.service.IoHandler;
9 | import org.apache.mina.core.session.IdleStatus;
10 | import org.apache.mina.filter.codec.ProtocolCodecFilter;
11 | import org.apache.mina.filter.executor.ExecutorFilter;
12 | import org.apache.mina.filter.executor.OrderedThreadPoolExecutor;
13 | import org.apache.mina.transport.socket.SocketSessionConfig;
14 | import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
15 |
16 | import com.dyz.gameserver.commons.tool.ServerThreadFactory;
17 | import com.dyz.gameserver.net.codec.GameProtocolcodecFactory;
18 |
19 | public class NetManager {
20 |
21 | private NioSocketAcceptor acceptor;
22 | private OrderedThreadPoolExecutor threadpool;
23 |
24 | public void startListner(IoHandler iohandler,int listenPort) throws Exception{
25 | acceptor = new NioSocketAcceptor();
26 | acceptor.setBacklog(100);
27 | acceptor.setReuseAddress(true);
28 | acceptor.setHandler(iohandler);
29 |
30 | DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
31 | IoFilter protocol = new ProtocolCodecFilter(new GameProtocolcodecFactory());
32 | chain.addLast("codec", protocol);
33 | threadpool = new OrderedThreadPoolExecutor(500);
34 | threadpool.setThreadFactory(new ServerThreadFactory("OrderedThreadPool"));
35 | chain.addLast("threadPool", new ExecutorFilter(threadpool));
36 |
37 | int recsize = 5120;
38 | int sendsize = 40480;
39 | int timeout = 10;
40 | SocketSessionConfig sc = acceptor.getSessionConfig();
41 | sc.setReuseAddress(true);// 设置每一个非主监听连接的端口可以重用
42 | sc.setReceiveBufferSize(recsize);// 设置输入缓冲区的大小
43 | sc.setSendBufferSize(sendsize);// 设置输出缓冲区的大小
44 | sc.setTcpNoDelay(true);// flush函数的调用 设置为非延迟发送,为true则不组装成大包发送,收到东西马上发出
45 | sc.setSoLinger(0);
46 | sc.setIdleTime(IdleStatus.READER_IDLE, timeout);
47 | acceptor.bind(new InetSocketAddress(listenPort));
48 | }
49 |
50 | public void stop() throws InterruptedException{
51 | acceptor.unbind();
52 | threadpool.shutdown();
53 | threadpool.awaitTermination(5, TimeUnit.SECONDS);
54 | acceptor.dispose(true);
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/net/codec/GameMsgDecoder.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.net.codec;
2 |
3 | import org.apache.mina.core.buffer.IoBuffer;
4 | import org.apache.mina.core.session.IoSession;
5 | import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
6 | import org.apache.mina.filter.codec.ProtocolDecoderOutput;
7 | import org.slf4j.Logger;
8 | import org.slf4j.LoggerFactory;
9 |
10 | import com.dyz.gameserver.commons.message.ClientRequest;
11 | import com.dyz.gameserver.net.MinaMsgHandler;
12 |
13 | /**
14 | * 消息解码器。将连续的字节按照协议规范分割成完整的消息包,并包装成ClientRequest。
15 | * @author dyz
16 | */
17 | public class GameMsgDecoder extends CumulativeProtocolDecoder {
18 |
19 | private static final Logger logger = LoggerFactory.getLogger(MinaMsgHandler.class);
20 |
21 | public GameMsgDecoder() {
22 |
23 | }
24 |
25 | /**
26 | * flag(1 byte)+length(4 byte,后边内容的长度)+protocol code(4 byte)+content
27 | * length的长度包括 :消息号+ 内容
28 | */
29 | @Override
30 | protected boolean doDecode(IoSession session, IoBuffer iobuffer,
31 | ProtocolDecoderOutput protocolDecoderOutput) throws Exception {
32 |
33 | if(iobuffer.remaining()<(MsgProtocol.flagSize+MsgProtocol.lengthSize+MsgProtocol.msgCodeSize)){//数据不完整
34 | logger.info("数据包长度不足");
35 | return false;
36 | }
37 | iobuffer.mark();
38 | byte flag = iobuffer.get();//flag,备用
39 | if (flag == 1) {
40 | int length = iobuffer.getInt();//读取长度字段
41 | if(length<=0 || length>MsgProtocol.maxPackLength){//长度字段异常
42 | logger.info("数据包长度异常");
43 | return false;
44 | }
45 | if(iobuffer.remaining()>=length){//
46 | int preLimit = iobuffer.limit();//记录下当前的limit值
47 |
48 | /**
49 | * 这行代码有一个bug,
50 | * 读取协议内容时,如果第一个字节不是1,则越过此字节继续往后的读,直到读到1,
51 | * 然而在设置limit时没有考虑到越过去的flag之前的字节,从而导致设置的limit比本应设置的位置小。
52 | * 所以导致,iobuffer中当前position到设置的limit的长度小于我们要读取的length。
53 | * 结果导致抛出BufferUnderflowException
54 | */
55 | //iobuffer.limit(MsgProtocol.flagSize+MsgProtocol.lengthSize+length);
56 | iobuffer.limit(iobuffer.position()+length);
57 | byte[] body = new byte[length];
58 | iobuffer.get(body);
59 | iobuffer.limit(preLimit);
60 | ClientRequest message = new ClientRequest(body);
61 | protocolDecoderOutput.write(message);
62 | return true;
63 | }else{
64 | logger.info("数据包尚不完整");
65 | iobuffer.reset();
66 | return false;
67 | }
68 | }else{
69 | logger.info("flag 错误");
70 | return false;
71 | }
72 | }
73 |
74 | public void finishDecode(IoSession session, ProtocolDecoderOutput out)
75 | throws Exception {
76 | }
77 |
78 | public void dispose(IoSession session) throws Exception {
79 | }
80 | }
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/net/codec/GameMsgEncoder.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.net.codec;
2 |
3 | import org.apache.mina.core.session.IoSession;
4 | import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
5 | import org.apache.mina.filter.codec.ProtocolEncoderOutput;
6 |
7 | import com.dyz.gameserver.commons.message.ResponseMsg;
8 |
9 | public class GameMsgEncoder extends ProtocolEncoderAdapter {
10 |
11 | public GameMsgEncoder() {
12 | }
13 |
14 | // 在此处实现对ResponseMsg的编码工作,并把它写入输出流中
15 | public void encode(IoSession session, Object message,
16 | ProtocolEncoderOutput out) throws Exception {
17 | ResponseMsg value = (ResponseMsg) message;
18 |
19 | out.write(value.entireMsg());
20 | value.release();
21 | }
22 |
23 | public void dispose() throws Exception {
24 | }
25 | }
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/net/codec/GameProtocolcodecFactory.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.net.codec;
2 |
3 | import org.apache.mina.core.session.IoSession;
4 | import org.apache.mina.filter.codec.ProtocolCodecFactory;
5 | import org.apache.mina.filter.codec.ProtocolDecoder;
6 | import org.apache.mina.filter.codec.ProtocolEncoder;
7 |
8 |
9 | /**
10 | * 一个对象工厂,提供了获取编码器和解码器的函数接口。
11 | *
12 | * @author dyz
13 | *
14 | */
15 | public class GameProtocolcodecFactory implements ProtocolCodecFactory {
16 | private final GameMsgEncoder encoder;
17 | private final GameMsgDecoder decoder;
18 |
19 | public GameProtocolcodecFactory() {
20 | encoder = new GameMsgEncoder();
21 | decoder = new GameMsgDecoder();
22 | }
23 |
24 | public ProtocolEncoder getEncoder(IoSession session) {
25 | return encoder;
26 | }
27 |
28 | public ProtocolDecoder getDecoder(IoSession session) {
29 | return decoder;
30 | }
31 | }
32 |
33 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/net/codec/MsgProtocol.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.net.codec;
2 |
3 | /**
4 | * 消息协议相关常量
5 | * @author dyz
6 | *
7 | */
8 | public interface MsgProtocol {
9 |
10 | /**默认flag值*/
11 | public byte defaultFlag = 1;
12 | /**最大长度*/
13 | public int maxPackLength = 1024 * 5;
14 | /**标识数占得 byte数*/
15 | public int flagSize = 1;//
16 | /**协议中 长度部分 占用的byte数,其值表示( 协议号+内容) 的长度*/
17 | public int lengthSize = 4;//
18 | /**消息号占用的byte数*/
19 | public int msgCodeSize = 4;
20 | }
21 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/sprite/Character.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.sprite;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.dyz.gameserver.sprite.base.GameObj;
7 | import com.dyz.gameserver.sprite.tool.AsyncTaskQueue;
8 | import com.dyz.persist.roledata.user.User;
9 |
10 | public class Character implements GameObj{
11 |
12 | private static final Logger logger = LoggerFactory.getLogger(Character.class);
13 |
14 | private AsyncTaskQueue asyncTaskQueue = new AsyncTaskQueue();
15 |
16 | private User user;
17 | public Character(User user){
18 | this.user=user;
19 | }
20 |
21 | public Integer getUserId(){
22 | return this.user.getId();
23 | }
24 |
25 | /**
26 | * 添加异步任务,针对异步操作数据库
27 | * @param tasks
28 | */
29 | public void addAsyncTask(Runnable... tasks){
30 | asyncTaskQueue.addTask(tasks);
31 | }
32 |
33 | @Override
34 | public void destroy() {
35 | logger.info("用户{}断开服务器链接",user.getName());
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/sprite/base/GameObj.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.sprite.base;
2 |
3 | /**
4 | * 服务器中精灵接口
5 | *
6 | * @author daiyongzhi
7 | * @date 2015年1月30日 下午4:42:46
8 | * @version V1.0
9 | */
10 | public interface GameObj {
11 |
12 | public void destroy();
13 | }
14 |
--------------------------------------------------------------------------------
/src/com/dyz/gameserver/sprite/tool/AsyncTaskQueue.java:
--------------------------------------------------------------------------------
1 | package com.dyz.gameserver.sprite.tool;
2 |
3 | import java.util.concurrent.ConcurrentLinkedQueue;
4 |
5 | import com.dyz.gameserver.context.ExecutorServiceManager;
6 | /**
7 | * 异步操作类,用于异步执行数据库操作
8 | *
9 | * @author daiyongzhi
10 | * @date 2015年1月30日 下午6:23:47
11 | * @version V1.0
12 | */
13 | public class AsyncTaskQueue {
14 |
15 | private ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue();
16 | private volatile boolean processingCompleted = true;
17 |
18 | public void addTask(Runnable... tasks){
19 | for(Runnable task:tasks){
20 | queue.offer(task);
21 | }
22 | if(processingCompleted){
23 | processingCompleted = false;
24 | ExecutorServiceManager.getInstance().getExecutorServiceForDB().execute(new Runnable() {
25 |
26 | @Override
27 | public void run() {
28 | executeQueueTask();
29 | }
30 | });
31 | }
32 | }
33 |
34 | private void executeQueueTask(){
35 | while(true){
36 | Runnable task = queue.poll();
37 | if(task == null){
38 | processingCompleted = true;
39 | break;
40 | }else{
41 | task.run();
42 | }
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/com/dyz/persist/gamedata/common/IGameDataCach.java:
--------------------------------------------------------------------------------
1 | package com.dyz.persist.gamedata.common;
2 | /**
3 | * 所有的对gamedata数据操作的管理器实现此接口,用于统一加载gamedata
4 | * 继承此类的 gamedata管理器要为单例模式
5 | *
6 | * @author dyz
7 | *
8 | */
9 | public interface IGameDataCach {
10 |
11 | /**
12 | * 重记载数据,也可以用于初始化记载,
13 | *
14 | */
15 | public void reload();
16 | }
17 |
--------------------------------------------------------------------------------
/src/com/dyz/persist/roledata/user/User.java:
--------------------------------------------------------------------------------
1 | package com.dyz.persist.roledata.user;
2 |
3 | public class User {
4 | /**
5 | * This field was generated by Apache iBATIS ibator.
6 | * This field corresponds to the database column t_user.f_id
7 | *
8 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
9 | */
10 | private Integer id;
11 |
12 | /**
13 | * This field was generated by Apache iBATIS ibator.
14 | * This field corresponds to the database column t_user.f_name
15 | *
16 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
17 | */
18 | private String name;
19 |
20 | /**
21 | * This field was generated by Apache iBATIS ibator.
22 | * This field corresponds to the database column t_user.f_phonenumber
23 | *
24 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
25 | */
26 | private String phonenumber;
27 |
28 | /**
29 | * This field was generated by Apache iBATIS ibator.
30 | * This field corresponds to the database column t_user.f_email
31 | *
32 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
33 | */
34 | private String email;
35 |
36 | /**
37 | * This field was generated by Apache iBATIS ibator.
38 | * This field corresponds to the database column t_user.f_passwd
39 | *
40 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
41 | */
42 | private String passwd;
43 |
44 | /**
45 | * This field was generated by Apache iBATIS ibator.
46 | * This field corresponds to the database column t_user.f_regdate
47 | *
48 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
49 | */
50 | private Long regdate;
51 |
52 | /**
53 | * This method was generated by Apache iBATIS ibator.
54 | * This method returns the value of the database column t_user.f_id
55 | *
56 | * @return the value of t_user.f_id
57 | *
58 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
59 | */
60 | public Integer getId() {
61 | return id;
62 | }
63 |
64 | /**
65 | * This method was generated by Apache iBATIS ibator.
66 | * This method sets the value of the database column t_user.f_id
67 | *
68 | * @param id the value for t_user.f_id
69 | *
70 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
71 | */
72 | public void setId(Integer id) {
73 | this.id = id;
74 | }
75 |
76 | /**
77 | * This method was generated by Apache iBATIS ibator.
78 | * This method returns the value of the database column t_user.f_name
79 | *
80 | * @return the value of t_user.f_name
81 | *
82 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
83 | */
84 | public String getName() {
85 | return name;
86 | }
87 |
88 | /**
89 | * This method was generated by Apache iBATIS ibator.
90 | * This method sets the value of the database column t_user.f_name
91 | *
92 | * @param name the value for t_user.f_name
93 | *
94 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
95 | */
96 | public void setName(String name) {
97 | this.name = name;
98 | }
99 |
100 | /**
101 | * This method was generated by Apache iBATIS ibator.
102 | * This method returns the value of the database column t_user.f_phonenumber
103 | *
104 | * @return the value of t_user.f_phonenumber
105 | *
106 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
107 | */
108 | public String getPhonenumber() {
109 | return phonenumber;
110 | }
111 |
112 | /**
113 | * This method was generated by Apache iBATIS ibator.
114 | * This method sets the value of the database column t_user.f_phonenumber
115 | *
116 | * @param phonenumber the value for t_user.f_phonenumber
117 | *
118 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
119 | */
120 | public void setPhonenumber(String phonenumber) {
121 | this.phonenumber = phonenumber;
122 | }
123 |
124 | /**
125 | * This method was generated by Apache iBATIS ibator.
126 | * This method returns the value of the database column t_user.f_email
127 | *
128 | * @return the value of t_user.f_email
129 | *
130 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
131 | */
132 | public String getEmail() {
133 | return email;
134 | }
135 |
136 | /**
137 | * This method was generated by Apache iBATIS ibator.
138 | * This method sets the value of the database column t_user.f_email
139 | *
140 | * @param email the value for t_user.f_email
141 | *
142 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
143 | */
144 | public void setEmail(String email) {
145 | this.email = email;
146 | }
147 |
148 | /**
149 | * This method was generated by Apache iBATIS ibator.
150 | * This method returns the value of the database column t_user.f_passwd
151 | *
152 | * @return the value of t_user.f_passwd
153 | *
154 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
155 | */
156 | public String getPasswd() {
157 | return passwd;
158 | }
159 |
160 | /**
161 | * This method was generated by Apache iBATIS ibator.
162 | * This method sets the value of the database column t_user.f_passwd
163 | *
164 | * @param passwd the value for t_user.f_passwd
165 | *
166 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
167 | */
168 | public void setPasswd(String passwd) {
169 | this.passwd = passwd;
170 | }
171 |
172 | /**
173 | * This method was generated by Apache iBATIS ibator.
174 | * This method returns the value of the database column t_user.f_regdate
175 | *
176 | * @return the value of t_user.f_regdate
177 | *
178 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
179 | */
180 | public Long getRegdate() {
181 | return regdate;
182 | }
183 |
184 | /**
185 | * This method was generated by Apache iBATIS ibator.
186 | * This method sets the value of the database column t_user.f_regdate
187 | *
188 | * @param regdate the value for t_user.f_regdate
189 | *
190 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
191 | */
192 | public void setRegdate(Long regdate) {
193 | this.regdate = regdate;
194 | }
195 | }
--------------------------------------------------------------------------------
/src/com/dyz/persist/roledata/user/UserDAO.java:
--------------------------------------------------------------------------------
1 | package com.dyz.persist.roledata.user;
2 |
3 | import com.dyz.persist.roledata.user.User;
4 | import com.dyz.persist.roledata.user.UserExample;
5 | import java.sql.SQLException;
6 | import java.util.List;
7 |
8 | public interface UserDAO {
9 | /**
10 | * This method was generated by Apache iBATIS ibator.
11 | * This method corresponds to the database table t_user
12 | *
13 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
14 | */
15 | int countByExample(UserExample example) throws SQLException;
16 |
17 | /**
18 | * This method was generated by Apache iBATIS ibator.
19 | * This method corresponds to the database table t_user
20 | *
21 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
22 | */
23 | int deleteByExample(UserExample example) throws SQLException;
24 |
25 | /**
26 | * This method was generated by Apache iBATIS ibator.
27 | * This method corresponds to the database table t_user
28 | *
29 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
30 | */
31 | int deleteByPrimaryKey(Integer id) throws SQLException;
32 |
33 | /**
34 | * This method was generated by Apache iBATIS ibator.
35 | * This method corresponds to the database table t_user
36 | *
37 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
38 | */
39 | void insert(User record) throws SQLException;
40 |
41 | /**
42 | * 插入记录并返回 主键id,并设置到User对象
43 | * @param user
44 | * @throws SQLException
45 | */
46 | void insertForId(User user)throws SQLException;
47 | /**
48 | * This method was generated by Apache iBATIS ibator.
49 | * This method corresponds to the database table t_user
50 | *
51 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
52 | */
53 | void insertSelective(User record) throws SQLException;
54 |
55 | /**
56 | * This method was generated by Apache iBATIS ibator.
57 | * This method corresponds to the database table t_user
58 | *
59 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
60 | */
61 | List selectByExample(UserExample example) throws SQLException;
62 |
63 | /**
64 | * This method was generated by Apache iBATIS ibator.
65 | * This method corresponds to the database table t_user
66 | *
67 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
68 | */
69 | User selectByPrimaryKey(Integer id) throws SQLException;
70 |
71 | /**
72 | * This method was generated by Apache iBATIS ibator.
73 | * This method corresponds to the database table t_user
74 | *
75 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
76 | */
77 | int updateByExampleSelective(User record, UserExample example) throws SQLException;
78 |
79 | /**
80 | * This method was generated by Apache iBATIS ibator.
81 | * This method corresponds to the database table t_user
82 | *
83 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
84 | */
85 | int updateByExample(User record, UserExample example) throws SQLException;
86 |
87 | /**
88 | * This method was generated by Apache iBATIS ibator.
89 | * This method corresponds to the database table t_user
90 | *
91 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
92 | */
93 | int updateByPrimaryKeySelective(User record) throws SQLException;
94 |
95 | /**
96 | * This method was generated by Apache iBATIS ibator.
97 | * This method corresponds to the database table t_user
98 | *
99 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
100 | */
101 | int updateByPrimaryKey(User record) throws SQLException;
102 | }
--------------------------------------------------------------------------------
/src/com/dyz/persist/roledata/user/UserDAOImpl.java:
--------------------------------------------------------------------------------
1 | package com.dyz.persist.roledata.user;
2 |
3 | import com.dyz.persist.roledata.user.User;
4 | import com.dyz.persist.roledata.user.UserExample;
5 | import com.ibatis.sqlmap.client.SqlMapClient;
6 | import java.sql.SQLException;
7 | import java.util.List;
8 |
9 | public class UserDAOImpl implements UserDAO {
10 | /**
11 | * This field was generated by Apache iBATIS ibator.
12 | * This field corresponds to the database table t_user
13 | *
14 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
15 | */
16 | private SqlMapClient sqlMapClient;
17 |
18 | /**
19 | * This method was generated by Apache iBATIS ibator.
20 | * This method corresponds to the database table t_user
21 | *
22 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
23 | */
24 | public UserDAOImpl(SqlMapClient sqlMapClient) {
25 | super();
26 | this.sqlMapClient = sqlMapClient;
27 | }
28 |
29 | /**
30 | * This method was generated by Apache iBATIS ibator.
31 | * This method corresponds to the database table t_user
32 | *
33 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
34 | */
35 | public int countByExample(UserExample example) throws SQLException {
36 | Integer count = (Integer) sqlMapClient.queryForObject("t_user.ibatorgenerated_countByExample", example);
37 | return count.intValue();
38 | }
39 |
40 | /**
41 | * This method was generated by Apache iBATIS ibator.
42 | * This method corresponds to the database table t_user
43 | *
44 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
45 | */
46 | public int deleteByExample(UserExample example) throws SQLException {
47 | int rows = sqlMapClient.delete("t_user.ibatorgenerated_deleteByExample", example);
48 | return rows;
49 | }
50 |
51 | /**
52 | * This method was generated by Apache iBATIS ibator.
53 | * This method corresponds to the database table t_user
54 | *
55 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
56 | */
57 | public int deleteByPrimaryKey(Integer id) throws SQLException {
58 | User key = new User();
59 | key.setId(id);
60 | int rows = sqlMapClient.delete("t_user.ibatorgenerated_deleteByPrimaryKey", key);
61 | return rows;
62 | }
63 |
64 | /**
65 | * This method was generated by Apache iBATIS ibator.
66 | * This method corresponds to the database table t_user
67 | *
68 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
69 | */
70 | public void insert(User record) throws SQLException {
71 | sqlMapClient.insert("t_user.ibatorgenerated_insert", record);
72 | }
73 |
74 | /**
75 | * This method was generated by Apache iBATIS ibator.
76 | * This method corresponds to the database table t_user
77 | *
78 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
79 | */
80 | public void insertSelective(User record) throws SQLException {
81 | sqlMapClient.insert("t_user.ibatorgenerated_insertSelective", record);
82 | }
83 |
84 | /**
85 | * This method was generated by Apache iBATIS ibator.
86 | * This method corresponds to the database table t_user
87 | *
88 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
89 | */
90 | public List selectByExample(UserExample example) throws SQLException {
91 | List list = sqlMapClient.queryForList("t_user.ibatorgenerated_selectByExample", example);
92 | return list;
93 | }
94 |
95 | /**
96 | * This method was generated by Apache iBATIS ibator.
97 | * This method corresponds to the database table t_user
98 | *
99 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
100 | */
101 | public User selectByPrimaryKey(Integer id) throws SQLException {
102 | User key = new User();
103 | key.setId(id);
104 | User record = (User) sqlMapClient.queryForObject("t_user.ibatorgenerated_selectByPrimaryKey", key);
105 | return record;
106 | }
107 |
108 | /**
109 | * This method was generated by Apache iBATIS ibator.
110 | * This method corresponds to the database table t_user
111 | *
112 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
113 | */
114 | public int updateByExampleSelective(User record, UserExample example) throws SQLException {
115 | UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
116 | int rows = sqlMapClient.update("t_user.ibatorgenerated_updateByExampleSelective", parms);
117 | return rows;
118 | }
119 |
120 | /**
121 | * This method was generated by Apache iBATIS ibator.
122 | * This method corresponds to the database table t_user
123 | *
124 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
125 | */
126 | public int updateByExample(User record, UserExample example) throws SQLException {
127 | UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
128 | int rows = sqlMapClient.update("t_user.ibatorgenerated_updateByExample", parms);
129 | return rows;
130 | }
131 |
132 | /**
133 | * This method was generated by Apache iBATIS ibator.
134 | * This method corresponds to the database table t_user
135 | *
136 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
137 | */
138 | public int updateByPrimaryKeySelective(User record) throws SQLException {
139 | int rows = sqlMapClient.update("t_user.ibatorgenerated_updateByPrimaryKeySelective", record);
140 | return rows;
141 | }
142 |
143 | /**
144 | * This method was generated by Apache iBATIS ibator.
145 | * This method corresponds to the database table t_user
146 | *
147 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
148 | */
149 | public int updateByPrimaryKey(User record) throws SQLException {
150 | int rows = sqlMapClient.update("t_user.ibatorgenerated_updateByPrimaryKey", record);
151 | return rows;
152 | }
153 |
154 | /**
155 | * This class was generated by Apache iBATIS ibator.
156 | * This class corresponds to the database table t_user
157 | *
158 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
159 | */
160 | private static class UpdateByExampleParms extends UserExample {
161 | private Object record;
162 |
163 | public UpdateByExampleParms(Object record, UserExample example) {
164 | super(example);
165 | this.record = record;
166 | }
167 |
168 | public Object getRecord() {
169 | return record;
170 | }
171 | }
172 |
173 | @Override
174 | public void insertForId(User user) throws SQLException {
175 | sqlMapClient.insert("t_user.customizationInsert", user);
176 | }
177 | }
--------------------------------------------------------------------------------
/src/com/dyz/persist/roledata/user/UserExample.java:
--------------------------------------------------------------------------------
1 | package com.dyz.persist.roledata.user;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashMap;
5 | import java.util.List;
6 | import java.util.Map;
7 |
8 | public class UserExample {
9 | /**
10 | * This field was generated by Apache iBATIS ibator.
11 | * This field corresponds to the database table t_user
12 | *
13 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
14 | */
15 | protected String orderByClause;
16 |
17 | /**
18 | * This field was generated by Apache iBATIS ibator.
19 | * This field corresponds to the database table t_user
20 | *
21 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
22 | */
23 | protected List oredCriteria;
24 |
25 | /**
26 | * This method was generated by Apache iBATIS ibator.
27 | * This method corresponds to the database table t_user
28 | *
29 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
30 | */
31 | public UserExample() {
32 | oredCriteria = new ArrayList();
33 | }
34 |
35 | /**
36 | * This method was generated by Apache iBATIS ibator.
37 | * This method corresponds to the database table t_user
38 | *
39 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
40 | */
41 | protected UserExample(UserExample example) {
42 | this.orderByClause = example.orderByClause;
43 | this.oredCriteria = example.oredCriteria;
44 | }
45 |
46 | /**
47 | * This method was generated by Apache iBATIS ibator.
48 | * This method corresponds to the database table t_user
49 | *
50 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
51 | */
52 | public void setOrderByClause(String orderByClause) {
53 | this.orderByClause = orderByClause;
54 | }
55 |
56 | /**
57 | * This method was generated by Apache iBATIS ibator.
58 | * This method corresponds to the database table t_user
59 | *
60 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
61 | */
62 | public String getOrderByClause() {
63 | return orderByClause;
64 | }
65 |
66 | /**
67 | * This method was generated by Apache iBATIS ibator.
68 | * This method corresponds to the database table t_user
69 | *
70 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
71 | */
72 | public List getOredCriteria() {
73 | return oredCriteria;
74 | }
75 |
76 | /**
77 | * This method was generated by Apache iBATIS ibator.
78 | * This method corresponds to the database table t_user
79 | *
80 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
81 | */
82 | public void or(Criteria criteria) {
83 | oredCriteria.add(criteria);
84 | }
85 |
86 | /**
87 | * This method was generated by Apache iBATIS ibator.
88 | * This method corresponds to the database table t_user
89 | *
90 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
91 | */
92 | public Criteria createCriteria() {
93 | Criteria criteria = createCriteriaInternal();
94 | if (oredCriteria.size() == 0) {
95 | oredCriteria.add(criteria);
96 | }
97 | return criteria;
98 | }
99 |
100 | /**
101 | * This method was generated by Apache iBATIS ibator.
102 | * This method corresponds to the database table t_user
103 | *
104 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
105 | */
106 | protected Criteria createCriteriaInternal() {
107 | Criteria criteria = new Criteria();
108 | return criteria;
109 | }
110 |
111 | /**
112 | * This method was generated by Apache iBATIS ibator.
113 | * This method corresponds to the database table t_user
114 | *
115 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
116 | */
117 | public void clear() {
118 | oredCriteria.clear();
119 | }
120 |
121 | /**
122 | * This class was generated by Apache iBATIS ibator.
123 | * This class corresponds to the database table t_user
124 | *
125 | * @ibatorgenerated Fri Jan 30 10:52:50 CST 2015
126 | */
127 | public static class Criteria {
128 | protected List criteriaWithoutValue;
129 |
130 | protected List criteriaWithSingleValue;
131 |
132 | protected List criteriaWithListValue;
133 |
134 | protected List criteriaWithBetweenValue;
135 |
136 | protected Criteria() {
137 | super();
138 | criteriaWithoutValue = new ArrayList();
139 | criteriaWithSingleValue = new ArrayList();
140 | criteriaWithListValue = new ArrayList();
141 | criteriaWithBetweenValue = new ArrayList();
142 | }
143 |
144 | public boolean isValid() {
145 | return criteriaWithoutValue.size() > 0
146 | || criteriaWithSingleValue.size() > 0
147 | || criteriaWithListValue.size() > 0
148 | || criteriaWithBetweenValue.size() > 0;
149 | }
150 |
151 | public List getCriteriaWithoutValue() {
152 | return criteriaWithoutValue;
153 | }
154 |
155 | public List getCriteriaWithSingleValue() {
156 | return criteriaWithSingleValue;
157 | }
158 |
159 | public List getCriteriaWithListValue() {
160 | return criteriaWithListValue;
161 | }
162 |
163 | public List getCriteriaWithBetweenValue() {
164 | return criteriaWithBetweenValue;
165 | }
166 |
167 | protected void addCriterion(String condition) {
168 | if (condition == null) {
169 | throw new RuntimeException("Value for condition cannot be null");
170 | }
171 | criteriaWithoutValue.add(condition);
172 | }
173 |
174 | protected void addCriterion(String condition, Object value, String property) {
175 | if (value == null) {
176 | throw new RuntimeException("Value for " + property + " cannot be null");
177 | }
178 | Map map = new HashMap();
179 | map.put("condition", condition);
180 | map.put("value", value);
181 | criteriaWithSingleValue.add(map);
182 | }
183 |
184 | protected void addCriterion(String condition, List values, String property) {
185 | if (values == null || values.size() == 0) {
186 | throw new RuntimeException("Value list for " + property + " cannot be null or empty");
187 | }
188 | Map map = new HashMap();
189 | map.put("condition", condition);
190 | map.put("values", values);
191 | criteriaWithListValue.add(map);
192 | }
193 |
194 | protected void addCriterion(String condition, Object value1, Object value2, String property) {
195 | if (value1 == null || value2 == null) {
196 | throw new RuntimeException("Between values for " + property + " cannot be null");
197 | }
198 | List list = new ArrayList();
199 | list.add(value1);
200 | list.add(value2);
201 | Map map = new HashMap();
202 | map.put("condition", condition);
203 | map.put("values", list);
204 | criteriaWithBetweenValue.add(map);
205 | }
206 |
207 | public Criteria andIdIsNull() {
208 | addCriterion("f_id is null");
209 | return this;
210 | }
211 |
212 | public Criteria andIdIsNotNull() {
213 | addCriterion("f_id is not null");
214 | return this;
215 | }
216 |
217 | public Criteria andIdEqualTo(Integer value) {
218 | addCriterion("f_id =", value, "id");
219 | return this;
220 | }
221 |
222 | public Criteria andIdNotEqualTo(Integer value) {
223 | addCriterion("f_id <>", value, "id");
224 | return this;
225 | }
226 |
227 | public Criteria andIdGreaterThan(Integer value) {
228 | addCriterion("f_id >", value, "id");
229 | return this;
230 | }
231 |
232 | public Criteria andIdGreaterThanOrEqualTo(Integer value) {
233 | addCriterion("f_id >=", value, "id");
234 | return this;
235 | }
236 |
237 | public Criteria andIdLessThan(Integer value) {
238 | addCriterion("f_id <", value, "id");
239 | return this;
240 | }
241 |
242 | public Criteria andIdLessThanOrEqualTo(Integer value) {
243 | addCriterion("f_id <=", value, "id");
244 | return this;
245 | }
246 |
247 | public Criteria andIdIn(List values) {
248 | addCriterion("f_id in", values, "id");
249 | return this;
250 | }
251 |
252 | public Criteria andIdNotIn(List values) {
253 | addCriterion("f_id not in", values, "id");
254 | return this;
255 | }
256 |
257 | public Criteria andIdBetween(Integer value1, Integer value2) {
258 | addCriterion("f_id between", value1, value2, "id");
259 | return this;
260 | }
261 |
262 | public Criteria andIdNotBetween(Integer value1, Integer value2) {
263 | addCriterion("f_id not between", value1, value2, "id");
264 | return this;
265 | }
266 |
267 | public Criteria andNameIsNull() {
268 | addCriterion("f_name is null");
269 | return this;
270 | }
271 |
272 | public Criteria andNameIsNotNull() {
273 | addCriterion("f_name is not null");
274 | return this;
275 | }
276 |
277 | public Criteria andNameEqualTo(String value) {
278 | addCriterion("f_name =", value, "name");
279 | return this;
280 | }
281 |
282 | public Criteria andNameNotEqualTo(String value) {
283 | addCriterion("f_name <>", value, "name");
284 | return this;
285 | }
286 |
287 | public Criteria andNameGreaterThan(String value) {
288 | addCriterion("f_name >", value, "name");
289 | return this;
290 | }
291 |
292 | public Criteria andNameGreaterThanOrEqualTo(String value) {
293 | addCriterion("f_name >=", value, "name");
294 | return this;
295 | }
296 |
297 | public Criteria andNameLessThan(String value) {
298 | addCriterion("f_name <", value, "name");
299 | return this;
300 | }
301 |
302 | public Criteria andNameLessThanOrEqualTo(String value) {
303 | addCriterion("f_name <=", value, "name");
304 | return this;
305 | }
306 |
307 | public Criteria andNameLike(String value) {
308 | addCriterion("f_name like", value, "name");
309 | return this;
310 | }
311 |
312 | public Criteria andNameNotLike(String value) {
313 | addCriterion("f_name not like", value, "name");
314 | return this;
315 | }
316 |
317 | public Criteria andNameIn(List values) {
318 | addCriterion("f_name in", values, "name");
319 | return this;
320 | }
321 |
322 | public Criteria andNameNotIn(List values) {
323 | addCriterion("f_name not in", values, "name");
324 | return this;
325 | }
326 |
327 | public Criteria andNameBetween(String value1, String value2) {
328 | addCriterion("f_name between", value1, value2, "name");
329 | return this;
330 | }
331 |
332 | public Criteria andNameNotBetween(String value1, String value2) {
333 | addCriterion("f_name not between", value1, value2, "name");
334 | return this;
335 | }
336 |
337 | public Criteria andPhonenumberIsNull() {
338 | addCriterion("f_phonenumber is null");
339 | return this;
340 | }
341 |
342 | public Criteria andPhonenumberIsNotNull() {
343 | addCriterion("f_phonenumber is not null");
344 | return this;
345 | }
346 |
347 | public Criteria andPhonenumberEqualTo(String value) {
348 | addCriterion("f_phonenumber =", value, "phonenumber");
349 | return this;
350 | }
351 |
352 | public Criteria andPhonenumberNotEqualTo(String value) {
353 | addCriterion("f_phonenumber <>", value, "phonenumber");
354 | return this;
355 | }
356 |
357 | public Criteria andPhonenumberGreaterThan(String value) {
358 | addCriterion("f_phonenumber >", value, "phonenumber");
359 | return this;
360 | }
361 |
362 | public Criteria andPhonenumberGreaterThanOrEqualTo(String value) {
363 | addCriterion("f_phonenumber >=", value, "phonenumber");
364 | return this;
365 | }
366 |
367 | public Criteria andPhonenumberLessThan(String value) {
368 | addCriterion("f_phonenumber <", value, "phonenumber");
369 | return this;
370 | }
371 |
372 | public Criteria andPhonenumberLessThanOrEqualTo(String value) {
373 | addCriterion("f_phonenumber <=", value, "phonenumber");
374 | return this;
375 | }
376 |
377 | public Criteria andPhonenumberLike(String value) {
378 | addCriterion("f_phonenumber like", value, "phonenumber");
379 | return this;
380 | }
381 |
382 | public Criteria andPhonenumberNotLike(String value) {
383 | addCriterion("f_phonenumber not like", value, "phonenumber");
384 | return this;
385 | }
386 |
387 | public Criteria andPhonenumberIn(List values) {
388 | addCriterion("f_phonenumber in", values, "phonenumber");
389 | return this;
390 | }
391 |
392 | public Criteria andPhonenumberNotIn(List values) {
393 | addCriterion("f_phonenumber not in", values, "phonenumber");
394 | return this;
395 | }
396 |
397 | public Criteria andPhonenumberBetween(String value1, String value2) {
398 | addCriterion("f_phonenumber between", value1, value2, "phonenumber");
399 | return this;
400 | }
401 |
402 | public Criteria andPhonenumberNotBetween(String value1, String value2) {
403 | addCriterion("f_phonenumber not between", value1, value2, "phonenumber");
404 | return this;
405 | }
406 |
407 | public Criteria andEmailIsNull() {
408 | addCriterion("f_email is null");
409 | return this;
410 | }
411 |
412 | public Criteria andEmailIsNotNull() {
413 | addCriterion("f_email is not null");
414 | return this;
415 | }
416 |
417 | public Criteria andEmailEqualTo(String value) {
418 | addCriterion("f_email =", value, "email");
419 | return this;
420 | }
421 |
422 | public Criteria andEmailNotEqualTo(String value) {
423 | addCriterion("f_email <>", value, "email");
424 | return this;
425 | }
426 |
427 | public Criteria andEmailGreaterThan(String value) {
428 | addCriterion("f_email >", value, "email");
429 | return this;
430 | }
431 |
432 | public Criteria andEmailGreaterThanOrEqualTo(String value) {
433 | addCriterion("f_email >=", value, "email");
434 | return this;
435 | }
436 |
437 | public Criteria andEmailLessThan(String value) {
438 | addCriterion("f_email <", value, "email");
439 | return this;
440 | }
441 |
442 | public Criteria andEmailLessThanOrEqualTo(String value) {
443 | addCriterion("f_email <=", value, "email");
444 | return this;
445 | }
446 |
447 | public Criteria andEmailLike(String value) {
448 | addCriterion("f_email like", value, "email");
449 | return this;
450 | }
451 |
452 | public Criteria andEmailNotLike(String value) {
453 | addCriterion("f_email not like", value, "email");
454 | return this;
455 | }
456 |
457 | public Criteria andEmailIn(List values) {
458 | addCriterion("f_email in", values, "email");
459 | return this;
460 | }
461 |
462 | public Criteria andEmailNotIn(List values) {
463 | addCriterion("f_email not in", values, "email");
464 | return this;
465 | }
466 |
467 | public Criteria andEmailBetween(String value1, String value2) {
468 | addCriterion("f_email between", value1, value2, "email");
469 | return this;
470 | }
471 |
472 | public Criteria andEmailNotBetween(String value1, String value2) {
473 | addCriterion("f_email not between", value1, value2, "email");
474 | return this;
475 | }
476 |
477 | public Criteria andPasswdIsNull() {
478 | addCriterion("f_passwd is null");
479 | return this;
480 | }
481 |
482 | public Criteria andPasswdIsNotNull() {
483 | addCriterion("f_passwd is not null");
484 | return this;
485 | }
486 |
487 | public Criteria andPasswdEqualTo(String value) {
488 | addCriterion("f_passwd =", value, "passwd");
489 | return this;
490 | }
491 |
492 | public Criteria andPasswdNotEqualTo(String value) {
493 | addCriterion("f_passwd <>", value, "passwd");
494 | return this;
495 | }
496 |
497 | public Criteria andPasswdGreaterThan(String value) {
498 | addCriterion("f_passwd >", value, "passwd");
499 | return this;
500 | }
501 |
502 | public Criteria andPasswdGreaterThanOrEqualTo(String value) {
503 | addCriterion("f_passwd >=", value, "passwd");
504 | return this;
505 | }
506 |
507 | public Criteria andPasswdLessThan(String value) {
508 | addCriterion("f_passwd <", value, "passwd");
509 | return this;
510 | }
511 |
512 | public Criteria andPasswdLessThanOrEqualTo(String value) {
513 | addCriterion("f_passwd <=", value, "passwd");
514 | return this;
515 | }
516 |
517 | public Criteria andPasswdLike(String value) {
518 | addCriterion("f_passwd like", value, "passwd");
519 | return this;
520 | }
521 |
522 | public Criteria andPasswdNotLike(String value) {
523 | addCriterion("f_passwd not like", value, "passwd");
524 | return this;
525 | }
526 |
527 | public Criteria andPasswdIn(List values) {
528 | addCriterion("f_passwd in", values, "passwd");
529 | return this;
530 | }
531 |
532 | public Criteria andPasswdNotIn(List values) {
533 | addCriterion("f_passwd not in", values, "passwd");
534 | return this;
535 | }
536 |
537 | public Criteria andPasswdBetween(String value1, String value2) {
538 | addCriterion("f_passwd between", value1, value2, "passwd");
539 | return this;
540 | }
541 |
542 | public Criteria andPasswdNotBetween(String value1, String value2) {
543 | addCriterion("f_passwd not between", value1, value2, "passwd");
544 | return this;
545 | }
546 |
547 | public Criteria andRegdateIsNull() {
548 | addCriterion("f_regdate is null");
549 | return this;
550 | }
551 |
552 | public Criteria andRegdateIsNotNull() {
553 | addCriterion("f_regdate is not null");
554 | return this;
555 | }
556 |
557 | public Criteria andRegdateEqualTo(Long value) {
558 | addCriterion("f_regdate =", value, "regdate");
559 | return this;
560 | }
561 |
562 | public Criteria andRegdateNotEqualTo(Long value) {
563 | addCriterion("f_regdate <>", value, "regdate");
564 | return this;
565 | }
566 |
567 | public Criteria andRegdateGreaterThan(Long value) {
568 | addCriterion("f_regdate >", value, "regdate");
569 | return this;
570 | }
571 |
572 | public Criteria andRegdateGreaterThanOrEqualTo(Long value) {
573 | addCriterion("f_regdate >=", value, "regdate");
574 | return this;
575 | }
576 |
577 | public Criteria andRegdateLessThan(Long value) {
578 | addCriterion("f_regdate <", value, "regdate");
579 | return this;
580 | }
581 |
582 | public Criteria andRegdateLessThanOrEqualTo(Long value) {
583 | addCriterion("f_regdate <=", value, "regdate");
584 | return this;
585 | }
586 |
587 | public Criteria andRegdateIn(List values) {
588 | addCriterion("f_regdate in", values, "regdate");
589 | return this;
590 | }
591 |
592 | public Criteria andRegdateNotIn(List values) {
593 | addCriterion("f_regdate not in", values, "regdate");
594 | return this;
595 | }
596 |
597 | public Criteria andRegdateBetween(Long value1, Long value2) {
598 | addCriterion("f_regdate between", value1, value2, "regdate");
599 | return this;
600 | }
601 |
602 | public Criteria andRegdateNotBetween(Long value1, Long value2) {
603 | addCriterion("f_regdate not between", value1, value2, "regdate");
604 | return this;
605 | }
606 | }
607 | }
--------------------------------------------------------------------------------
/src/com/dyz/persist/roledata/user/UserService.java:
--------------------------------------------------------------------------------
1 | package com.dyz.persist.roledata.user;
2 |
3 | import java.sql.SQLException;
4 | import java.util.List;
5 |
6 | import com.dyz.persist.util.DBUtil;
7 |
8 | public class UserService {
9 |
10 | private final UserDAO userDao = new UserDAOImpl(DBUtil.getRoledataSqlMapClient());
11 |
12 | private static UserService userService = new UserService();
13 |
14 | private UserService(){}
15 |
16 | public static UserService getInstance(){
17 | return userService;
18 | }
19 |
20 | public void insertUser(User user) throws SQLException{
21 | userDao.insertForId(user);
22 | }
23 |
24 | public User selectUser(String phone,String passwd) throws SQLException{
25 | UserExample userExample = new UserExample();
26 | userExample.createCriteria().andPhonenumberEqualTo(phone).andPasswdEqualTo(passwd);
27 | List users = userDao.selectByExample(userExample);
28 | if(users!=null && users.size()>0){
29 | return users.get(0);
30 | }else{
31 | return null;
32 | }
33 | }
34 |
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/com/dyz/persist/util/DBUtil.java:
--------------------------------------------------------------------------------
1 | package com.dyz.persist.util;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 |
6 | import com.ibatis.common.resources.Resources;
7 | import com.ibatis.sqlmap.client.SqlMapClient;
8 | import com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser;
9 | /**
10 | * 操作数据库工具类,负责初始化ibatis的SqlMapClient,
11 | * 注意,如果ibatis的config文件中没有配置sqlMap元素,不要初始化,否则会报错
12 | * @author dyz
13 | *
14 | */
15 | public class DBUtil {
16 |
17 | private static SqlMapClient roledataSqlMapClient;
18 |
19 | private static SqlMapClient gamedataSqlMapClient;
20 |
21 | public static void initAllSqlMapClient()throws Exception{
22 | //gamedataSqlMapClient=initSqlMapClient("ibatis-gamedata-config.xml");
23 | roledataSqlMapClient=initSqlMapClient("ibatis-roledata-config.xml");
24 | }
25 |
26 | private static SqlMapClient initSqlMapClient(String cfgName) throws Exception{
27 | InputStream in = null;
28 | SqlMapClient sqlMapClient = null;
29 | try{
30 | in = Resources.getResourceAsStream(cfgName);
31 | sqlMapClient = new SqlMapConfigParser().parse(in);
32 | }finally{
33 | try {
34 | in.close();
35 | } catch (IOException e) {
36 | e.printStackTrace();
37 | }
38 | }
39 | return sqlMapClient;
40 | }
41 |
42 | public static SqlMapClient getRoledataSqlMapClient() {
43 | return roledataSqlMapClient;
44 | }
45 |
46 | public static SqlMapClient getGamedataSqlMapClient() {
47 | return gamedataSqlMapClient;
48 | }
49 |
50 |
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/src/dbsqlmapper/roledata/t_user_SqlMap.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
21 |
22 |
23 | (
24 |
25 | $oredCriteria[].criteriaWithoutValue[]$
26 |
27 |
28 | $oredCriteria[].criteriaWithSingleValue[].condition$
29 | #oredCriteria[].criteriaWithSingleValue[].value#
30 |
31 |
32 | $oredCriteria[].criteriaWithListValue[].condition$
33 |
34 | #oredCriteria[].criteriaWithListValue[].values[]#
35 |
36 |
37 |
38 | $oredCriteria[].criteriaWithBetweenValue[].condition$
39 | #oredCriteria[].criteriaWithBetweenValue[].values[0]# and
40 | #oredCriteria[].criteriaWithBetweenValue[].values[1]#
41 |
42 | )
43 |
44 |
45 |
46 |
47 |
51 | select f_id, f_name, f_phonenumber, f_email, f_passwd, f_regdate
52 | from t_user
53 |
54 |
55 |
56 | order by $orderByClause$
57 |
58 |
59 |
60 |
61 |
65 | select f_id, f_name, f_phonenumber, f_email, f_passwd, f_regdate
66 | from t_user
67 | where f_id = #id:INTEGER#
68 |
69 |
70 |
74 | delete from t_user
75 | where f_id = #id:INTEGER#
76 |
77 |
78 |
82 | delete from t_user
83 |
84 |
85 |
86 |
90 | insert into t_user (f_id, f_name, f_phonenumber, f_email, f_passwd, f_regdate)
91 | values (#id:INTEGER#, #name:VARCHAR#, #phonenumber:VARCHAR#, #email:VARCHAR#, #passwd:VARCHAR#,
92 | #regdate:BIGINT#)
93 |
94 |
95 |
99 | insert into t_user
100 |
101 |
102 | f_id
103 |
104 |
105 | f_name
106 |
107 |
108 | f_phonenumber
109 |
110 |
111 | f_email
112 |
113 |
114 | f_passwd
115 |
116 |
117 | f_regdate
118 |
119 | )
120 |
121 | values
122 |
123 |
124 | #id:INTEGER#
125 |
126 |
127 | #name:VARCHAR#
128 |
129 |
130 | #phonenumber:VARCHAR#
131 |
132 |
133 | #email:VARCHAR#
134 |
135 |
136 | #passwd:VARCHAR#
137 |
138 |
139 | #regdate:BIGINT#
140 |
141 | )
142 |
143 |
144 |
145 | insert into t_user (f_id, f_name, f_phonenumber, f_email, f_passwd, f_regdate)
146 | values (#id:INTEGER#, #name:VARCHAR#, #phonenumber:VARCHAR#, #email:VARCHAR#, #passwd:VARCHAR#,
147 | #regdate:BIGINT#)
148 |
149 | SELECT @@IDENTITY AS ID
150 |
151 |
152 |
153 |
157 | select count(*) from t_user
158 |
159 |
160 |
161 |
165 | update t_user
166 |
167 |
168 | f_id = #record.id:INTEGER#
169 |
170 |
171 | f_name = #record.name:VARCHAR#
172 |
173 |
174 | f_phonenumber = #record.phonenumber:VARCHAR#
175 |
176 |
177 | f_email = #record.email:VARCHAR#
178 |
179 |
180 | f_passwd = #record.passwd:VARCHAR#
181 |
182 |
183 | f_regdate = #record.regdate:BIGINT#
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
195 | update t_user
196 | set f_id = #record.id:INTEGER#,
197 | f_name = #record.name:VARCHAR#,
198 | f_phonenumber = #record.phonenumber:VARCHAR#,
199 | f_email = #record.email:VARCHAR#,
200 | f_passwd = #record.passwd:VARCHAR#,
201 | f_regdate = #record.regdate:BIGINT#
202 |
203 |
204 |
205 |
206 |
207 |
211 | update t_user
212 |
213 |
214 | f_name = #name:VARCHAR#
215 |
216 |
217 | f_phonenumber = #phonenumber:VARCHAR#
218 |
219 |
220 | f_email = #email:VARCHAR#
221 |
222 |
223 | f_passwd = #passwd:VARCHAR#
224 |
225 |
226 | f_regdate = #regdate:BIGINT#
227 |
228 |
229 | where f_id = #id:INTEGER#
230 |
231 |
232 |
236 | update t_user
237 | set f_name = #name:VARCHAR#,
238 | f_phonenumber = #phonenumber:VARCHAR#,
239 | f_email = #email:VARCHAR#,
240 | f_passwd = #passwd:VARCHAR#,
241 | f_regdate = #regdate:BIGINT#
242 | where f_id = #id:INTEGER#
243 |
244 |
--------------------------------------------------------------------------------
/消息协议.xls:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yongzhidai/GameServer/fa521bb4bfa6554e3f0e1cc885689006825c3c89/消息协议.xls
--------------------------------------------------------------------------------