diff --git a/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/repository/DatabaseUserRepository.java b/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/repository/DatabaseUserRepository.java index 500724321..d5ffe63b7 100644 --- a/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/repository/DatabaseUserRepository.java +++ b/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/repository/DatabaseUserRepository.java @@ -43,7 +43,7 @@ private Connection getConnection() { @Override public boolean save(User user) { - return false; + return executeQuery(INSERT_USER_DML_SQL,resultSet -> {return true;}, COMMON_EXCEPTION_HANDLER); } @Override diff --git a/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/service/UserServiceImpl.java b/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/service/UserServiceImpl.java new file mode 100644 index 000000000..2b9b59c52 --- /dev/null +++ b/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/service/UserServiceImpl.java @@ -0,0 +1,48 @@ +package org.geektimes.projects.user.service; + +import org.geektimes.projects.user.domain.User; +import org.geektimes.projects.user.repository.DatabaseUserRepository; +import org.geektimes.projects.user.repository.UserRepository; +import org.geektimes.projects.user.sql.DBConnectionManager; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.util.Collection; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * @author zhimingxiao + * @since + */ +public class UserServiceImpl implements UserService{ + + + private UserRepository userRepository = new DatabaseUserRepository(new DBConnectionManager()); + + @Override + public boolean register(User user) { + return userRepository.save(user); + } + + + @Override + public boolean deregister(User user) { + return false; + } + + @Override + public boolean update(User user) { + return false; + } + + @Override + public User queryUserById(Long id) { + return null; + } + + @Override + public User queryUserByNameAndPassword(String name, String password) { + return null; + } +} diff --git a/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/sql/DBConnectionManager.java b/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/sql/DBConnectionManager.java index 3b2b0e25c..0757c0960 100644 --- a/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/sql/DBConnectionManager.java +++ b/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/sql/DBConnectionManager.java @@ -2,6 +2,10 @@ import org.geektimes.projects.user.domain.User; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; @@ -16,14 +20,29 @@ public class DBConnectionManager { private Connection connection; + + // JNDI 获取数据源 + private static DataSource dataSource; + public void setConnection(Connection connection) { this.connection = connection; } public Connection getConnection() { - return this.connection; + String databaseURL = "jdbc:derby:/db/user-platform;create=true"; + Connection connection = null; + try { + connection = DriverManager.getConnection(databaseURL); + this.connection = connection; + return this.connection; + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + return null; + } + public void releaseConnection() { if (this.connection != null) { try { diff --git a/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/web/controller/HelloWorldController.java b/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/web/controller/HelloWorldController.java index 2cf444985..b9aa4f0fb 100644 --- a/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/web/controller/HelloWorldController.java +++ b/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/web/controller/HelloWorldController.java @@ -15,7 +15,6 @@ public class HelloWorldController implements PageController { @GET - @POST @Path("/world") // /hello/world -> HelloWorldController public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable { return "index.jsp"; diff --git a/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/web/controller/UserController.java b/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/web/controller/UserController.java new file mode 100644 index 000000000..2a31acbff --- /dev/null +++ b/projects/stage-0/user-platform/user-web/src/main/java/org/geektimes/projects/user/web/controller/UserController.java @@ -0,0 +1,45 @@ +package org.geektimes.projects.user.web.controller; + +import org.geektimes.projects.user.domain.User; +import org.geektimes.projects.user.repository.DatabaseUserRepository; +import org.geektimes.projects.user.service.UserService; +import org.geektimes.projects.user.service.UserServiceImpl; +import org.geektimes.web.mvc.controller.PageController; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.Path; +import java.sql.Connection; + +/** + * @author zhimingxiao + * @since + */ +@Path("/user") +public class UserController implements PageController { + + private static final UserService userService; + + static { + userService = new UserServiceImpl(); + } + + + + @Override + @Path("/register") + public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable { + String name = request.getParameter("name"); + + String phoneNumber = request.getParameter("phoneNumber"); + String email = request.getParameter("email"); + String password = request.getParameter("password"); + User user = new User(); + user.setName(name); + user.setPassword(password); + user.setPhoneNumber(phoneNumber); + user.setEmail(email); + userService.register(new User()); + return "success.jsp"; + } +} diff --git a/projects/stage-0/user-platform/user-web/src/main/resources/META-INF/services/org.geektimes.web.mvc.controller.Controller b/projects/stage-0/user-platform/user-web/src/main/resources/META-INF/services/org.geektimes.web.mvc.controller.Controller index 609d9a29d..390733009 100644 --- a/projects/stage-0/user-platform/user-web/src/main/resources/META-INF/services/org.geektimes.web.mvc.controller.Controller +++ b/projects/stage-0/user-platform/user-web/src/main/resources/META-INF/services/org.geektimes.web.mvc.controller.Controller @@ -1 +1 @@ -org.geektimes.projects.user.web.controller.HelloWorldController \ No newline at end of file +org.geektimes.projects.user.web.controller.UserController \ No newline at end of file diff --git a/projects/stage-0/user-platform/user-web/src/main/webapp/WEB-INF/context.xml b/projects/stage-0/user-platform/user-web/src/main/webapp/WEB-INF/context.xml index bcfac3d55..d5dca0b28 100644 --- a/projects/stage-0/user-platform/user-web/src/main/webapp/WEB-INF/context.xml +++ b/projects/stage-0/user-platform/user-web/src/main/webapp/WEB-INF/context.xml @@ -8,4 +8,5 @@ driverClassName="org.apache.derby.jdbc.EmbeddedDriver" url="jdbc:derby:Databases/UserPlatformDB"/> + \ No newline at end of file diff --git a/projects/stage-0/user-platform/user-web/src/main/webapp/WEB-INF/web.xml b/projects/stage-0/user-platform/user-web/src/main/webapp/WEB-INF/web.xml index 0ada79e4f..ff9a8cc4d 100644 --- a/projects/stage-0/user-platform/user-web/src/main/webapp/WEB-INF/web.xml +++ b/projects/stage-0/user-platform/user-web/src/main/webapp/WEB-INF/web.xml @@ -85,11 +85,12 @@ /index.jsp - - - - - - + + DB Connection + jdbc/UserPlatformDB + javax.sql.DataSource + Container + + \ No newline at end of file diff --git a/projects/stage-0/user-platform/user-web/src/main/webapp/success.jsp b/projects/stage-0/user-platform/user-web/src/main/webapp/success.jsp new file mode 100644 index 000000000..7f6fdedda --- /dev/null +++ b/projects/stage-0/user-platform/user-web/src/main/webapp/success.jsp @@ -0,0 +1,11 @@ + + +My Home Page + + +
+ + Hello,World 2021 +
+ \ No newline at end of file