刚开始学习Java Web框架中的Struts2,暂且将数据库的增删查改以及分页显示做一总结,过程中使用的是MySQL数据库,其中用到了c3p0连接池,现在将编写过程分享给大家,第一次写,欢迎指正.
工程结构如下:
一.展示信息列表
(1)主页index.jsp只有一个超链接,跳转到展示页面show.jsp
<body> <a href="lend.action">展示学生列表</a> </body>
(2)在struts.xml中配置lend.action(3)编写StudentAction.java<!-- 展示 --> <action name="lend" class="com.qf.action.StudentAction" method="list"> <result>/show.jsp</result> </action>
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接口:
StudentDaoImpl实现类中实现方法:public interface StudentDao { List<Student> getList() throws Exception; }
@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()
Struts2+MVC+MySql数据库增删查改及分页显示
最新推荐文章于 2021-02-06 14:20:37 发布
本文介绍了使用Struts2框架结合MVC模式,利用MySQL数据库进行CRUD操作,并实现分页显示的功能。文章详细讲解了工程结构、信息列表展示、分页功能的实现,涉及c3p0连接池的使用,同时提供了Struts.xml配置和相关页面的编写过程。
摘要由CSDN通过智能技术生成