一.在pom.xml中导入依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yztc</groupId> <artifactId>Request</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>Request Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.31</version> </dependency> <!--mysql数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency> <!--数据库连接池--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>mchange-commons-java</artifactId> <version>0.2.12</version> </dependency> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.2</version> </dependency> </dependencies> <build> <finalName>Request</finalName> </build> </project>
二.实体类entity
package com.yztc.request.entity; public class User { private String uid; private String username; private String password; private String name; private String email; private String sex; private String birthday; public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } @Override public String toString() { return "User [uid=" + uid + ", username=" + username + ", password=" + password + ", name=" + name + ", email=" + email + ", sex=" + sex + ", birthday=" + birthday + "]"; } }
三.Dao层接口和实现类
package com.yztc.request.dao; import com.yztc.request.entity.User; import com.yztc.request.until.DbManager; import org.apache.commons.dbutils.QueryRunner; import java.sql.SQLException; /** * _ooOoo_ * o8888888o * 88" . "88 * (| -_- |) * O = /O * ___/`---'\____ * . ' \| |// `. * / \||| : |||// * / _||||| -:- |||||- * | | \ - /// | | * | \_| ''---/'' | | * .-\__ `-` ___/-. / * ___`. .' /--.-- `. . __ * ."" '< `.___\_<|>_/___.' >'"". * | | : `- \`.;` _ /`;.`/ - ` : | | * `-. \_ __ /__ _/ .-` / / * ======`-.____`-.___\_____/___.-`____.-'====== * `=---=' * ............................................. * * @author bindu * @date 2017-12-09 14:54 */ public class UserDaoImpl implements UserDao { @Override public void regist(User user) throws SQLException { String sql = "insert into user values(?,?,?,?,?,?,?)"; QueryRunner queryRunner = DbManager.getInstance().getQuery(); queryRunner.update(sql,user.getUid(),user.getUsername(),user.getPassword(),user.getName(), user.getEmail(),user.getSex(),user.getBirthday()); } }
四.controller层
package com.yztc.request.controller; import com.yztc.request.dao.UserDaoImpl; import com.yztc.request.entity.User; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; import java.util.Map; import java.util.UUID; /** * _ooOoo_ * o8888888o * 88" . "88 * (| -_- |) * O = /O * ___/`---'\____ * . ' \| |// `. * / \||| : |||// * / _||||| -:- |||||- * | | \ - /// | | * | \_| ''---/'' | | * .-\__ `-` ___/-. / * ___`. .' /--.-- `. . __ * ."" '< `.___\_<|>_/___.' >'"". * | | : `- \`.;` _ /`;.`/ - ` : | | * `-. \_ __ /__ _/ .-` / / * ======`-.____`-.___\_____/___.-`____.-'====== * `=---=' * ............................................. * * @author bindu * @date 2017-12-09 15:13 */ public class RegisterServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Map<String, String[]> properties = req.getParameterMap(); User user = new User(); try { BeanUtils.populate(user,properties); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } user.setUid(UUID.randomUUID().toString()); UserDaoImpl userDao = new UserDaoImpl(); try { userDao.regist(user); } catch (SQLException e) { e.printStackTrace(); } resp.sendRedirect(req.getContextPath()+"/login.jsp"); } }
五.配置web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>first</servlet-name> <!--包名+类型--> <servlet-class>com.yztc.request.controller.RegisterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>first</servlet-name> <url-pattern>/register</url-pattern> </servlet-mapping> </web-app>
工具包: DbManager
package com.yztc.request.until; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.apache.commons.dbutils.QueryRunner; import java.sql.Connection; import java.sql.SQLException; /** * 加载驱动 * 建立连接 * 执行sql的对象 **/ public class DbManager { // 数据库连接核心类 static ComboPooledDataSource dataSource; static { dataSource = new ComboPooledDataSource(); } private static volatile DbManager instance = null; private DbManager() { } public static DbManager getInstance() { if (instance == null) { synchronized (DbManager.class) { if (instance == null) { instance = new DbManager(); } } } return instance; } public Connection getConnection() throws SQLException { return dataSource.getConnection(); } /** * DbUtils * QueryRunner * ResultSetHandler */ public QueryRunner getQuery() { return new QueryRunner(dataSource); } }
数据库连接文件: c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/request?useUnicode=true&characterEncoding=UTF-8 </property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">ROOT</property> <property name="checkoutTimeout">30000</property> <property name="idleConnectionTestPeriod">30</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements">200</property> </default-config> </c3p0-config>
至于前端页面,需要自己写了!