简单Web项目搭建:
一.流程
1. 导包
n个springMVC;
2个mybatis<其中一个是mybatis-spring>;
3个jackson包;
2. xml配置
web.xml和applicationContext.xml
3. 建包,建接口,建类
4. 建jsp
二:具体分说
1. XML配置:
1.1 web.xml配置:
字符编码器和SpringMVC控制器
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name></display-name> <welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param> </filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>*.do</url-pattern></filter-mapping><servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>
a.字符编号器 :filter
1 <filter> 2 <filter-name>CharacterEncodingFilter</filter-name> 3 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>utf-8</param-value> 7 </init-param> 8 </filter> 9 <filter-mapping> 10 <filter-name>CharacterEncodingFilter</filter-name> 11 <url-pattern>/*</url-pattern> 12 </filter-mapping>
b.SpringMVC控制器:Servlet
1 <servlet> 2 <servlet-name>DispatcherServlet</servlet-name> 3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 4 <init-param> 5 <param-name>contextConfigLocation</param-name> 6 <param-value>classpath:applicationContext.xml</param-value> 7 </init-param> 8 <load-on-startup>1</load-on-startup> 9 </servlet> 10 <servlet-mapping> 11 <servlet-name>DispatcherServlet</servlet-name> 12 <url-pattern>/</url-pattern> 13 </servlet-mapping>
1.2 applicationContext.xml配置:
注解扫描、mvc驱动、数据源、sqlSessionFactory、dao层帮助类、事务管理、事务驱动(没有先后顺序)
<?xml version="1.0" encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd "><context:component-scan base-package="cn.bdqn.ssm"></context:component-scan><mvc:annotation-driven></mvc:annotation-driven><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property><property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property><property name="user" value="tb28"></property><property name="password" value="accp"></property></bean><bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="typeAliasesPackage" value="cn.web.ssm" ></property></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sessionFactory"></property><property name="basePackage" value="cn.web.ssm.dao"></property></bean><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><tx:annotation-driven transaction-manager="transactionManager"/></beans>
a. 注解扫描:
1 <context:component-scan base-package="cn.bdqn.ssm"> 2 </context:component-scan>
b. mvc驱动:
<mvc:annotation-driven></mvc:annotation-driven>
c. 数据源:看导入的jdbc及数据库的包:本次是 c3p0,ojdbc(Oracle数据库)
1 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 2 <property name="user" value="betterLearning"></property> 3 <property name="password" value="gzh"></property> 4 <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property> 5 <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> 6 </bean>
d. sqlSessionFactory:要从数据源中获取
1 <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 2 <property name="dataSource" ref="dataSource"></property> 3 <property name="typeAliasesPackage" value="cn.web.ssm"></property> 4 </bean>
e. dao层帮助类:扫描xxxDao.xml,xxxDao.xml作为实现类放入容器中
1 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 2 <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property> 3 <property name="basePackage" value="cn.web.ssm.dao"></property> 4 </bean>
f. 事务管理:也要从数据源中获取
1 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 2 <property name="dataSource" ref="dataSource"></property> 3 </bean>
g. 事务驱动:
<tx:annotation-driven transaction-manager="transactionManager"/>
三、建包、建接口、建类(展示部分)
流程:entity--dao--service(包括serviceImpl)--controller
a. entity包:Ticket.java
package cn.web.ssm.entity;public class Ticket {private Integer id;private String company;private Double price;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getCompany() {return company;}public void setCompany(String company) {this.company = company;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;}@Overridepublic String toString() {return "Ticket [id=" + id + ", company=" + company + ", price=" + price+ "]";} }
b. dao包:TicketDao.xml和TicketDao.java;
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "http://mybatis.org/dtd/mybatis-3-mapper.dtd" "mybatis-3-mapper.dtd" > <mapper namespace="cn.web.ssm.dao.TicketDao"><select id="getTicketsById" resultType="Ticket">select t.id id,t.sell_company company,t.ticket_price pricefrom flight_tickets t where t.flight_id=#{id} </select> </mapper>
package cn.bdqn.ssm.dao; import java.util.List; import cn.web.ssm.entity.Ticket;public interface TicketDao {List<Ticket> getTicketsById(Integer id); }
c. service包:TicketService.java和TicketServiceImpl.java
package cn.web.ssm.service; import java.util.List; import cn.web.ssm.entity.Ticket;public interface TicketService {List<Ticket> getTicketsById(Integer id); }
package cn.web.ssm.service; import java.util.List;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;import cn.web.ssm.dao.TicketDao; import cn.web.ssm.entity.Ticket;@Service @Transactional public class TicketServiceImpl implements TicketService {@Autowiredprivate TicketDao ticketDao;public void setTicketDao(TicketDao ticketDao) {this.ticketDao = ticketDao;}
public List<Ticket> getTicketsById(Integer id) {return ticketDao.getTicketsById(id);} }
d.controller包:TicketController.java;
package cn.web.ssm.controller; import java.util.List;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 cn.web.ssm.entity.Ticket; import cn.web.ssm.service.TicketService;@Controller public class TicketController {@Autowiredprivate TicketService ticketService;public void setTicketService(TicketService ticketService) {this.ticketService = ticketService;}@RequestMapping("showTicket")@ResponseBodypublic List<Ticket> showTicket(Integer id) {List<Ticket> tickets = ticketService.getTicketsById(id);for (Ticket ticket : tickets) {System.out.println(ticket);} return tickets;} }