javaWeb实战教程
5.用户模块的开发
5.1 需求分析
用户可以注册、登录;登录完成的用户记录登录状态;用户下次访问自动使用cookie登录;
5.2 编码javaBean
在fy.test.model内新建User.java,按照数据库的字段编写javaBean:
public class User {
private int id;
private String username;
private String pwd;
private String cookie;
private String address;
private String tel;
private String zip;
private String realname;
}
使用eclipse自动生成getter、setter。
5.3 编码dao
在fy.test.dao内新建UserDao.java,根据需求,需要getById方法、getByName方法、add方法、updateCookie方法:
方法名 | 作用 |
---|---|
getByName | 用户登录时,使用用户名查找用户 |
add | 用户注册时新增用户 |
getById | 用户注册完成后根据id查找用户 |
updateCookie | 用户登录后要生成一个cookie,保存到数据库中 |
public void updateCookie(int userId, String cookie){}
public int add(String username, String pwd, String cookie){}
public User getById(int id){}
public User getByName(String username){}
在UserDao.java内新建全局变量private Connection connection;
,并生成set、get方法;connection的open、close由service管理,因为connection可以开启事务管理,通常事务管理都放在service层。
使用commons-dbutils-1.6.jar的QueryRunner来实现数据库操作:
public void updateCookie(int userId, String cookie) throws SQLException {
new QueryRunner().update(connection, "update user set cookie = ? where id= ?",
cookie, userId);
}
public int add(String username, String pwd, String cookie) throws SQLException {
return new QueryRunner().insert(connection,
"insert into user (username,pwd,cookie)values(?,?,?)",
new ScalarHandler<Long>(), username, pwd, cookie).intValue();
}
public User getById(int id) throws SQLException {
return new QueryRunner().query(connection, "select * from user where id=" + id,
new BeanHandler<>(User.class));
}
public User getByName(String username) throws SQLException {
return new QueryRunner().query(connection, "select * from user where username=?",
new BeanHandler<>(User.class), username);
}
dao里的每一个方法都抛出了SQLException,过去的实现是把这个错try{}catch{}掉,但在实际项目中,方法里的错误最好继续往上层抛,这样如果是因为数据库连接出错或sql语句出错,调用dao方法的人可以知道哪里出错,如果单纯try{}catch{}又不做任何处理,调用的人很难找到错误原因。向上层抛错的编程方法是java的一个特色。
在fy.test.test包内新建测试类UserDaoTest.java来测试一下刚刚写的dao方法是否正确:
@Before
public void before() {
userDao=new UserDao();
userDao.setConnection(DBUtil.getConnection());
}
@Test
public void test1(){
try {
int id=userDao.add("xiaoming", "000", "");
User user=userDao.getById(id);
System.out.println(user.getUsername());
user.setCookie(UUID.randomUUID().toString());
userDao.updateCookie(user.getId(), user.getCookie());
} catch (Exception e) {
e.printStackTrace();
}
}
5.4 编码service
在包fy.test.service内新建类UserService.java,根据需求创建如下方法:
方法名 | 作用 |
---|---|
login | 用户登录 |
register | 用户注册 |
getById | 根据id获得用户实例 |
public User login(String username, String pwd) {}
public User register(String username, String pwd, String repeatPwd) {}
public User getById(int id) {}
新建全局变量private UserDao userDao;
;在方法里要调用userDao前必须先初始化userDao,初始化userDao可以放在方法里、构造方法里、新建变量时,这里选择放在构造方法里:
public UserService() {
userDao = new UserDao();
}
5.4.1 用户登录-login
现在实现login方法,用户从浏览器提交表单,发送来用户名和密码字段,首先判断用户名、密码是否为空,这里在包fy.test.utils里新建一个工具类RegexUtil.java用于数据校验,在RegexUtil.java里写上判断字符串是否为空的静态方法:
public static boolean isEmpty(String data) {
if (data == null)
return true;
if (data.trim().length() == 0)
return true;
return false;
}
要判断密码是否大于6位,所以再写一个判断字符串长度的静态方法:
public static boolean checkSize(String data, int length) {
if (isEmpty(data))
return false;
if (data.length() <= length)
return false;
return true;
}
在login方法里写判断语句:
if (RegexUtil.isEmpty(username))
throw new RuntimeException("请填写用户名");
if (RegexUtil.checkSize(pwd, 6))
throw new RuntimeExce