mybits-springmvc学习笔记

论坛 期权论坛 脚本     
匿名技术用户   2021-1-7 09:04   56   0
  1. 架包搭配.(架包不匹配经常报出一些莫名其秒的问题)

新建一个项目 并导入以下架包(这里少一个mysql的驱动包)

架包下载地址:http://download.csdn.net/detail/hu5080126/5166795

3.配置WEB.xml

<?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">
 
 <servlet>
  <servlet-name>test</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!--
   DispatcherServlet 默认加载的bean文件是/WEB-INF/(servlet-name)-servlet.xml
   可以通过配置contextConfigLocation来改变加载的文件
  -->
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/*Context.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <!--为DispatcherServlet建立映射 -->
 <servlet-mapping>
  <servlet-name>test</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>


4在web-Inf下面新建aplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
 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-2.5.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 <!-- 开启注解模式 -->
 <mvc:annotation-driven />
 <!-- 对org.simple下所有包下的类的注解进行扫描,并自动创建bean实例和装配bean -->
 <context:component-scan base-package="org.simple"></context:component-scan>
 <!-- 配置视图 -->
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/" />
  <property name="suffix" value=".jsp" />
  <!--
   可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑
  -->
  <property name="viewClass">
   <value>org.springframework.web.servlet.view.InternalResourceView
   </value>
  </property>
 </bean>
 
</beans>

继续新建一个daoContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
 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-2.5.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 <!-- 载入MySql配置 文件-->
 <context:property-placeholder location="classpath:MySql.properties" />
 <!-- 配置DataSource数据源-->
 <bean id="DataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.user}" />
  <property name="password" value="${jdbc.password}" />
 </bean>
    <!-- Spring-Mybatis整合 -->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="configLocation" value="classpath:MyBatis-config.xml" />
  <property name="dataSource" ref="DataSource" />
 </bean>
 <!-- 自动扫描mappar-->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="org.simple.dao"></property>
 </bean>
</beans>

5.在src下面建立一个Mybatis-Config.xml

<?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>
     <typeAliases>
         <typeAlias alias="User" type="org.simple.model.User"/>
     </typeAliases>
     <mappers>
        <mapper  resource="org/simple/dao/mapper/UserMapper.xml"/>
     </mappers>
</configuration>


不写了 感觉自己在copy代码 没啥解释 因为小弟也刚刚接触 不久。 直接上源码好了。


项目源码:

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP