Skip to content

Commit 0980b66

Browse files
committed
Initial commit
0 parents  commit 0980b66

File tree

85 files changed

+13450
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+13450
-0
lines changed

src/com/longdi/dao/BookDao.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.longdi.dao;
2+
3+
import com.longdi.pojo.Book;
4+
5+
import java.util.List;
6+
7+
/**
8+
* @author: 龍弟
9+
* @description
10+
* @date: 2021/9/18 12:19
11+
*/
12+
public interface BookDao {
13+
public int addBook(Book book);
14+
public int deleteBookById(Integer id);
15+
public int updateBook(Book book);
16+
17+
public Book queryBookById(Integer id);
18+
public List<Book> queryBooks();
19+
20+
Integer queryForPageTotalCount();
21+
22+
List<Book> queryForPageItems(int begin, int pageSize);
23+
24+
List<Book> queryForPageItemsByPrice(int begin, int pageSize, int min, int max);
25+
26+
Integer queryForPageTotalCountByPrice(int min, int max);
27+
}

src/com/longdi/dao/OrderDao.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.longdi.dao;
2+
3+
import com.longdi.pojo.Order;
4+
5+
public interface OrderDao {
6+
7+
public int saveOrder(Order order);
8+
9+
}

src/com/longdi/dao/OrderItemDao.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.longdi.dao;
2+
3+
import com.longdi.pojo.OrderItem;
4+
5+
public interface OrderItemDao {
6+
public int saveOrderItem(OrderItem orderItem);
7+
}

src/com/longdi/dao/UserDao.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.longdi.dao;
2+
3+
import com.longdi.pojo.User;
4+
5+
/**
6+
* @author: 龍弟
7+
* @description
8+
* @date: 2021/9/16 18:40
9+
*/
10+
public interface UserDao {
11+
12+
/**
13+
* 根据用户名查询用户信息
14+
* @param username 用户名
15+
* @return 如果返回null,说明没有这个用户。反之亦然
16+
*/
17+
public User queryUserByUsername(String username);
18+
19+
/**
20+
* 根据 用户名和密码查询用户信息
21+
* @param username
22+
* @param password
23+
* @return 如果返回null,说明用户名或密码错误,反之亦然
24+
*/
25+
public User queryUserByUsernameAndPassword(String username, String password);
26+
27+
/**
28+
* 保存用户信息
29+
* @param user
30+
* @return 返回-1表示操作失败,其他是sql语句影响的行数
31+
*/
32+
public int saveUser(User user);
33+
34+
35+
}

src/com/longdi/dao/impl/BaseDao.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.longdi.dao.impl;
2+
3+
import com.longdi.utils.JdbcUtils;
4+
import org.apache.commons.dbutils.QueryRunner;
5+
import org.apache.commons.dbutils.handlers.BeanHandler;
6+
import org.apache.commons.dbutils.handlers.BeanListHandler;
7+
import org.apache.commons.dbutils.handlers.ScalarHandler;
8+
9+
import java.sql.Connection;
10+
import java.sql.SQLException;
11+
import java.util.List;
12+
13+
/**
14+
* @author: 龍弟
15+
* @description
16+
* @date: 2021/9/16 18:01
17+
*/
18+
public abstract class BaseDao {
19+
20+
//使用DbUtils操作数据库
21+
private QueryRunner queryRunner = new QueryRunner();
22+
23+
/**
24+
* update() 方法用来执行:Insert\Update\Delete语句
25+
*
26+
* @return 如果返回-1,说明执行失败<br/>返回其他表示影响的行数
27+
*/
28+
public int update(String sql, Object... args) {
29+
30+
System.out.println(" BaseDao 程序在[" +Thread.currentThread().getName() + "]中");
31+
32+
Connection connection = JdbcUtils.getConnection();
33+
try {
34+
return queryRunner.update(connection, sql, args);
35+
} catch (SQLException e) {
36+
e.printStackTrace();
37+
throw new RuntimeException(e);
38+
}
39+
}
40+
41+
/**
42+
* 查询返回一个javaBean的sql语句
43+
*
44+
* @param type 返回的对象类型
45+
* @param sql 执行的sql语句
46+
* @param args sql对应的参数值
47+
* @param <T> 返回的类型的泛型
48+
* @return
49+
*/
50+
public <T> T queryForOne(Class<T> type, String sql, Object... args) {
51+
Connection con = JdbcUtils.getConnection();
52+
try {
53+
return queryRunner.query(con, sql, new BeanHandler<T>(type), args);
54+
} catch (SQLException e) {
55+
e.printStackTrace();
56+
throw new RuntimeException(e);
57+
}
58+
}
59+
60+
/**
61+
* 查询返回多个javaBean的sql语句
62+
*
63+
* @param type 返回的对象类型
64+
* @param sql 执行的sql语句
65+
* @param args sql对应的参数值
66+
* @param <T> 返回的类型的泛型
67+
* @return
68+
*/
69+
public <T> List<T> queryForList(Class<T> type, String sql, Object... args) {
70+
Connection con = JdbcUtils.getConnection();
71+
try {
72+
return queryRunner.query(con, sql, new BeanListHandler<T>(type), args);
73+
} catch (SQLException e) {
74+
e.printStackTrace();
75+
throw new RuntimeException(e);
76+
}
77+
}
78+
79+
/**
80+
* 执行返回一行一列的sql语句
81+
* @param sql 执行的sql语句
82+
* @param args sql对应的参数值
83+
* @return
84+
*/
85+
public Object queryForSingleValue(String sql, Object... args){
86+
87+
Connection conn = JdbcUtils.getConnection();
88+
89+
try {
90+
return queryRunner.query(conn, sql, new ScalarHandler(), args);
91+
} catch (SQLException e) {
92+
e.printStackTrace();
93+
throw new RuntimeException(e);
94+
}
95+
96+
}
97+
98+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.longdi.dao.impl;
2+
3+
import com.longdi.dao.BookDao;
4+
import com.longdi.pojo.Book;
5+
6+
import java.util.List;
7+
8+
/**
9+
* @author: 龍弟
10+
* @description
11+
* @date: 2021/9/18 12:23
12+
*/
13+
public class BookDaoImpl extends BaseDao implements BookDao {
14+
@Override
15+
public int addBook(Book book) {
16+
17+
String sql = "insert into t_book(`name`,`author`,`price`,`sales`,`stock`,`img_path`) values(?,?,?,?,?,?)";
18+
19+
return update(sql, book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath());
20+
21+
}
22+
23+
@Override
24+
public int deleteBookById(Integer id) {
25+
String sql = "delete from t_book where id = ?";
26+
return update(sql, id);
27+
}
28+
29+
@Override
30+
public int updateBook(Book book) {
31+
String sql = "update t_book set `name`=?,`author`=?,`price`=?,`sales`=?,`stock`=?,`img_path`=? where id = ?";
32+
return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath(),book.getId());
33+
}
34+
35+
@Override
36+
public Book queryBookById(Integer id) {
37+
String sql = "select `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` imgPath from t_book where id = ?";
38+
return queryForOne(Book.class, sql,id);
39+
}
40+
41+
@Override
42+
public List<Book> queryBooks() {
43+
String sql = "select `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` imgPath from t_book";
44+
return queryForList(Book.class, sql);
45+
}
46+
47+
48+
@Override
49+
public Integer queryForPageTotalCount() {
50+
String sql = "select count(*) from t_book";
51+
Number count = (Number) queryForSingleValue(sql);
52+
return count.intValue();
53+
}
54+
55+
@Override
56+
public List<Book> queryForPageItems(int begin, int pageSize) {
57+
String sql = "select `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` imgPath from t_book limit ?,?";
58+
return queryForList(Book.class,sql,begin,pageSize);
59+
}
60+
61+
@Override
62+
public Integer queryForPageTotalCountByPrice(int min, int max) {
63+
String sql = "select count(*) from t_book where price between ? and ?";
64+
Number count = (Number) queryForSingleValue(sql,min,max);
65+
return count.intValue();
66+
}
67+
68+
@Override
69+
public List<Book> queryForPageItemsByPrice(int begin, int pageSize, int min, int max) {
70+
String sql = "select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` imgPath " +
71+
"from t_book where price between ? and ? order by price limit ?,?";
72+
return queryForList(Book.class,sql,min,max,begin,pageSize);
73+
}
74+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.longdi.dao.impl;
2+
3+
import com.longdi.dao.OrderDao;
4+
import com.longdi.pojo.Order;
5+
6+
public class OrderDaoImpl extends BaseDao implements OrderDao {
7+
@Override
8+
public int saveOrder(Order order) {
9+
String sql = "insert into t_order(`order_id`,`create_time`,`price`,`status`,`user_id`) values(?,?,?,?,?)";
10+
11+
return update(sql,order.getOrderId(),order.getCreateTime(),order.getPrice(),order.getStatus(),order.getUserId());
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.longdi.dao.impl;
2+
3+
import com.longdi.dao.OrderItemDao;
4+
import com.longdi.pojo.OrderItem;
5+
6+
public class OrderItemDaoImpl extends BaseDao implements OrderItemDao {
7+
@Override
8+
public int saveOrderItem(OrderItem orderItem) {
9+
String sql = "insert into t_order_item(`name`,`count`,`price`,`total_price`,`order_id`) values(?,?,?,?,?)";
10+
return update(sql,orderItem.getName(),orderItem.getCount(),orderItem.getPrice(),orderItem.getTotalPrice(),orderItem.getOrderId());
11+
}
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.longdi.dao.impl;
2+
3+
import com.longdi.dao.UserDao;
4+
import com.longdi.pojo.User;
5+
6+
/**
7+
* @author: 龍弟
8+
* @description
9+
* @date: 2021/9/16 18:44
10+
*/
11+
public class UserDaoImpl extends BaseDao implements UserDao {
12+
@Override
13+
public User queryUserByUsername(String username) {
14+
String sql = "select `id`,`username`,`password`,`email` from t_user where username = ?";
15+
return queryForOne(User.class, sql, username);
16+
}
17+
18+
@Override
19+
public User queryUserByUsernameAndPassword(String username, String password) {
20+
String sql = "select `id`,`username`,`password`,`email` from t_user where username = ? and password = ?";
21+
return queryForOne(User.class, sql, username,password);
22+
}
23+
24+
@Override
25+
public int saveUser(User user) {
26+
String sql = "insert into t_user(`username`,`password`,`email`) values(?,?,?)";
27+
return update(sql, user.getUsername(),user.getPassword(),user.getEmail());
28+
}
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.longdi.filter;
2+
3+
import javax.servlet.*;
4+
import javax.servlet.http.HttpServletRequest;
5+
import java.io.IOException;
6+
7+
/**
8+
* @author: 龍弟
9+
* @description
10+
* @date: 2021/9/21 21:04
11+
*/
12+
public class ManagerFilter implements Filter {
13+
14+
@Override
15+
public void init(FilterConfig filterConfig) throws ServletException {
16+
17+
}
18+
19+
@Override
20+
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
21+
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
22+
23+
Object user = httpServletRequest.getSession().getAttribute("user");
24+
25+
if (user == null) {
26+
httpServletRequest.getRequestDispatcher("/pages/user/login.jsp").forward(servletRequest,servletResponse);
27+
} else {
28+
filterChain.doFilter(servletRequest,servletResponse);
29+
}
30+
}
31+
32+
@Override
33+
public void destroy() {
34+
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.longdi.filter;
2+
3+
import com.longdi.utils.JdbcUtils;
4+
5+
import javax.servlet.*;
6+
import java.io.IOException;
7+
8+
public class TransactionFilter implements Filter {
9+
10+
@Override
11+
public void init(FilterConfig filterConfig) throws ServletException {
12+
13+
}
14+
15+
@Override
16+
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
17+
try {
18+
filterChain.doFilter(servletRequest,servletResponse);
19+
JdbcUtils.commitAndClose();// 提交事务
20+
} catch (Exception e) {
21+
JdbcUtils.rollbackAndClose();//回滚事务
22+
e.printStackTrace();
23+
throw new RuntimeException(e);//把异常抛给Tomcat管理展示友好的错误页面
24+
}
25+
}
26+
27+
@Override
28+
public void destroy() {
29+
30+
}
31+
}

0 commit comments

Comments
 (0)