1. 程式人生 > >SSM專案整合

SSM專案整合

1、新增jar包

2、web.xml檔案 

<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ssm_demo</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  
  <!-- 讀取主配置檔案
  	context-param >>  listener >> filter >>  servlet 
  	context-param:被例項化後轉化為鍵值對,並交給了SerlvetContext
  	-->
  <context-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>
  			classpath:applicationContext.xml
  		</param-value>
  </context-param>
  
   <!-- 該物件的作用是從SerlvetContext物件中獲取鍵值對,並載入spring的配置檔案 -->
  <listener>
  		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <filter>
  	<filter-name>characterEncoding</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>
  	 	<!-- 設定值為true,只是用當前設定的編碼格式。而其他位置的
  		編碼格式設定失效 -->
  	<param-value>true</param-value>
  </init-param>
  </filter>
  <filter-mapping>
  		<filter-name>characterEncoding</filter-name>
  		<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  <servlet>
  		<servlet-name>DispatcherServlet</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>
  </servlet>
  <servlet-mapping>
  		<servlet-name>DispatcherServlet</servlet-name>
  		<url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

3、配置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:util="http://www.springframework.org/schema/util"  
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd ">
        
      <!-- 不攔截靜態資源 -->
      <mvc:resources location="/js/" mapping="/js/*"/>
      <mvc:resources location="/css/" mapping="/css/*"/>
      <mvc:resources location="/html/" mapping="/html/*"/>
      
      
      <!-- 啟用IOC掃描,只掃描controller層 -->   
      <context:component-scan base-package="com.huaruan.ssm.controller"></context:component-scan>
      
      <!-- 使用標記,引入對映器,介面卡 -->
      <mvc:annotation-driven></mvc:annotation-driven>
      
      
</beans>

4、配置application.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:util="http://www.springframework.org/schema/util"  
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd ">
        
     
     <!-- 配置DataSource資料來源 ,使用c3p0連線池-->   
     <bean  id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
     	<property name="driverClass"  value="com.mysql.jdbc.Driver"></property>
     	<property name="jdbcUrl" 
     				value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull"></property>
     	<property name="user" value="root"></property>
     	<property name="password" value="123456"></property>
     	<!-- 指定連線資料庫連線池的最小連線數 -->
		<property name="minPoolSize" value="10"></property>
		<!-- 指定連線資料庫連線池的最大連線數 -->
		<property name="maxPoolSize" value="20"></property>
		<!--初始化時獲取三個連線,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
		<property name="initialPoolSize" value="15"></property>
		<!-- 指定連線資料庫連線池的連線的最大空閒時間 -->
		<property name="maxIdleTime" value="120"></property>
		<!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。Default: 3 -->
		<property name="acquireIncrement" value="5"></property>
		<property name="maxStatements" value="100"></property>
		<!--每60秒檢查所有連線池中的空閒連線。Default: 0 -->
		<property name="idleConnectionTestPeriod" value="60"></property>
		<property name="automaticTestTable" value="c3p0testtable"></property>
     	
     </bean>
     
     <!-- 建立SqlSessionFactory,並同時指定資料來源 -->
     <bean id="sqlSessionFactory" 
     	class="org.mybatis.spring.SqlSessionFactoryBean">
     	<!-- name 值是固定的,是 org.mybatis.spring.SqlSessionFactoryBean 中的引數
     		ref 的值對應的是上面配置的資料來源的id,這個是自己定義的	-->
     	<property name="dataSource"  ref="dataSource"></property>
     </bean>
     	
     <!-- 載入mybatis的對映檔案,並通過對應的介面自動生成實現類 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     	<property name="sqlSessionFactory"  ref="sqlSessionFactory"></property>
     	<property name="basePackage" value="com.huaruan.ssm.dao"></property>
     </bean>
     
     
     <!-- 事務管理 ....-->
     
     
     <!-- 啟用元件掃描,掃描com包下的類,但不掃描com.isoft.controller包當中類 -->
     <context:component-scan base-package="com.huaruan.ssm">
     		<context:exclude-filter type="annotation" 
     			expression="org.springframework.stereotype.Controller"/>
     </context:component-scan>
     
</beans>

5、dao層

(1)介面

import org.apache.ibatis.annotations.Param;

public interface UserMapper {
	public void deleteUser(@Param("uid") int id);
	
}

(2)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="com.test.ssm.dao.UserMapper">

<delete id="deleteUser" >
	delete from user where uid = #{uid}
</delete>
</mapper>

6、service層

(1)介面

public interface UserService {
	boolean deleteUser(int id);
}

(2)實現類

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.huaruan.ssm.dao.UserMapper;

@Service
public class UserServiceImpl implements UserService {

	@Resource
	private UserMapper userMapper;
	
	@Override
	public boolean deleteUser(int uid) {
		try {
			userMapper.deleteUser(uid);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}