ssm配置一一道来

resource下配置文件
Mybatis------Spring------SpringMVC
1:创建mybatis包

1.1创建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>

    <setting>

    <setting name="mapUnderscoreToCamelCase" value="true"/>      

    <settting>

<configuration>  

----------------------------------------------------------------------------

1.2创建mappers包(可以有各对应mapper下的类,此处只创建一个)

----------------------------------------------------------------------------

<?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="cn.xlh.travel.mapper.UserMapper">
    -----各类sql语句-----
  <select id="queryUserByUserName" resultType="User">
select * from tab_user where username=#{username}
</select>
</mapper>
------------------------------------------------------------

2:创建spring包
2.1:创建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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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.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">
//开启注解扫描
<context:component-scan base-package=
"cn.xlh.travael.service">
</context:component-scan>

//加载外部资源配置文件
<bean id="dataSource" class="
com.alibaba.druid.pool.DruidDataSource
">
  <property name="driverClassName"
value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql:
//127.0.0.1:3306/travel11"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
</beans>
--------------------------------------------------------
2.2:创建spring连接mybatis的配置文件applicationContext-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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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.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">
//装配SqlSessionFactory(会话工厂)
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean"
>
<property name="configLocation" value="classpath:
mybatis/mybatis-config.xml"></property>
<property name="dataSourcee" ref="dataSource"></property>
<property name="typeAliasesPackage" value="
cn.xlh.travel.pojo"></property>
<property name="mapperLocations" value="
classpath:mybatis/mappers/**/*.xml"></property>
</bean>

//装配mappper
<bean class="
org.mybatis.spring.mapper.MapperScannerConfigurer
">
</property name="basePackage" value="
cn.xlh.travel.mspper"></property>

</beans>
-----------------------------------------------------------------------------------
2.3:装配全局的事务配置文件applicationContext-tx.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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.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">
//xml配置事务的步骤
//1.装配事务管理器
//2.配置事务策略
//3.配置AOP
<bean id="transactionManager" class="
org.springframework.jdbc.datasource.DataSourceTransactionManager
"></bean>

<tx:advice id="txAdvice">
  <tx:attributes>
<tx:method name="*/">

</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="pt1"
expression="execution(* cn.itcast.travel.service.impl.*.*(..))"
></aop:poincut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1">
</aop:advisor>

</aop:config>
</beans>
------------------------------------------------------------------------------------
2.4:配置springMVC-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:p="http://www.springframework.org/schema/p"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
//开启注解扫描
<context:component-scan base-package="
cn.xlh.travel.web"></context:component-scan>

//配置注解驱动
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="defaultCharset" value="utf-8"></property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

//放行静态资源
<mvc:default-servlet-handler>
</mvc:default-servlet-handler>

</beans>
---------------------------------------------------
3:main-webapp-WEB-INF-web.xml
---------------------------------------------------
//---1Spring的监听器
<listener>
  <listener-class>
org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-parm>
<parm-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>

//---2配置前端控制器
<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/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>


//拦截
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--拦截所有请求,jsp除外-->
<url-pattern>/</url-pattern>
</servlet-mapping>


</web-app>
-------------------------------------
 









原文地址:https://www.cnblogs.com/xlhlx/p/10665499.html