1. 程式人生 > >SSM的簡單整合入門

SSM的簡單整合入門

handler 部分 lis rep times nvi javax str 反轉

1.首先我們得知道什麽是SSM?

  SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis兩個開源框架整合而成(SpringMVC是Spring中的部分內容)。常作為數據源較簡單的web項目的框架。

2.SSM能做什麽,能給我們帶來什麽呢?

Spring
  Spring就像是整個項目中裝配bean的大工廠,在配置文件中可以指定使用特定的參數去調用實體類的構造方法來實例化對象。也可以稱之為項目中的粘合劑。
  Spring的核心思想是IOC(控制反轉),即不再需要程序員去顯式地`new`一個對象,而是讓Spring框架幫你來完成這一切。   DI(依賴註入),在其它層引用對象時可以將其註入   AOP(面向切面編程),可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發的效率。
SpringMVC
  SpringMVC在項目中攔截用戶請求,它的核心Servlet即DispatcherServlet承擔中介或是前臺這樣的職責,將用戶請求通過HandlerMapping去匹配Controller,Controller就是具體對應請求所執行的操作。SpringMVC相當於SSH框架中struts。 Mybatis
  mybatis是對jdbc的封裝,它讓數據庫底層操作變的透明。mybatis的操作都是圍繞一個sqlSessionFactory實例展開的。mybatis通過配置文件關聯到各實體類的Mapper文件,Mapper文件中配置了每個類對數據庫所需進行的sql語句映射。在每次與數據庫交互時,通過sqlSessionFactory拿到一個sqlSession,再執行sql命令。 頁面發送請求給控制器,控制器調用業務層處理邏輯,邏輯層向持久層發送請求,持久層與數據庫交互,後將結果返回給業務層,業務層將處理邏輯發送給控制器,控制器再調用視圖展現數據。

  就像上面所說的,它是什麽就決定了它能做什麽

3.下面見識一下SSM的魅力吧

技術分享圖片

 3.1 整合思路

  1. 首先先搭建環境
  2. 先把spring的配置搭起來
  3. 用spring整合springmvc
  4. 用spring整合mybatis

 3.2需要的jar包

  

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target

>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mysql.version>5.1.6</mysql.version>
<mybatis.version>3.4.5</mybatis.version
>
</properties>

<dependencies>

<!-- spring -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>

<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.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>

<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

</dependencies>

  3.3 需要的實體類 com.a.domian

public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
}

  3.4 Dao層 com.a.dao

@Repository

public interface AccountDao{

  @Select("select * from account)

  List findAll();

  @Insert into account values(#{id},#{name},#{money})

  void saveAccount(Account account);

}

  3.5 service層 com.a.service

public interface AccountService(){

   List findAll();

   void saveAccount();  

}

實現類:

@Service

public Class AccountServiceImpl implement AccountService{

    @Autowired

    private AccountDao accountdao;

  public List fndAll(){

   return accountDao.findAll();

}

  public void saveAccount(Account account){

   accountDao.saveAccount(account);

}

}

  

  3.6 AccountController類 com.a.controller

@Controller

@RequestMapping("/Account")

public class AccountController{

  @Autowired

  private AccountService accountService;

  

@RequestMapping(path = "/findAll")

  public String findAll(){ 

  List<Account> list = accountService.findAll();
for (Account account : list) {
System.out.println(account);
}
return "success";

}

@RequestMapping(path = "/save")

public String saveAccount(Account account){

  accountService.saveAccount(account);

return "success";

}

}

  3.7 Mybatis環境

在resources下配置一個

SqlMapConfig.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>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///ssm"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>

<!-- 使用的是註解 -->
<mappers>
<!-- <mapper class="com.itheima.ssm.dao.AccountDao"/> -->
<!-- 該包下所有的dao接口都可以使用 -->
<package name="com.a.dao"/>
</mappers>
</configuration>

  3.8 spring環境

applicationContext.xml

<context:component-scan base-package="com.a.service">

</context:component-scan>

<import resource="SqlMapConfig.xml">

log4j.properties(日誌輸出)

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=info, CONSOLE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:/axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

  3.9 SpringMVC環境

web.xml

  

<!-- 配置前端控制器:服務器啟動必須加載,需要加載springmvc.xml配置文件, SpringMVC的容器>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化參數,創建完DispatcherServlet對象,加載springmvc.xml配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 服務器啟動的時候,讓DispatcherServlet對象創建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 配置解決中文亂碼的過濾器 -->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

resources下,創建springmvc.xml

    

context:component-scan base-package="com.a.controller>
</context:component-scan>

<!-- 配置視圖解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- JSP文件所在的目錄 -->
<property name="prefix" value="/WEB-INF/pages/" />
<!-- 文件的後綴名 -->
<property name="suffix" value=".jsp" />
</bean>

<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />

<!-- 開啟對SpringMVC註解的支持 -->
<mvc:annotation-driven />

<import resource="applicationContext.xml">

SSM的簡單整合入門