Skip to content

Commit

Permalink
Add lend
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghuanhao committed Feb 13, 2019
1 parent 9f7f969 commit 8e28bff
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 75 deletions.
65 changes: 25 additions & 40 deletions src/main/java/com/library/dao/LendDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class LendDao {
Expand All @@ -27,55 +27,40 @@ public class LendDao {
private final static String MY_LEND_LIST_SQL = "SELECT * FROM lend_list WHERE reader_id = ? ";
private final static String DELETE_LEND_SQL = "DELETE FROM lend_list WHERE ser_num = ?";

public int returnBookOne(long bookId, long readerId) {
return jdbcTemplate.update(RETURN_BOOK_ONE_SQL, df.format(new Date()), bookId, readerId);
public int returnBookOne(final long book_id, long reader_id) {
Map<String, Object> map = new HashMap<>();
map.put("book_id", book_id);
map.put("reader_id", reader_id);
//map.put("back_date", df.format(new Date()));
return sqlSessionTemplate.update(NAMESPACE + "returnBookOne", map);
}

public int returnBookTwo(long bookId) {
return jdbcTemplate.update(RETURN_BOOK_TWO_SQL, bookId);
public int returnBookTwo(final long book_id) {
return sqlSessionTemplate.update(NAMESPACE + "returnBookTwo", book_id);
}

public int lendBookOne(long bookId, long readerId) {
return jdbcTemplate.update(LEND_BOOK_ONE_SQL, bookId, readerId, df.format(new Date()));
public int lendBookOne(final long book_id, final long reader_id) {
Map<String, Object> map = new HashMap<>();
map.put("book_id", book_id);
map.put("reader_id", reader_id);
return sqlSessionTemplate.insert(NAMESPACE + "lendBookOne", map);
}

public int lendBookTwo(long bookId) {
return jdbcTemplate.update(LEND_BOOK_TWO_SQL, bookId);
}

private Lend getLendFromResult(ResultSet resultSet) throws SQLException {
Lend lend = new Lend();
lend.setBackDate(resultSet.getDate("back_date"));
lend.setBookId(resultSet.getLong("book_id"));
lend.setLendDate(resultSet.getDate("lend_date"));
lend.setReaderId(resultSet.getInt("reader_id"));
lend.setSer_num(resultSet.getLong("ser_num"));
return lend;
public int lendBookTwo(final long book_id) {
return sqlSessionTemplate.update(NAMESPACE + "lendBookTwo", book_id);
}

public ArrayList<Lend> lendList() {
final ArrayList<Lend> list = new ArrayList<>();
jdbcTemplate.query(LEND_LIST_SQL, (resultSet) -> {
resultSet.beforeFirst();
while (resultSet.next()) {
list.add(getLendFromResult(resultSet));
}
});
return list;
List<Lend> result = sqlSessionTemplate.selectList(NAMESPACE + "lendList");
return (ArrayList<Lend>) result;
}

public ArrayList<Lend> myLendList(long readerId) {
final ArrayList<Lend> list = new ArrayList<>();
jdbcTemplate.query(MY_LEND_LIST_SQL, new Object[]{readerId}, (resultSet) -> {
resultSet.beforeFirst();
while (resultSet.next()) {
list.add(getLendFromResult(resultSet));
}
});
return list;
public ArrayList<Lend> myLendList(final long reader_id) {
List<Lend> result = sqlSessionTemplate.selectList(NAMESPACE + "myLendList", reader_id);
return (ArrayList<Lend>) result;
}

public int deleteLend(long serNum) {
return jdbcTemplate.update(DELETE_LEND_SQL, serNum);
public int deleteLend(final long ser_num) {
return sqlSessionTemplate.delete(NAMESPACE + "deleteLend", ser_num);
}
}
4 changes: 2 additions & 2 deletions src/main/resources/MyBatis/admin.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?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.AdminDao">
<select id="getMatchCount" resultType="int" parameterType="com.library.bean.Admin">
<select id="getMatchCount" resultType="int">
select count(*) from admin
where admin_id = #{admin_id}
and password = #{password}
</select>
<update id="resetPassword" parameterType="com.library.bean.Admin">
<update id="resetPassword">
update admin set password = #{password}
where admin_id = #{admin_id}
</update>
Expand Down
51 changes: 18 additions & 33 deletions src/main/resources/MyBatis/lend.xml
Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
<?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.LendDao">

private final static String RETURN_BOOK_ONE_SQL = "UPDATE lend_list SET back_date = ? WHERE book_id = ? AND
reader_id = ? AND back_date is NULL";
private final static String RETURN_BOOK_TWO_SQL = "UPDATE book_info SET number = number + 1 WHERE book_id = ? ";
private final static String LEND_BOOK_ONE_SQL = "INSERT INTO lend_list VALUES (NULL , ? , ? , ? , NULL)";
private final static String LEND_BOOK_TWO_SQL = "UPDATE book_info SET number = number - 1 WHERE book_id = ? ";
private final static String LEND_LIST_SQL = "SELECT * FROM lend_list";
private final static String MY_LEND_LIST_SQL = "SELECT * FROM lend_list WHERE reader_id = ? ";
private final static String DELETE_LEND_SQL = "DELETE FROM lend_list WHERE ser_num = ?";

<update id="returnBookOne" parameterType="com.library.bean.Book">
update lend_list set back_date = ? where book_id = ? AND reader_id = ? AND back_date is NULL";
<update id="returnBookOne">
update lend_list set back_date = sysdate() where book_id = #{book_id}
and reader_id = #{reader_id} and back_date is null
</update>


<update id="editBook" parameterType="com.library.bean.Book">
update book_info set name=#{name}, author=#{author},
publish=#{publish}, ISBN=#{ISBN}, introduction=#{introduction},
language=#{language}, price=#{price}, pub_date=#{pub_date},
class_id=#{class_id}, number=#{number} where book_id=#{book_id}
<update id="returnBookTwo" parameterType="long">
update book_info set number = number + 1 where book_id = #{book_id}
</update>
<select id="getAllBooks" resultType="com.library.bean.Book">
select * from book_info
</select>
<select id="queryBook" resultType="com.library.bean.Book" parameterType="String">
select * from book_info where name like #{search}
or author like #{search} or introduction like #{search}
</select>
<select id="matchBook" resultType="int" parameterType="String">
select count(*) from book_info where name like #{search}
or author like #{search} or introduction like #{search}
<insert id="lendBookOne">
insert into lend_list values
(null , #{book_id} , #{reader_id} , sysdate() , null)
</insert>
<update id="lendBookTwo" parameterType="long">
update book_info set number = number - 1 where book_id = #{book_id}
</update>
<select id="lendList" resultType="com.library.bean.Lend">
select * from lend_list
</select>
<select id="getBook" resultType="com.library.bean.Book" parameterType="long">
select * from book_info where book_id = #{book_id}
<select id="myLendList" resultType="com.library.bean.Lend" parameterType="long">
select * from lend_list where reader_id = #{reader_id}
</select>
<delete id="deleteBook" parameterType="long">
delete from book_info where book_id = #{book_id}
<delete id="deleteLend" parameterType="long">
delete from lend_list where ser_num = #{ser_num}
</delete>
</mapper>

0 comments on commit 8e28bff

Please sign in to comment.