Skip to content

Commit

Permalink
version 0.3.1 update
Browse files Browse the repository at this point in the history
修复多用户同时注册时可能造成用户名重复的bug;新增UserDao的SpringJDBC模板类实现
  • Loading branch information
ZongXR committed Sep 28, 2020
1 parent 24f63b1 commit 5645023
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 5 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
<li>未经许可不得将本项目商用,如需商用请联系作者<a href="mailto:zxr@tju.edu.cn">zxr@tju.edu.cn</a></li>
</ul>
<h2>关键技术</h2>
<p>基于SSM框架开发</p>
<p>基于SSM框架开发的商城系统,代码严格遵循MVC分层思想,可部署到服务器上,设计精良,不断完善</p>
<h3>前端</h3>
<ul>
<li>前端对应View层</li>
<li>前端使用jsp页面展示,并通过EL表达式接收服务器打给浏览器的数据</li>
<li>使用js实现网页动态效果,通过jQuery简化操作,并配合html5实现前端的数据校验</li>
<li>使用Ajax技术完成前端用户名可用性校验、退出登录功能</li>
Expand Down Expand Up @@ -152,6 +153,16 @@
</td>
<td>2020年9月25日</td>
</tr>
<tr>
<td>0.3.1</td>
<td>
<ul>
<li>新增UserDao的SpringJDBC模板类实现</li>
<li>注册用户功能开启事务,修复多个用户同时注册时可能造成用户名重复的bug</li>
</ul>
</td>
<td>2020年9月29日</td>
</tr>
</table>
<h2>配置情况</h2>
<ul>
Expand Down
5 changes: 4 additions & 1 deletion src/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
</bean>
<!-- 开启注解事务管理-->
<tx:annotation-driven/>

<!-- 配置JDBC模板类 -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate" >
<property name="dataSource" ref="dataSource" />
</bean>

</beans>
14 changes: 14 additions & 0 deletions src/com/supermarket/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public interface UserDao {
*/
public User queryUser(User user);

/**
* 查找好多用户
* @param user bean
* @return 用户组成的List
*/
public List<? extends User> queryUsers(User user);

/**
* 用map查询用户
*
Expand All @@ -26,6 +33,13 @@ public interface UserDao {
*/
public User queryUser(Map<String, Object> map);

/**
* 查询很多用户
* @param map map
* @return 很多用户
*/
public List<? extends User> queryUsers(Map<String, Object> map);

/**
* 插入用户
*
Expand Down
109 changes: 109 additions & 0 deletions src/com/supermarket/dao/UserJDBC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.supermarket.dao;

import com.supermarket.domain.User;
import com.supermarket.domain.UserImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class UserJDBC implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate = null;

@Override
public User queryUser(User user) {
List<? extends User> users = this.queryUsers(user);
if (users.isEmpty())
return null;
else
return users.get(0);
}

@Override
public List<? extends User> queryUsers(User user) {
if (user == null)
return new LinkedList<>();
String sql = "select * from user where 1 = 1";
List<Object> args = new LinkedList<Object>();
if (user.getId() != 0) {
sql = sql + " and id = ?";
args.add(user.getId());
}
if (user.getUsername() != null) {
sql = sql + " and username = ?";
args.add(user.getUsername());
}
if (user.getPassword() != null) {
sql = sql + " and password = ?";
args.add(user.getPassword());
}
if (user.getNickname() != null) {
sql = sql + " and nickname = ?";
args.add(user.getNickname());
}
if (user.getEmail() != null) {
sql = sql + " and email = ?";
args.add(user.getEmail());
}
List<? extends User> users = this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UserImpl.class), args.toArray());
return users;
}

@Override
public User queryUser(Map<String, Object> map) {
List<? extends User> users = this.queryUsers(map);
if (users.isEmpty()){
return null;
}else {
return users.get(0);
}
}

@Override
public List<? extends User> queryUsers(Map<String, Object> map) {
if (map == null)
return new LinkedList<>();
String sql = "select * from user where 1 = 1";
List<Object> args = new LinkedList<Object>();
if (map.get("id") != null) {
sql = sql + " and id = ?";
args.add(map.get("id"));
}
if (map.get("username") != null) {
sql = sql + " and username = ?";
args.add(map.get("username"));
}
if (map.get("password") != null) {
sql = sql + " and password = ?";
args.add(map.get("password"));
}
if (map.get("nickname") != null) {
sql = sql + " and nickname = ?";
args.add(map.get("nickname"));
}
if (map.get("email") != null) {
sql = sql + " and email = ?";
args.add(map.get("email"));
}
List<? extends User> users = this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UserImpl.class), args.toArray());
return users;
}

@Override
public void insertUser(User user) {
if (user == null)
return;
this.jdbcTemplate.update(
"insert into user (id, username, password, nickname, email) values (?, ?, ?, ?, ?)",
user.getId(),
user.getUsername(),
user.getPassword(),
user.getNickname(),
user.getEmail()
);
}
}
12 changes: 12 additions & 0 deletions src/com/supermarket/exception/MsgRollbackException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.supermarket.exception;

public class MsgRollbackException extends MsgException{
/**
* 韩餐构造方法
*
* @param message 在网页中的提示信息
*/
public MsgRollbackException(String message) {
super(message);
}
}
11 changes: 11 additions & 0 deletions src/com/supermarket/mapper/UserMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
</where>
</select>

<select id="queryUsers" resultType="com.supermarket.domain.UserImpl">
select * from user
<where>
<if test="id != 0 and id != null">id = #{id}</if>
<if test="username != null">and username = #{username}</if>
<if test="password != null">and password = #{password}</if>
<if test="nickname != null">and nickname = #{nickname}</if>
<if test="email != null">and email = #{email}</if>
</where>
</select>

<insert id="insertUser">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
Expand Down
11 changes: 9 additions & 2 deletions src/com/supermarket/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.supermarket.domain.User;
import com.supermarket.domain.UserImpl;
import com.supermarket.exception.MsgException;
import com.supermarket.exception.MsgRollbackException;
import com.supermarket.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -35,7 +36,7 @@ public User login(User user, String valistr, String valicode) throws MsgExceptio
return user;
}

@Transactional
@Transactional(rollbackOn = {RuntimeException.class, MsgRollbackException.class})
@Override
public User regist(
@Valid User user,
Expand Down Expand Up @@ -63,13 +64,19 @@ public User regist(
throw new MsgException("用户名已被注册");
// 通过以上校验,插入数据
this.userDao.insertUser(user);
// 检查数据库该用户名只有一个
Map<String, Object> map = new HashMap<String, Object>();
map.put("username", user.getUsername());
List<? extends User> users = this.userDao.queryUsers(map);
if (users.size() > 1)
throw new MsgRollbackException("用户名已被注册");
return user;
}

@Override
public boolean isUsernameAvailable(String username) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("username", username);
return this.userDao.queryUser(map) == null;
return this.userDao.queryUsers(map).size() == 0;
}
}
5 changes: 4 additions & 1 deletion web/WEB-INF/classes/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
</bean>
<!-- 开启注解事务管理-->
<tx:annotation-driven/>

<!-- 配置JDBC模板类 -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate" >
<property name="dataSource" ref="dataSource" />
</bean>

</beans>
11 changes: 11 additions & 0 deletions web/WEB-INF/classes/com/supermarket/mapper/UserMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
</where>
</select>

<select id="queryUsers" resultType="com.supermarket.domain.UserImpl">
select * from user
<where>
<if test="id != 0 and id != null">id = #{id}</if>
<if test="username != null">and username = #{username}</if>
<if test="password != null">and password = #{password}</if>
<if test="nickname != null">and nickname = #{nickname}</if>
<if test="email != null">and email = #{email}</if>
</where>
</select>

<insert id="insertUser">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
Expand Down

0 comments on commit 5645023

Please sign in to comment.