大型运输行业实战_day03_1_基于intellij idea的非maven spring+springMVC+mybatis搭建
1.搭建标准web项目结构
搭建完成后的项目结构如图

1.创建普通web项目(略)

2.在lib中添加jar包

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

大型运输行业实战_day03_1_基于intellij idea的非maven spring+springMVC+mybatis搭建的更多相关文章
- 大型运输行业实战_day11_2_事务理论与实际生产配置事务管理
1.什么是事务(Transaction:tx) 数据库的某些需要分步完成,看做是一个整体(独立的工作单元),不能分割,要么整体成功,要么整体生效.“一荣俱荣,一损俱损”,最能体现事务的思想.案例:银行 ...
- SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
本篇文章主要内容是介绍如何使用IntelliJ IDEA创建Spring + SpringMVC + MyBatis项目,下面会给出项目搭建的详细步骤以及相关的配置文件. 1. 创建maven项目 ...
- 大型运输行业实战_day14_1_webserivce简单入门
1.简单使用 1.1.服务端 1.编写接口 package com.day02.sation.ws; /** * Created by Administrator on 1/12. */ public ...
- 大型运输行业实战_day15_1_全文检索之Lucene
1.引入 全文检索简介: 非结构化数据又一种叫法叫全文数据.从全文数据(文本)中进行检索就叫全文检索. 2.数据库搜索的弊端 案例 : select * from product whe ...
- 大型运输行业实战_day13_1_定时任务spring-quartz
1.jar包 拷贝quartz-2.2.3.jar包到项目 2.编写定时任务类TicketQuart.java package com.day02.sation.task; import com.da ...
- 大型运输行业实战_day12_1_权限管理实现
1.业务分析 权限说的是不同的用户对同一个系统有不同访问权限,其设计的本质是:给先给用户分配好URL,然后在访问的时候判断该用户是否有当前访问的URL. 2.实现 2.1数据库设计标准5表权限结构 2 ...
- 大型运输行业实战_day11_1_aop理论与aop实际业务操作
1.aop概述 Spring的AOP:什么叫做AOP:Aspect oritention programming(面向切面编程)什么是切面:看图,业务方法 执行前后.AOP的目的:AOP能够将那些与业 ...
- 大型运输行业实战_day01_2_需求文档
1.文档格式 (见模板文件) 2.Axure简单使用 2.1安装Axure傻瓜式安装 2.2简单使用axure 3.总结 需求文件完成后应该包括三种文件: 1.axure文件 2.axure生成的ht ...
- 大型运输行业实战_day01_1_业务分析
1.业务分析 发展历史: 上车收费-->车站买票(相当于先收钱后上车)-->站务系统--->联网售票 2.项目结构 3.开发流程分析 1.业务分析 图文并茂 ...
随机推荐
- Postgresql 创建账户,修改密码
sudo su postgres psql \password postgres输入密码\q 本机调试的时候,最好在装完以后添加一个pgsql的管理员帐号,否则phppgadmin不让登陆 创建用户 ...
- ios之block笔记
目测和函数指针基本类似用法,贴个hello world,备用 typedef int (^TestBlock)(int val1,int val2); __block ;//这里加__block是为了 ...
- 分布式一致性协议之:Gossip(八卦)算法
Gossip算法因为Cassandra而名声大噪,Gossip看似简单,但要真正弄清楚其本质远没看起来那么容易.为了寻求Gossip的本质,下面的内容主要参考Gossip的原始论文:<<E ...
- [转]Excel.dll 导出Excel控制
Excel.dll 导出Excel控制 2010-06-12 11:26 2932人阅读 评论(2) 收藏 举报 excelmicrosoftstring产品服务器google 最近做了个导出Exce ...
- CentOS 6.4 添加永久静态路由所有方法汇总(原创)
转摘,原文章地址:http://blog.sina.com.cn/s/blog_828e50020101ern5.html 查看路由的命令route -n CentOS添加永久静态路由 在使用双网卡, ...
- mysql导入导出数据中文乱码解决方法小结(1、navicat导入问题已解决,创建连接后修改连接属性,选择高级->将使用Mysql字符集复选框去掉,下拉框选择GBK->导入sql文件OK;2、phpmyadmin显示乱码的问题也解决,两步:1.将sql文件以utf8的字符集编码另存,2.将文件中sql语句中的字段字符集编码改成utf8,导入OK)
当向 MySQL 数据库插入一条带有中文的数据形如 insert into employee values(null,'张三','female','1995-10-08','2015-11-12',' ...
- python redis启用线程池管理
pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT,max_connections=3,password=REDIS_PASSWO ...
- Ps操作技巧(快捷键大全)
一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取) 矩形.椭圆选框工具 [M] 移动工具 [V] 套索.多边形套索.磁性套索 [L] 魔棒工具 [W] 裁剪工具 [C] 切片工 ...
- Python 实现双向链表(图解)
原文:https://blog.csdn.net/qq490691606/article/details/49948263 git 路径 https://github.com/wangpanjun/d ...
- 线程池,queue模块增加用法
1 同一个进程内的队列(多线程) import queue queue.Queue() 先进先出 queue.LifoQueue() 后进先出 queue.PriorityQueue() 优先级队列 ...