1、SpringMVC+MyBaits實現查詢所有


1、創建如下所示項目

2、在src下的com.entity包下創建 Emp.java

 1 package com.entity;
2 /**
3 *
4 * @author Holly老師
5 *
6 */
7 public class Emp {
8 private Integer empno;
9 private String ename;
10 private double sal;
11 private Integer deptno;
12 private String dname;
13
14 public Integer getEmpno() {
15 return empno;
16 }
17 public void setEmpno(Integer empno) {
18 this.empno = empno;
19 }
20 public String getEname() {
21 return ename;
22 }
23 public void setEname(String ename) {
24 this.ename = ename;
25 }
26 public double getSal() {
27 return sal;
28 }
29 public void setSal(double sal) {
30 this.sal = sal;
31 }
32 public Integer getDeptno() {
33 return deptno;
34 }
35 public void setDeptno(Integer deptno) {
36 this.deptno = deptno;
37 }
38 public String getDname() {
39 return dname;
40 }
41 public void setDname(String dname) {
42 this.dname = dname;
43 }
44 @Override
45 public String toString() {
46 return "Emp [deptno=" + deptno + ", dname=" + dname + ", empno=" + empno
47 + ", ename=" + ename + ", sal=" + sal + "]";
48 }
49
50
51 }
Emp.java

3、在src下的com.mapper包下創建 EmpMapper.java

 1 package com.mapper;
2
3 import java.util.List;
4
5 import com.entity.Emp;
6 /**
7 *
8 * @author Holly老師
9 *
10 */
11 public interface EmpMapper {
12 public List<Emp> findAll();
13
14 }
EmpMapper.java

4、在src下的com.mapper包下創建 EmpMapper.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
3 <mapper namespace="com.mapper.EmpMapper">
4 <select id="findAll" resultType="com.entity.Emp">
5 select e.empno,e.ename,e.sal,e.deptno,d.dname from emp e,dept d where e.deptno=d.deptno
6 </select>
7 </mapper>
EmpMapper.xml

5、在src下的com.service包下創建 EmpService.java

 1 package com.service;
2
3 import java.util.List;
4
5 import com.entity.Emp;
6
7 /**
8 *
9 * @author Holly老師
10 *
11 */
12 public interface EmpService {
13
14 List<Emp> findAll();
15
16 }
EmpService.java

6、在src下的com.service.impl包下創建EmpServiceImpl.java

 1 package com.service.impl;
2
3 import java.util.List;
4
5
6 import javax.annotation.Resource;
7
8 import org.springframework.stereotype.Service;
9 import org.springframework.transaction.annotation.Transactional;
10
11 import com.entity.Emp;
12 import com.mapper.EmpMapper;
13 import com.service.EmpService;
14 /**
15 *
16 * @author Holly老師
17 *
18 */
19 @Service
20 @Transactional
21 public class EmpServiceImpl implements EmpService {
22 @Resource
23 private EmpMapper empMapper;
24
25 public List<Emp> findAll() {
26 return empMapper.findAll();
27 }
28
29 }
EmpServiceImpl.java

7、在com.controller包下創建EmpController.java

 1 package com.controller;
2 import java.util.List;
3 import javax.annotation.Resource;
4 import javax.servlet.http.HttpServletRequest;
5 import javax.servlet.http.HttpServletResponse;
6 import org.springframework.stereotype.Controller;
7 import org.springframework.web.bind.annotation.RequestMapping;
8 import com.entity.Emp;
9 import com.service.impl.EmpServiceImpl;
10 /**
11 * 7.控制類
12 * @author pc
13 *
14 */
15 @Controller
16 public class EmpController {
17 @Resource
18 private EmpServiceImpl empServiceImpl;
19 //方法1
20 @RequestMapping("/findAll.do")
21 public String findAll(HttpServletRequest request,HttpServletResponse response){
22 List<Emp> list=empServiceImpl.findAll();
23 if(list!=null){
24 System.out.println("findAll success");
25 request.setAttribute("list", list);
26 }else{
27 System.out.println("findAll error");
28
29 }
30 return "/index.jsp";
31 }
32
33
34 }
EmpController.java

8、在WebRoot下的WEB-INF下創建springmvc-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:aop="http://www.springframework.org/schema/aop"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xmlns:mvc="http://www.springframework.org/schema/mvc"
7 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8 xsi:schemaLocation="http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/aop
11 http://www.springframework.org/schema/aop/spring-aop.xsd
12 http://www.springframework.org/schema/context
13 http://www.springframework.org/schema/context/spring-context.xsd
14 http://www.springframework.org/schema/tx
15 http://www.springframework.org/schema/tx/spring-tx.xsd
16 http://www.springframework.org/schema/mvc
17 http://www.springframework.org/schema/mvc/spring-mvc.xsd
18 ">
19
20 <!-- 1.mvc支持注解 -->
21 <mvc:annotation-driven/>
22
23 <!-- 2.全局掃描包資源 -->
24 <context:component-scan base-package="com"/>
25
26 <!-- 3.驅動管理的數據源配置 -->
27 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
28 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
29 <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
30 <property name="username" value="scott"/>
31 <property name="password" value="tiger"/>
32 </bean>
33
34 <!-- 4.配置數據源的事務 -->
35 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
36 <property name="dataSource" ref="dataSource"/>
37 </bean>
38
39 <!-- 5.配置sqlsession的bean工廠 -->
40 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
41 <property name="dataSource" ref="dataSource"/>
42 <property name="mapperLocations" value="classpath:com/mapper/EmpMapper.xml"/>
43 </bean>
44
45 <!-- 6.spring和mybaits整合,自動掃映射輸入參數 -->
46 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
47 <property name="basePackage" value="com.mapper"/>
48 </bean>
49 </beans>
springmvc-servlet.xml

9、在WebRoot下的WEB-INF下創建web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.5"
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
7 <servlet>
8 <servlet-name>springmvc</servlet-name>
9 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
10 </servlet>
11 <servlet-mapping>
12 <servlet-name>springmvc</servlet-name>
13 <url-pattern>*.do</url-pattern>
14 </servlet-mapping>
15 <filter>
16 <filter-name>CharacterEncodingFilter</filter-name>
17 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
18 <init-param>
19 <param-name>encoding</param-name>
20 <param-value>UTF-8</param-value>
21 </init-param>
22 </filter>
23 <filter-mapping>
24 <filter-name>CharacterEncodingFilter</filter-name>
25 <url-pattern>/*</url-pattern>
26 </filter-mapping>
27 <welcome-file-list>
28 <welcome-file></welcome-file>
29 </welcome-file-list>
30 </web-app>
web.xml

10、在WebRoot下創建index.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3 <%
4 String path = request.getContextPath();
5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
6 %>
7
8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9 <html>
10 <head>
11 <base href="<%=basePath%>">
12
13 <title>My JSP 'index.jsp' starting page</title>
14 <meta http-equiv="pragma" content="no-cache">
15 <meta http-equiv="cache-control" content="no-cache">
16 <meta http-equiv="expires" content="0">
17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18 <meta http-equiv="description" content="This is my page">
19 <!--
20 <link rel="stylesheet" type="text/css" href="styles.css">
21 -->
22 </head>
23
24 <body>
25 <table>
26 <tr><td>員工編號</td><td>員工姓名</td><td>工資</td><td>部門編號</td><td>部門名稱</td></tr>
27 <c:forEach var="i" items="${list}">
28 <tr><td>${i.empno }</td><td>${i.ename }</td><td>${i.sal }</td><td>${i.deptno }</td><td>${i.dname}</td></tr>
29 </c:forEach>
30 </table>
31
32 </body>
33 </html>
index.jsp

11、運行效果如下

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com