zoukankan      html  css  js  c++  java
  • 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、运行效果如下

  • 相关阅读:
    Python Semaphore
    Python 互斥锁
    Python 递归锁
    Python GIL锁
    Python 线程调用
    进程与线程
    Python paramiko模块
    Python SocketServer模块
    MonoDevelop with Visual Studio to Linux and Mac OSX maintaining a single code base for all platforms.
    mime大全收集
  • 原文地址:https://www.cnblogs.com/holly8/p/5562072.html
Copyright © 2011-2022 走看看