注册功能_后台_Servlet代码实现与注册功能_后台_Dao代码实现
注册功能_后台_Servlet代码实现
1. 编写RegistUserServlet
package cn.itcast.travel.web.servlet; import cn.itcast.travel.domain.ResultInfo; import cn.itcast.travel.domain.User; import cn.itcast.travel.service.impl.UserServiceImpl; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; @WebServlet("/registUserServlet") public class RegistUserServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取数据 Map<String, String[]> map = request.getParameterMap(); //2.封装对象 User user = new User(); try { BeanUtils.populate(user,map); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } //3.调用service完成注册 UserServiceImpl service = new UserServiceImpl(); boolean flag = service.regist(user); ResultInfo info = new ResultInfo(); //4.响应结果 if (flag){ //注册成功 info.setFlag(true); }else { //注册失败 info.setFlag(false ); info.setErrorMsg("注册失败!"); } //将info对象序列化为json ObjectMapper mapper = new ObjectMapper(); mapper.writeValueAsString(info); //将inof数据写回客户端 //设置content-type response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
2.编写UserService以及UserServiceImpl
package cn.itcast.travel.service; import cn.itcast.travel.domain.User; public interface UserService { /** * 注册用户 * @param user * @return */ boolean regist(User user); }
package cn.itcast.travel.service.impl; import cn.itcast.travel.domain.User; import cn.itcast.travel.service.UserService; public class UserServiceImpl implements UserService { /** * 注册用户 * @param user * @return */ @Override public boolean regist(User user) { //1.根据用户名查询用户对象 //2.保存用户信息 return false; } }
注册功能_后台_Dao代码实现
1. 编写UserDao以及UserDaoImpl
package cn.itcast.travel.dao; import cn.itcast.travel.domain.User; public interface UserDao { /** * 根据用户名查询用户信息 * @param username * @return */ public User findByUsernam(String username); public void seve(User user); }
package cn.itcast.travel.dao.impl; import cn.itcast.travel.dao.UserDao; import cn.itcast.travel.domain.User; import cn.itcast.travel.util.JDBCUtils; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; public class UserDaoimpl implements UserDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); @Override public User findByUsernam(String username) { //1.定义sql String sql = "select * from tab_user where username =?"; //执行sql User user = template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class)); return user; } @Override public void seve(User user) { //定义sql String sql ="insert into user(username,password,name,birthday,sex,telephone,email) values(?,?,?,?,?,?,?)"; //执行sql template.update(sql,user.getUsername(), user.getPassword(), user.getName(), user.getBirthday(), user.getSex(), user.getTelephone(), user.getEmail()); } }