spring+springmvc+mybatis

来源:互联网 发布:火车票抢票用什么软件 编辑:程序博客网 时间:2024/06/04 20:02

学习框架简单搭建

1.首先创建工程

2.建立包,根据mvc模式把包分为四层,model,dao,service,serviceimp,controller.

model为实体

dao为接口层,定义的方法是直接对数据库进行操作。在mapper文件中对数据库进行增删改查的sql语句书写

service也为接口层,可以看成业务接口,实现业务的功能接口。

serviceimp 实现service接口,用注解方式引用dao层的接口.

controller 为控制层,实现页面的跳转以及接口的调用。用注解的方式引用service层的接口


还可以建立一个专门存放mapper的包,里面放实体的映射文件,进行增删改查


重要的配置文件为spring-servlet.xml,Configure.xml,applicationContext.xml

1.spring-servlet.xml配置扫描的包以及解析路径

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>      <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"          xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"          xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd           http://www.springframework.org/schema/mvc            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd            http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context.xsd">                 <!-- 默认注解映射支持 -->           <mvc:annotation-driven/>            <!-- 自动扫描包 -->            <context:component-scan base-package="com.tag" />                      <!-- 配置jsp的视图路径 -->         <bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/" /><property name="suffix" value=".jsp"></property></bean>    </beans>  </span>

Configure.xml配置实体映射文件mapper.xml的路径

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><mappers><mapper resource="com/tag/mapper/UserMapper.xml"/></mappers></configuration></span>

applicationContext.xml配置数据源,session工厂,事物管理,以及配置Configure.xml配置文件和扫描接口和Configure.xml所在的包

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?><beans      xmlns="http://www.springframework.org/schema/beans"      xmlns:tx="http://www.springframework.org/schema/tx"      xmlns:p="http://www.springframework.org/schema/p"      xmlns:aop="http://www.springframework.org/schema/aop"       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-3.0.xsd       http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  <!-- 配置数据源-->      <bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">      <property name="driverClassName">          <value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>      </property>      <property name="url">          <value>jdbc:sqlserver://10.189.80.24:1433;databaseName=wechatdb</value>         <!--springmybaitis是我的数据库  -->    </property>      <property name="username">          <value>wechatuser</value>      </property>      <property name="password">          <value>wechatuser@gtja</value>      </property>  </bean>  <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="jdbcDataSource" />    </bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">      <property name="dataSource" ref="jdbcDataSource" />      <property name="configLocation" value="classpath:Configure.xml"></property>  </bean>  <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.tag.mapper" />    </bean>        <bean name="baseDaoMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"abstract="true" lazy-init="true"><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>        <bean id="usersDao" parent="baseDaoMapper"><property name="mapperInterface" value="com.tag.dao.UserDao" /></bean></beans></span>

最后配置web.xml,这里面配置监听器,字符集过滤器,扫描applicationContext.xml以及spring-servlet.xml

<span style="font-size:14px;"><?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>SpringMVC-Spring-myBatis</display-name>         <!-- 配置文件位置,默认为/WEB-INF/applicationContext.xml -->      <context-param>          <param-name>contextConfigLocation</param-name>          <param-value>classpath:ApplicationContent.xml</param-value>      </context-param>         <!-- 字符集过滤器 -->      <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>/*</url-pattern>      </filter-mapping>             <!-- 上下文Spring监听器 -->      <listener>          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>             <!-- servlet控制跳转 -->      <servlet>          <servlet-name>springmvc</servlet-name>          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>          <!-- 配置文件 -->          <init-param>              <param-name>contextConfigLocation</param-name>              <param-value>classpath:spring-servlet.xml</param-value>          </init-param>      </servlet>      <servlet-mapping>          <servlet-name>springmvc</servlet-name>          <url-pattern>/</url-pattern>      </servlet-mapping>           <!-- 激活静态资源的默认配置,解决Rest风格兼容 -->    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.css</url-pattern>    </servlet-mapping>    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.js</url-pattern>    </servlet-mapping>         <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.gif</url-pattern>    </servlet-mapping>    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.jpg</url-pattern>    </servlet-mapping>           <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app></span>



0 0
原创粉丝点击