Struts2+MVC+MySql数据库增删查改及分页显示

本文介绍了使用Struts2框架结合MVC模式,利用MySQL数据库进行CRUD操作,并实现分页显示的功能。文章详细讲解了工程结构、信息列表展示、分页功能的实现,涉及c3p0连接池的使用,同时提供了Struts.xml配置和相关页面的编写过程。
摘要由CSDN通过智能技术生成

刚开始学习Java Web框架中的Struts2,暂且将数据库的增删查改以及分页显示做一总结,过程中使用的是MySQL数据库,其中用到了c3p0连接池,现在将编写过程分享给大家,第一次写,欢迎指正.

工程结构如下:

一.展示信息列表

(1)主页index.jsp只有一个超链接,跳转到展示页面show.jsp

<body>
	<a href="lend.action">展示学生列表</a>
</body>

(2)在struts.xml中配置lend.action
<!-- 展示 -->
<action name="lend" class="com.qf.action.StudentAction" method="list">
	<result>/show.jsp</result>
</action>
(3)编写StudentAction.java

private StudentService service = new StudentServiceImpl();
private Student student;
private List<Student> list;
public String list() throws Exception{
		setStudentList(service.getList());
		return Action.SUCCESS;
}

(4)还没有service层,实体类以及DAO层,所以新建StudentDao接口和StudentDaoImpl实现类,Student类

service接口:

public List<Student> getList() ;

service实现类:

private StudentDao dao = new StudentDaoImpl();
@Override
public List<Student> getList() {
	return dao.getList();
}


StudentDao接口:

public interface StudentDao {

	List<Student> getList() throws Exception;

}
StudentDaoImpl实现类中实现方法:
@Override
public List<Student> getList(int pageNow,int pageSize) throws Exception {
	QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
	String sql = "select * from student";
	List<Student> list = qr.query(sql, new BeanListHandler<Student>(Student.class));
	return list;
}

这其中用到了c3p0连接池,工具类如下:

public class DataSourceUtils {
	private static ComboPooledDataSource ds=new ComboPooledDataSource();
	//--
	private static ThreadLocal<Connection> tl=new ThreadLocal<>();

	public static DataSource getDataSource(){
		return ds;
	}

	public static Connection getConnection() throws SQLException{

		Connection conn = tl.get()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值