Hill_wang的博客

Hello World

ssm搭建

SSM工程搭建步骤
1.使用maven创建一个工程项目
2.在prom.xml中引入核心架包依赖
1.myBatis
2.mybatis-spring
3.mysql
4.commons-dbcp
5.spring-orm
6.spring-webmvc
7.slf4j-log4j12
8.servlet-api
9.jsp-api
10.jstl
11.junit
 具体代码如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.hill</groupId>
  <artifactId>ssmDemo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1</version>
  <name>ssmDemo</name>
  <url>http://maven.apache.org</url>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.mybatis.version>3.1.1</org.mybatis.version>
    <org.mybatis-spring.version>1.2.2</org.mybatis-spring.version>
    <org.springframework.version>4.1.9.RELEASE</org.springframework.version>
    <org.slf4j.version>1.7.21</org.slf4j.version>
    <mysql.version>5.1.38</mysql.version>
    <commons.dbcp.version>1.4</commons.dbcp.version>
  </properties>


  <dependencies>
    <!-- DB start -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
      <scope>runtime</scope>
    </dependency>
    <!-- DB end -->


    <!-- commons start -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>${commons.dbcp.version}</version>
    </dependency>
    <!-- commons end -->


    <!-- MyBatis start -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${org.mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>${org.mybatis-spring.version}</version>
    </dependency>
    <!-- MyBatis end -->


    <!-- springframework start -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${org.springframework.version}</version>
      <exclusions>
        <exclusion>
          <artifactId>commons-logging</artifactId>
          <groupId>commons-logging</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
    <!-- springframework end -->
    <!-- log start -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${org.slf4j.version}</version>
    </dependency>
  <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
  </dependency>
  
    <!-- log end -->


    <!-- webapp start -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      <scope>runtime</scope>
    </dependency>
    <!-- webapp end -->
    <!-- alibaba fastjson包 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.46</version>
    </dependency>


    <!-- junit start -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- junit end -->
  </dependencies>


  <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.5.1</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>${project.build.sourceEncoding}</encoding>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <version>2.7</version>
          <configuration>
          <encoding>${project.build.sourceEncoding}</encoding>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.19.1</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.6</version>
        </plugin>
        <plugin>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-maven-plugin</artifactId>
          <version>9.3.7.v20160115</version>
          <configuration>
            <webApp>
              <contextPath>/${project.artifactId}</contextPath>
            </webApp>
          </configuration>
        </plugin>
      </plugins>
      <finalName>${project.artifactId}</finalName>
  </build>
</project>

3.在web.xml中配置spring的核心监听器和SpringMVC的servlet

 核心代码如下
<?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" version="2.5">
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- spring 核心监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 加载spring核心配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <context-param>
	<param-name>log4jConfigLocation</param-name>
	<param-value>classpath:log4j.properties</param-value>
  </context-param>
  <!-- springMVC 配置 -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等)
     		  若不配置,默认加载WEB-INF/servlet名称-servlet(springmvc-servlet.xml) -->
<!--         <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>springmvc</servlet-name>
    <!-- 拦截所有请求  /  -->
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  <!-- 字符编码过滤 -->
  <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>
  <!-- 配置404, 500 页面 -->
  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/view/error/404.html</location>
  </error-page>
  <error-page>
    <error-code>500</error-code>
    <location>/WEB-INF/view/error/500.html</location>
  </error-page>
</web-app>

 

4.创建实体类,编写实体类映射文件xxxMapper.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.hill.dao.UserDao">  
  <!-- 查询所有用户 -->  
  <select id="getAllUsers" resultType="com.hill.entity.User">  
    select * from user 
  </select>  
 
</mapper>  
5.创建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>
  <mappers>
    <!-- 配置映射文件 -->
    <mapper resource="com/hill/mapper/userMapper.xml"/>
  </mappers>
</configuration>
6.整合Spring-myBatis创建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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.1.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
  
  <!--支持注解  -->
  <context:annotation-config/>
  <!-- 组件扫描 -->
  <context:component-scan base-package="com.mipo">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
  </context:component-scan>


    <!-- 读取数据库属性资源文件(明文) -->
    <!-- <context:property-placeholder location="classpath:db.properties" /> -->
    <!-- 读取数据库加密属性资源文件(加密) -->  
    <bean id="myPropertyConfigurer"  
        class="com.mipo.commons.DecryptPropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list><value>classpath:en_db.properties</value></list>  
        </property>  
        <property name="fileEncoding" value="UTF-8"/>  
        <property name="keyLocation" value="classpath:key.k" />  
    </bean>  


  <!-- 定义数据源 -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
  </bean>


  <!-- 配置SqlSessionFactory -->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--引入数据源  -->
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:mybatis-config.xml" />
  </bean>


  <!-- 事务管理 -->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>
7.在WEB-INF目录下创建springMVC的核心配置文件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: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-4.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">




  <!-- 注册组件 -->
  <mvc:annotation-driven />
  <context:component-scan base-package="com.hill.controller">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>
  
  <!--静态方法过滤  -->
  <mvc:default-servlet-handler/>


  <!--视图管理器  -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
  </bean>
</beans>
8.测试spring和myBatis的整合,编写测试类
package com.hill;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mipo.service.UserService;

public class TestDemo {
	private static ClassPathXmlApplicationContext applicationContext = null;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		 applicationContext = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
	}


	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		 applicationContext.close();
	}

	@Test
	public void test() {
		  UserService userService = applicationContext.getBean("userService", UserService.class);
	        System.out.println(userService.getUser(1).getuName());
	}
}


阅读更多
文章标签: spring springmvc mybatis
个人分类: ssm
上一篇ehcache入门及与spring整合
下一篇mybatis配置
想对作者说点什么? 我来说一句

ssm框架的搭建

2017年09月05日 37KB 下载

ssm搭建的商城

2018年04月27日 20.61MB 下载

搭建SSM框架需要的jar包

2017年11月20日 19KB 下载

ssm搭建实例

2017年07月17日 9.15MB 下载

SSM框架搭建

2017年05月03日 12.48MB 下载

ssm框架快速搭建

2017年10月26日 21.47MB 下载

myeclipse平台maven搭建ssm例子.zip

2018年07月16日 49KB 下载

没有更多推荐了,返回首页

关闭
关闭