您的位置:首页 > 编程语言 > Java开发

利用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映射文件

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();

}

5:新建spring配置文件

 

 

<?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>

6:新建测试类

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

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐