Skip to content

Commit

Permalink
Add readerCard.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghuanhao committed Feb 13, 2019
1 parent f83f399 commit 59fdfaa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
59 changes: 31 additions & 28 deletions src/main/java/com/library/dao/ReaderCardDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,49 @@

import com.library.bean.ReaderCard;
import com.library.bean.ReaderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Repository
public class ReaderCardDao {
@Autowired
private JdbcTemplate jdbcTemplate;
private final static String MATCH_READER_ID_SQL = "select count(*) from reader_card where reader_id = ? and password = ? ";
private final static String GET_PASSWORD_SQL = "select password from reader_card where reader_id = ? ";
private final static String FIND_READER_BY_USERID = "select * from reader_card where reader_id = ? ";
private final static String RE_PASSWORD_SQL = "UPDATE reader_card set password = ? where reader_id = ? ";
private final static String ADD_READERCARD_SQL = "INSERT INTO reader_card values ( ? , ? , ?)";

public int getIdMatchCount(long readerId, String password) {
return jdbcTemplate.queryForObject(MATCH_READER_ID_SQL, new Object[]{readerId, password}, Integer.class);

private final static String NAMESPACE = "com.library.dao.ReaderCardDao.";
@Resource
private SqlSessionTemplate sqlSessionTemplate;

public int getIdMatchCount(final long reader_id, final String password) {
Map<String, Object> map = new HashMap<>();
map.put("reader_id", reader_id);
map.put("password", password);
return sqlSessionTemplate.selectOne(NAMESPACE + "getIdMatchCount", map);
}

public ReaderCard findReaderByReaderId(long readerId) {
final ReaderCard readerCard = new ReaderCard();
jdbcTemplate.query(FIND_READER_BY_USERID, new Object[]{readerId},
(resultSet) -> {
readerCard.setReaderId(resultSet.getInt("reader_id"));
readerCard.setPassword(resultSet.getString("password"));
readerCard.setName(resultSet.getString("username"));
});
return readerCard;
public ReaderCard findReaderByReaderId(final long reader_id) {
return sqlSessionTemplate.selectOne(NAMESPACE + "findReaderByReaderId");
}

public int resetPassword(long readerId, String newPassword) {
return jdbcTemplate.update(RE_PASSWORD_SQL, newPassword, readerId);
public int resetPassword(final long reader_id, final String newPassword) {
Map<String, Object> map = new HashMap<>();
map.put("reader_id", reader_id);
map.put("password", newPassword);
return sqlSessionTemplate.update(NAMESPACE + "resetPassword", map);
}

public int addReaderCard(ReaderInfo readerInfo,String password) {
public int addReaderCard(final ReaderInfo readerInfo, final String password) {
String username = readerInfo.getName();
long readerId = readerInfo.getReaderId();
return jdbcTemplate.update(ADD_READERCARD_SQL, readerId, username, password);
long reader_id = readerInfo.getReaderId();
Map<String, Object> map = new HashMap<>();
map.put("reader_id", reader_id);
map.put("username", username);
map.put("password", password);
return sqlSessionTemplate.update(NAMESPACE + "addReaderCard", map);
}

public String getPassword(long readerId) {
return jdbcTemplate.queryForObject(GET_PASSWORD_SQL, new Object[]{readerId}, String.class);
public String getPassword(final long reader_id) {
return sqlSessionTemplate.selectOne(NAMESPACE + "getPassword", reader_id);
}
}
22 changes: 22 additions & 0 deletions src/main/resources/MyBatis/readerCard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.library.dao.ReaderCardDao">
<select id="getIdMatchCount" resultType="int">
select count(*) from reader_card where
reader_id = #{reader_id} and password = #{password}
</select>
<select id="findReaderByReaderId" resultType="com.library.bean.ReaderCard" parameterType="long">
select * from reader_card where reader_id = #{reader_id}
</select>
<update id="resetPassword">
update reader_card set password = #{password}
where reader_id = #{reader_id}
</update>
<select id="getPassword" resultType="String" parameterType="long">
select password from reader_card where reader_id = #{reader_id}
</select>
<insert id="addReaderCard">
insert into reader_card values
(#{reader_id}, #{username}, #{password})
</insert>
</mapper>

0 comments on commit 59fdfaa

Please sign in to comment.