1.搭建标准web项目结构
搭建完成后的项目结构如图
1.创建普通web项目(略)
2.在lib中添加jar包
3.在resources中添加spring-config.xml主配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc.xsd 13 "> 14 <!-- 开启注解--> 15 <mvc:annotation-driven/> 16 <!-- 扫描包--> 17 <context:component-scan base-package="com.day02.sation.controller,com.day02.sation.service"/> 18 <!--读取配置文件--> 19 <context:property-placeholder location="classpath:db.properties"/> 20 <!-- 配置连接池--> 21 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 22 <property name="driverClassName" value="${mysql.driver}"/> 23 <property name="url" value="${mysql.url}"/> 24 <property name="username" value="${mysql.userName}"/> 25 <property name="password" value="${mysql.password}"/> 26 </bean> 27 <!-- 配置sqlSessionFactory--> 28 <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 29 <!-- 配置连接池--> 30 <property name="dataSource" ref="dataSource"/> 31 <!--别名--> 32 <!-- 读取映射文件--> 33 <property name="mapperLocations" value="classpath:mapper/*.xml"/> 34 </bean> 35 <!-- 扫描接口包--> 36 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 37 <!-- sessionFactory--> 38 <property name="sqlSessionFactoryBeanName" value="sessionFactory"/> 39 <!-- 扫描接口包--> 40 <property name="basePackage" value="com.day02.sation.dao"/> 41 </bean> 42 <!--前缀后缀--> 43 </beans>
使用到的db.properties如下:
1 #驱动 2 mysql.driver=com.mysql.jdbc.Driver 3 #数据库ip地址 4 mysql.url=jdbc:mysql://localhost:3306/station 5 #用户名 6 mysql.userName=root 7 #密码 8 mysql.password=admin
4.配置web.xml代码
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 <!-- 配置dispatcherServlet--> 7 <servlet> 8 <servlet-name>dispatcherServlet</servlet-name> 9 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 10 <!-- 读取spring-config.xml文件--> 11 <init-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>classpath:spring-config.xml</param-value> 14 </init-param> 15 <!-- 项目启动的时候初始化--> 16 <load-on-startup>1</load-on-startup> 17 </servlet> 18 <!-- 映射地址--> 19 <servlet-mapping> 20 <servlet-name>dispatcherServlet</servlet-name> 21 <url-pattern>/</url-pattern> 22 </servlet-mapping> 23 </web-app>
5.在model中创建Ticket.java文件
1 package com.day02.sation.model; 2 3 /** 4 * Created by Administrator on 12/27. 5 */ 6 public class Ticket { 7 private Integer id; 8 private String startStation; 9 private String stopStation; 10 11 public Integer getId() { 12 return id; 13 } 14 15 public void setId(Integer id) { 16 this.id = id; 17 } 18 19 public String getStartStation() { 20 return startStation; 21 } 22 23 public void setStartStation(String startStation) { 24 this.startStation = startStation; 25 } 26 27 public String getStopStation() { 28 return stopStation; 29 } 30 31 public void setStopStation(String stopStation) { 32 this.stopStation = stopStation; 33 } 34 }
6.在dao中添加ITicket.java接口
1 package com.day02.sation.dao; 2 3 import com.day02.sation.model.Ticket; 4 5 import java.util.List; 6 7 /** 8 * Created by Administrator on 12/27. 9 */ 10 public interface ITicketDao { 11 /** 12 * 查询所有 13 * @return 14 */ 15 List<Ticket> getList(); 16 }
7.在resources中添加mapper文件夹,然后在mapper中添加ticketMapper.xml映射文件
1 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 2 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <!-- 4 对应的接口地址: namespace="com.day02.sation.dao.ITicketDao" 5 --> 6 <mapper namespace="com.day02.sation.dao.ITicketDao"> 7 <!-- 查询所有 8 对应的接口方法名称 id="getList" 9 定义返回的类型 resultType="com.day02.sation.model.Ticket" 10 --> 11 <select id="getList" resultType="com.day02.sation.model.Ticket"> 12 SELECT id,start_station startStation,stop_station stopStation FROM ticket 13 </select> 14 </mapper>
8.编写测试类进行测试 记住所有dao必须做测试,非常重要
1 package com.day02.sation.test; 2 3 import com.day02.sation.dao.ITicketDao; 4 import com.day02.sation.model.Ticket; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.test.context.ContextConfiguration; 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 11 import java.util.List; 12 13 /** 14 * Created by Administrator on 12/27. 15 */ 16 @RunWith(SpringJUnit4ClassRunner.class) 17 @ContextConfiguration("classpath:spring-config.xml") 18 public class TestDao { 19 @Autowired 20 private ITicketDao ticketDao; 21 @Test 22 public void testGetList(){ 23 24 List<Ticket> list = ticketDao.getList(); 25 26 System.out.println("list="+list); 27 } 28 }
9.编写ITicketservice.java接口
1 package com.day02.sation.service; 2 3 import com.day02.sation.model.Ticket; 4 5 import java.util.List; 6 7 /** 8 * Created by Administrator on 12/27. 9 */ 10 public interface ITicketService { 11 /* 查询所有*/ 12 List<Ticket> getList(); 13 }
10.编写TicketService实现类
1 package com.day02.sation.service.impl; 2 3 import com.day02.sation.dao.ITicketDao; 4 import com.day02.sation.model.Ticket; 5 import com.day02.sation.service.ITicketService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 9 import java.util.List; 10 11 /** 12 * Created by Administrator on 12/27. 13 */ 14 @Service 15 public class TicketService implements ITicketService { 16 @Autowired 17 private ITicketDao ticketDao; 18 @Override 19 public List<Ticket> getList() { 20 return ticketDao.getList(); 21 } 22 }
11.编写控制层 TicketController
1 package com.day02.sation.controller; 2 3 import com.day02.sation.model.Ticket; 4 import com.day02.sation.service.ITicketService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 10 import javax.servlet.http.HttpServletRequest; 11 import java.util.List; 12 13 /** 14 * Created by Administrator on 12/27. 15 */ 16 @Controller 17 @RequestMapping("/ticket") 18 public class TicketController { 19 @Autowired 20 private ITicketService ticketService; 21 22 /** 23 * 数据和页面一起请求 24 * @param req 25 * @return 26 */ 27 @RequestMapping("/list") 28 public String listPage(HttpServletRequest req){ 29 System.out.println("----listPage--------"); 30 //接收参数 31 //调用业务方法 32 List<Ticket> list = ticketService.getList(); 33 req.setAttribute("list",list); 34 return "/WEB-INF/views/list.jsp"; 35 } 36 37 38 }
12.在该路径下(WEB-INF/views/)下添加list.jsp
注意在使用c标签时必须
a.配置 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
b.添加jar包 standard.jar jstl.jar
1 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3 <html> 4 <head> 5 <title>车票列表</title> 6 </head> 7 <body> 8 <div align="center"> 9 <table id="ticketList" border="1" cellspacing="1"> 10 <tr> 11 <td>编号</td> 12 <td>开始车站</td> 13 <td>到达车站</td> 14 </tr> 15 <%--模拟假数据--%> 16 <tr> 17 <td>1</td> 18 <td>北京</td> 19 <td>成都</td> 20 </tr> 21 <%-- 数据库中获取的数据--%> 22 <c:forEach items="${list}" var="ticket"> 23 <tr> 24 <td>${ticket.id}</td> 25 <td>${ticket.startStation}</td> 26 <td>${ticket.stopStation}</td> 27 </tr> 28 </c:forEach> 29 </table> 30 </div> 31 </body> 32 </html>
13.部署启动,访问 http://localhost:8080/ticket/list 界面如下图: