2021年9月1日------SSM整合

SSM整合

  • 配置SpringMVC
  • 配置spring
  • 配置mybatis

配置SpringMVC

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""xmlns:xsi=""xsi:schemaLocation=" 
0.xsd"version="4.0"><!--全站乱码解决过滤器--><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></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--配置前端控制器--><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:/springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.action</url-pattern></servlet-mapping>
</web-app>

配置springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:xsi=""
xmlns:mvc="" 
xsi:schemaLocation=" 
.xsd
 .xsd 
.xsd">xmlns:context=""<!--配置包扫描--><context:component-scan base-package="cn.tedu.controller"/><!--注解方式mvc--><mvc:annotation-driven/><!--配置视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/></bean>
</beans>

配置spring

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:xsi=""xmlns:context=""xsi:schemaLocation=" 
.xsd
 .xsd 
.xsd 
.xsd"><!--配置包扫描--><context:component-scan base-package="cn.tedu"/><!--注解方式DI--><context:annotation-config/><!--注解方式AOP--><aop:aspectj-autoproxy/><!--配置数据源--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql:///mydb"/><property name="user" value="root"/><property name="password" value="root"/></bean><!--整合MyBatis--><!--配置SqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:sqlMapConfig.xml"/><property name="mapperLocations" value="classpath:/mappers/*.xml"/></bean><!-- MyBatis MapperBean扫描器,负责为MapperBean生成实现类 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="cn.tedu.mapper"></property></bean><!--声明式事务处理--><!--配置事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--开启注解方式事务管理开关--><tx:annotation-driven/>
</beans>

web.xml中配置监听器,使Spring容器在web应用初始化时自动加载:

<!--配置监听器,用于创建和销毁Spring容器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:/applicationContext.xml</param-value></context-param>

配置mybatis

配置sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN"".dtd"> <configuration></configuration>

配置映射文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"".dtd"> <mapper namespace="cn.tedu.mapper.UserMapper"><select id="selectById" resultType="cn.tedu.domain.User">select * from user where id=#{id}</select><insert id="insertUser">insert into user valuse (null,#{name},#{age});</insert>
</mapper>

2021年9月1日------SSM整合

SSM整合

  • 配置SpringMVC
  • 配置spring
  • 配置mybatis

配置SpringMVC

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""xmlns:xsi=""xsi:schemaLocation=" 
0.xsd"version="4.0"><!--全站乱码解决过滤器--><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></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--配置前端控制器--><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:/springmvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.action</url-pattern></servlet-mapping>
</web-app>

配置springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:xsi=""
xmlns:mvc="" 
xsi:schemaLocation=" 
.xsd
 .xsd 
.xsd">xmlns:context=""<!--配置包扫描--><context:component-scan base-package="cn.tedu.controller"/><!--注解方式mvc--><mvc:annotation-driven/><!--配置视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/></bean>
</beans>

配置spring

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:xsi=""xmlns:context=""xsi:schemaLocation=" 
.xsd
 .xsd 
.xsd 
.xsd"><!--配置包扫描--><context:component-scan base-package="cn.tedu"/><!--注解方式DI--><context:annotation-config/><!--注解方式AOP--><aop:aspectj-autoproxy/><!--配置数据源--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql:///mydb"/><property name="user" value="root"/><property name="password" value="root"/></bean><!--整合MyBatis--><!--配置SqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:sqlMapConfig.xml"/><property name="mapperLocations" value="classpath:/mappers/*.xml"/></bean><!-- MyBatis MapperBean扫描器,负责为MapperBean生成实现类 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="cn.tedu.mapper"></property></bean><!--声明式事务处理--><!--配置事务管理器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--开启注解方式事务管理开关--><tx:annotation-driven/>
</beans>

web.xml中配置监听器,使Spring容器在web应用初始化时自动加载:

<!--配置监听器,用于创建和销毁Spring容器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:/applicationContext.xml</param-value></context-param>

配置mybatis

配置sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN"".dtd"> <configuration></configuration>

配置映射文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"".dtd"> <mapper namespace="cn.tedu.mapper.UserMapper"><select id="selectById" resultType="cn.tedu.domain.User">select * from user where id=#{id}</select><insert id="insertUser">insert into user valuse (null,#{name},#{age});</insert>
</mapper>