SSM 基本整合

 1、项目结构

2、引入jar包

 

3、数据库属性文件db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

日志:log4j.propertis

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.huaxin=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

4.mybatis配置文件: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>
        <package name="com.sjzc.pojo" />
    </typeAliases>
    <!--配置Mapper的位置 -->
	<mappers> 
       <!-- <mapper resource="com/sjzc/pojo/CustomerMapper.xml" /> -->
       <!-- Mapper接口开发方式  -->
	   <mapper resource="com/sjzc/mapper/CustomerMapper.xml" />
      
	</mappers>
</configuration>

5、spring配置文件: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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
   
    <!--读取db.properties -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
		<!--数据库驱动 -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<!--连接数据库的url -->
		<property name="url" value="${jdbc.url}" />
		<!--连接数据库的用户名 -->
		<property name="username" value="${jdbc.username}" />
		<!--连接数据库的密码 -->
		<property name="password" value="${jdbc.password}" />
		<!--最大连接数 -->
		<property name="maxTotal" value="${jdbc.maxTotal}" />
		<!--最大空闲连接 -->
		<property name="maxIdle" value="${jdbc.maxIdle}" />
		<!--初始化连接数 -->
		<property name="initialSize" value="${jdbc.initialSize}" />
	</bean>
	<!-- 事务管理器,依赖于数据源 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!--开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	<!--配置MyBatis工厂 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!--注入数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!--指定核心配置文件位置 -->
		<property name="configLocation" value="classpath:mybatis-config.xml" />
	</bean>

	<!--实例化Dao -->
	<bean id="customerDao" class="com.sjzc.dao.CustomerDao">
		<!-- 注入SqlSessionFactory对象实例 -->
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>
	<!-- Mapper代理开发(基于MapperFactoryBean) -->
	<!-- <bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
		<property name="mapperInterface" value="com.huaxin.mapper.CustomerMapper" 
		/> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> -->
	<!-- Mapper代理开发(基于MapperScannerConfigurer):自动配置接口的mapper代理和注入sqlSessionFactory -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.sjzc.mapper" />
	</bean>
	
	<!-- 开启扫描  -->
	<context:component-scan base-package="com.sjzc.*" />
	
</beans>

6.springmvc配置文件: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: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-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
    <!-- 扫描controller --> 
	<context:component-scan base-package="com.sjzc.controller"/>
	 
	<!-- 三大组件 -->
	
	<!-- 处理器映射器和适配器 -->
	
	<mvc:annotation-driven/>
	
	<!-- 视图解析器 -->
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		 
		<property name="prefix" value="WEB-INF/jsp/"></property>
		 
		<property name="suffix" value=".jsp"></property>
	</bean>
 </beans>

7、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>TestSSM</display-name>
  <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>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <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>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

猜你喜欢

转载自blog.csdn.net/xiaoshuo566/article/details/85129356