利用SpringMvc和MyBatis实现员工列表显示页面
2016-08-21 14:02
441 查看
利用SpringMvc和MyBatis实现员工列表显示页面
首先使用MapperScannerConfigurer将带有@MyBatisRespository注解的Dao接口扫描生成MapperFacotryBean实例
然后通过MapperFactoryBean生成Dao实例给SpringMvc的Controller对象注入。
最后Controller将DAO查询返回的记录传递到Jsp显示
步骤
1:建工程,导包
2:新建Emp实体类和EmpMapper.xml映射文件
3:注解接口MyBatisRepository
4:新建Dao接口EmpDao
5:新建spring配置文件
6:新建测试类
7:运行测试类
8:新建EmpListController
8:新建jsp
测试
http://localhost:8080/SMMvcTest/emp/list.form
首先使用MapperScannerConfigurer将带有@MyBatisRespository注解的Dao接口扫描生成MapperFacotryBean实例
然后通过MapperFactoryBean生成Dao实例给SpringMvc的Controller对象注入。
最后Controller将DAO查询返回的记录传递到Jsp显示
步骤
1:建工程,导包
2:新建Emp实体类和EmpMapper.xml映射文件
package com.eduask.mvc.entity; import java.util.Date; public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Double sal; private Double comm; private Integer deptno; public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public Double getComm() { return comm; } public void setComm(Double comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } } |
3:注解接口MyBatisRepository
|
package com.eduask.mvc.annotation; import org.springframework.stereotype.Repository; @Repository public @interface MyBatisRepository { String value() default ""; } |
4:新建Dao接口EmpDao
|
package com.eduask.mvc.dao; import java.util.List; import com.eduask.mvc.annotation.MyBatisRepository; import com.eduask.mvc.entity.Emp; @MyBatisRepository public interface EmpDao { public List<Emp> findAll(); } |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:myns="http://www.mycompany.com/schema/myns" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.mycompany.com/schema/myns http://www.mycompany.com/schema/myns/myns-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:component-scan base-package="com.eduask" /> <mvc:annotation-driven /> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"></property> <property name="username" value="scott"></property> <property name="password" value="zgoracle"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="myDataSource"/> <property name="mapperLocations" value="classpath:com/eduask/mvc/entity/*.xml"/> </bean> <!-- 按指定包和注解标记扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> <property name="basePackage" value="com.eduask"/> <property name="annotationClass" value="com.eduask.mvc.annotation.MyBatisRepository"/> </bean> <!-- 配置视图解析器 --> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp"></property> </bean> </beans> |
package com.eduask.mvc.test; import java.io.IOException; import java.util.List; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.eduask.mvc.dao.EmpDao; import com.eduask.mvc.entity.Emp; public class TestEmpDao { @Test public void testFindAll() throws IOException{ String conf="applicationContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(conf); EmpDao mapper=ac.getBean("empDao",EmpDao.class); List<Emp> list=mapper.findAll(); for(Emp emp:list){ System.out.println(emp.getEname()); } } } |
7:运行测试类
8:新建EmpListController
package com.eduask.mvc.web; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.eduask.mvc.dao.EmpDao; import com.eduask.mvc.entity.Emp; @Controller @RequestMapping("/emp") public class EmpListController { private EmpDao dao; @Autowired public void setDao(EmpDao dao) { this.dao = dao; } @RequestMapping("/list.form") public String execute(Model model){ List<Emp> list=dao.findAll(); model.addAttribute("emps",list); return "emp_list"; } } |
8:新建jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%> <!DOCTYPE HTMLf> <html> <head> <title>My JSP 'emp_list.jsp' starting page</title> <meta charset="utf-8"> </head> <body> <table border="1"> <tr> <td>编号</td> <td>姓名</td> <td>工资</td> <td>入职时间</td> </tr> <c:forEach items="${emps}" var="emp"> <tr> <td>${emp.empno}</td> <td>${emp.ename}</td> <td>${emp.sal}</td> <td>${emp.hiredate}</td> </tr> </c:forEach> </table> </body> </html> |
测试
http://localhost:8080/SMMvcTest/emp/list.form
|
相关文章推荐
- 利用mybatis的分页插件实现商品列表的显示
- 一个页面实现修改删除列表显示,ajax动态生成table(使用ajax,springmvc,jsp)
- 利用jQuery和css实现的模仿百度搜索列表页面的分页的足迹效果
- springMVC、mybatis实现的登录页面(maven)
- 利用热门标签布局,实现单选列表(实现ViewGroup多行显示单选功能)
- 十二、初学.NET—利用AJAX在页面头部显示服务器当前时间,并实现倒计时功能
- SpringMVC+Spring+MyBatis 的综合练习 13 (前端页面之员工列表)
- Android中利用ListView和GridView实现列表视图和网格(图标)视图的相互切换显示
- 问题五-实现详细信息页面(点击列表,预览窗口显示具体信息)
- 利用JS实现:页面组件的动态显示、隐藏
- 实现jsp页面显示用户登录信息,利用session保存。
- 静态页面也可以实现预览 列表不同的显示方式 原创
- 利用css页面隐藏和显示,实现页面滑动
- 利用Struts2和Hibernate实现页面分页显示的功能
- 利用Ajax实现在页面显示局部刷新
- 利用VBS实现显示系统服务列表
- 利用springboot,mybatis和luncene整合创建博客系统(只给出全数据库搜索并显示在搜索页面)
- 怎么实现在播放页面显示正在播放的歌曲列表呢
- 利用struts2框架实现当用户上传图片到服务器后,再显示到前台页面中
- JavaEE_Mybatis_SpringMVC_Mybatis_lesson10__利用foreach标签_拼接SQL串,实现IN(X,Y,Z,...的形式)