springmvc+mybatis配置

来源:互联网 发布:铁观音 淘宝推荐 编辑:程序博客网 时间:2024/05/21 11:16

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"><!--  加载Spring容器配置  --><listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener> <!--  设置Spring容器加载所有的配置文件的路径  --> <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath*:config/spring-*.xml</param-value>  </context-param> <!--  配置SpringMVC核心控制器  --> <servlet>  <servlet-name>springMVC</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--  配置初始配置化文件,前面contextConfigLocation看情况二选一  --> <init-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath*:config/spring-mvc.xml</param-value>  </init-param> <!--  启动加载一次  -->  <load-on-startup>1</load-on-startup>  </servlet> <!-- 为DispatcherServlet建立映射  --> <servlet-mapping>  <servlet-name>springMVC</servlet-name> <!--  此处可以可以配置成*.do,对应struts的后缀习惯  -->  <url-pattern>/</url-pattern>  </servlet-mapping> <!--  防止Spring内存溢出监听器  --> <!-- <listener>  <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  </listener> --> <!--  解决工程编码过滤器  --> <filter>  <filter-name>encodingFilter</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> <init-param>  <param-name>forceEncoding</param-name>  <param-value>true</param-value>  </init-param>  </filter> <filter-mapping>  <filter-name>encodingFilter</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping> <welcome-file-list>  <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  </web-app>

springmvc.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:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    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-4.0.xsd    http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">    <!-- 注解扫描包 -->    <context:component-scan base-package="com.tgb" />    <!-- 开启注解 -->    <mvc:annotation-driven />    <!--        配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd    -->    <mvc:resources mapping="/img/**" location="/img/" />    <mvc:resources mapping="/js/**" location="/js/" />    <mvc:resources mapping="/css/**" location="/css/" />    <mvc:resources mapping="/html/**" location="/html/" />  <!-- 定义跳转的文件的前后缀 ,视图模式配置-->    <bean id="viewResolver"        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->        <property name="prefix" value="/WEB-INF/jsp/" />        <property name="suffix" value=".jsp" />    </bean></beans>

spring-common.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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.0.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    <!-- 1. 数据源 : DriverManagerDataSource -->    <bean id="dataSource"        class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="com.mysql.jdbc.Driver" />        <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />        <property name="username" value="root" />        <property name="password" value="xxxx" />    </bean>    <!--        2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源        MyBatis定义数据源,同意加载配置    -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"></property>        <property name="configLocation" value="classpath:config/mybatis-config.xml" />    </bean>    <!--        3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory        basePackage:指定sql映射文件/接口所在的包(自动扫描)    -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.tgb.mapper"></property>        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>    </bean>    <!--        4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源    -->    <bean id="txManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"></property>    </bean>    <!-- 5. 使用声明式事务         transaction-manager:引用上面定义的事务管理器     -->    <tx:annotation-driven transaction-manager="txManager" /></beans>

mybatis.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="com.tgb.model.User" />    </typeAliases>    <!-- 实体接口映射资源 -->    <!--        说明:如果xxMapper.xml配置文件放在和xxMapper.java统一目录下,mappers也可以省略,因为org.mybatis.spring.mapper.MapperFactoryBean默认会去查找与xxMapper.java相同目录和名称的xxMapper.xml    -->    <mappers>        <mapper resource="com/tgb/mapper/userMapper.xml" />    </mappers></configuration> 

usermapping配置文件

<?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:必须与对应的接口全类名一致    id:必须与对应接口的某个对应的方法名一致     --><mapper namespace="com.tgb.mapper.UserMapper">        <insert id="save" parameterType="User">        insert into user(userName,age) values(#{userName},#{age})    </insert>        <update id="update" parameterType="User">        update user set userName=#{userName},age=#{age} where id=#{id}    </update>        <delete id="delete" parameterType="int">        delete from user where id=#{id}    </delete>         <!-- mybsits_config中配置的alias类别名,也可直接配置resultType为类路劲 -->      <select id="findById" parameterType="int" resultType="User">        select id id,userName userName,age age from user where id=#{id}    </select>        <select id="findAll" resultType="User">        select * from user    </select>    </mapper>


0 0