web.xml (tomcat启动时读取的配置文件)

首页配置
<welcome-file-list>:index.jsp

servlet配置(<servlet>和<servletmapping>配对出现)
<servlet>
servlet名称
servlet类包路径
servlet初始化参数
参数key:contextConfigLocation
参数值:classpath:spring-servlet.xml,指定servlet初始化的配置文件
指定tomcat容器启动时是否实例化本servlet并调用init方法
<servletmapping>
servlet名称
servlet的url匹配

容器参数配置<context-param>
参数key:contextConfigLocation
参数值:classpath:applicationContext.xml,指定容器参数的配置文件

spring框架监听器配置<listener>
org.springframework.web.context.ContextLoaderListener

////////////////////////////////////////////////////////////////////////////////////////////////////

spring-servlet.xml (指定servlet初始化的配置文件)
启动注解驱动
mvc:annotation-driven
启动包扫描
context:component-scan
添加对模型视图名称的解析
viewClass、prefix、suffix

////////////////////////////////////////////////////////////////////////////////////////////////////

applicationContext.xml (指定容器参数的配置文件)

上下文注解配置
context:annotation-config
启动包扫描
context:component-scan
导入mybatis的配置文件
<import>:spring-mybatis.xml

////////////////////////////////////////////////////////////////////////////////////////////////////

spring-mybatis.xml (数据库配置)

指定配置业务sql的配置文件
classpath:mapper/*.xml
配置事物管理
org.springframework.jdbc.datasource.DataSourceTransactionManager

web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring MVC配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--Spring-servlet.xml config-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<!-- load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法) -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!--spring listener config-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

  

index.jsp

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

  

spring-servlet.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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven > </mvc:annotation-driven> <!-- 启动包扫描功能,以便注册带有@Controllers、@service、@repository、@Component等注解的类成为spring的bean -->
<context:component-scan base-package="Controllers" />
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/> <!-- 前缀 -->
<property name="suffix" value=".jsp"/> <!-- 后缀 -->
</bean>
<!-- 访问静态文件(jpg,js,css)的方法 -->
<!--<mvc:resources location="/files/" mapping="/files/**" />-->
<!--<mvc:resources location="/scripts/" mapping="/scripts/**" />-->
<!--<mvc:resources location="/styles/" mapping="/styles/**" />-->
<!--<mvc:resources location="/Views/" mapping="/Views/**" />-->
</beans>

  

applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config />
<!-- 配置component所在的包,自动加载需要管理的Bean -->
<context:component-scan base-package="Service,Dao" />
<import resource="spring-mybatis.xml" />
</beans>

  

spring-mybatis.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="configProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean> <!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="minPoolSize" value="${c3p0.minPoolSize}"/>
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
<property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>
<property name="acquireRetryDelay" value="${c3p0.acquireRetryDelay}"/>
<property name="autoCommitOnClose" value="${c3p0.autoCommitOnClose}"/>
<property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/>
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
<property name="testConnectionOnCheckin" value="${c3p0.testConnectionOnCheckin}"/>
<property name="maxStatements" value="${c3p0.maxStatements}"/>
<property name="maxStatementsPerConnection" value="${c3p0.maxStatementsPerConnection}"/>
<property name="unreturnedConnectionTimeout" value="${c3p0.unreturnedConnectionTimeout}"/>
</bean> <!-- 配置mybatisSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 配置mybatis mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="Dao"/>
<!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>-->
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> </beans>

  

StudentMapper.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">
<mapper namespace="Dao.StudentMapper">
<resultMap id="BaseResultMap" type="Entity.Student">
<id column="Uid" jdbcType="BINARY" property="uid" />
<result column="Name" jdbcType="VARCHAR" property="name" />
<result column="Age" jdbcType="INTEGER" property="age" />
<result column="ClassId" jdbcType="INTEGER" property="classid" />
</resultMap>
<sql id="Base_Column_List">
Uid, Name, Age, ClassId
</sql>
<select id="selectByPrimaryKey" parameterType="byte[]" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from student
where Uid = #{uid,jdbcType=BINARY}
</select>
<select id="selectByCondition" parameterType="Entity.Student" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
from student
<where>
1=1
<if test="uid != null">
and Uid=#{uid,jdbcType=BINARY}
</if>
<if test="name != null">
and Name=#{name,jdbcType=VARCHAR}
</if>
<if test="age != null">
and Age=#{age,jdbcType=INTEGER}
</if>
<if test="classid != null">
and ClassId=#{classid,jdbcType=INTEGER}
</if>
</where>
</select>
<delete id="deleteByPrimaryKey" parameterType="byte[]">
delete from student
where Uid = #{uid,jdbcType=BINARY}
</delete>
<insert id="insert" parameterType="Entity.Student">
insert into student (Uid, Name, Age,
ClassId)
values (#{uid,jdbcType=BINARY}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
#{classid,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="Entity.Student">
insert into student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">
Uid,
</if>
<if test="name != null">
Name,
</if>
<if test="age != null">
Age,
</if>
<if test="classid != null">
ClassId,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="uid != null">
#{uid,jdbcType=BINARY},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
<if test="classid != null">
#{classid,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="Entity.Student">
update student
<set>
<if test="name != null">
Name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
Age = #{age,jdbcType=INTEGER},
</if>
<if test="classid != null">
ClassId = #{classid,jdbcType=INTEGER},
</if>
</set>
where Uid = #{uid,jdbcType=BINARY}
</update>
<update id="updateByPrimaryKey" parameterType="Entity.Student">
update student
set Name = #{name,jdbcType=VARCHAR},
Age = #{age,jdbcType=INTEGER},
ClassId = #{classid,jdbcType=INTEGER}
where Uid = #{uid,jdbcType=BINARY}
</update>
</mapper>

  

最新文章

  1. WPF自适应窗体实现小结
  2. 代码合并工具——Beyond Compare
  3. 在.sln文件中设置Visual Studio默认启动项目的简单方法
  4. Poj 1061 青蛙的约会(扩展GCD)
  5. Java for LeetCode 031 Next Permutation
  6. HDU-1047(DP-二进制状态压缩)
  7. poj1742 多重背包的可行性问题
  8. shell 整理,更新,记录
  9. Android 设计模式实战之关于封装计费代码库的策略模式详谈
  10. weblogic 部署问题定位与解决
  11. 洛谷 [P2766] 最长不下降子序列问题
  12. mysql数据库备份_可执行文件
  13. VS Code 工具配置和格式化
  14. vbs调用bat 隐藏bat运行时的黑框
  15. LeetCode--155--最小栈(java版)
  16. Spark面对OOM问题的解决方法及优化总结 (转载)
  17. [译]How To Use the Linux Auditing System on CentOS 7
  18. mac 安装gevent报错
  19. (精品)微信支付android端
  20. mysql主从延迟(摘自http://www.linuxidc.com/Linux/2012-02/53995.htm)

热门文章

  1. 调试ucosii_pendsv中断函数有感
  2. pixy&amp;STM32使用记录(串口&amp;SPI外设)
  3. [WC2006]水管局长(LCT)
  4. PEP8 - Python编码规范
  5. Zabbix监控磁盘IO值
  6. spring boot下使用logback或log4j生成符合Logstash标准的JSON格式
  7. 分类器的评价指标-ROC&amp;AUC
  8. python __call__或者说func()()的理解
  9. head里两个重要标签base和meta
  10. ansible Templates