整合SSM


1.新建maven项目

2.导入依赖

  
<dependencies>
   
   <dependency>
       <groupId>junitgroupId>
       <artifactId>junitartifactId>
       <version>4.12version>
   dependency>
   
   <dependency>
       <groupId>mysqlgroupId>
       <artifactId>mysql-connector-javaartifactId>
       <version>5.1.47version>
   dependency>
   
   <dependency>
       <groupId>com.mchangegroupId>
       <artifactId>c3p0artifactId>
       <version>0.9.5.2version>
   dependency>

   
   <dependency>
       <groupId>javax.servletgroupId>
       <artifactId>servlet-apiartifactId>
       <version>2.5version>
   dependency>
   <dependency>
       <groupId>javax.servlet.jspgroupId>
       <artifactId>jsp-apiartifactId>
       <version>2.2version>
   dependency>
   <dependency>
       <groupId>javax.servletgroupId>
       <artifactId>jstlartifactId>
       <version>1.2version>
   dependency>

   
   <dependency>
       <groupId>org.mybatisgroupId>
       <artifactId>mybatisartifactId>
       <version>3.5.2version>
   dependency>
   <dependency>
       <groupId>org.mybatisgroupId>
       <artifactId>mybatis-springartifactId>
       <version>2.0.2version>
   dependency>

   
   <dependency>
       <groupId>org.springframeworkgroupId>
       <artifactId>spring-webmvcartifactId>
       <version>5.1.9.RELEASEversion>
   dependency>
   <dependency>
       <groupId>org.springframeworkgroupId>
       <artifactId>spring-jdbcartifactId>
       <version>5.1.9.RELEASEversion>
   dependency>
dependencies>
maven依赖

3.maven的资源过滤设置

  
<build>
   <resources>
       <resource>
           <directory>src/main/javadirectory>
           <includes>
               <include>**/*.propertiesinclude>
               <include>**/*.xmlinclude>
           includes>
           <filtering>falsefiltering>
       resource>
       <resource>
           <directory>src/main/resourcesdirectory>
           <includes>
               <include>**/*.propertiesinclude>
               <include>**/*.xmlinclude>
           includes>
           <filtering>falsefiltering>
       resource>
   resources>
build>
maven资源过滤

4.建立基本框架以及mybaits的配置和spring的配置

  
<?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>

configuration>
mybaits基本配置   
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

beans>
Spring基本配置

5.mybaits的配置,spring整合mybaits

  配置文件中最好用jdbc.xxx

  另外不要用username,数据源配置中也注意!!

  
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybaits?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
数据源配置文件

   mybaits核心配置:别名和关联mapper.xml。数据源交给spring来做

  
<?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.kuang.pojo"/>
   typeAliases>
   <mappers>
       <mapper resource="com/kuang/dao/BookMapper.xml"/>
   mappers>

configuration>
mybaits核心配置

  编写实体类和mapper接口,注意接口中跟数据库中字段不一样时候要用@Param()注解实现

    例如:int deleteBookById(@Param("bookID") int id);

  
package com.kuang.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
   
   private int bookID;
   private String bookName;
   private int bookCounts;
   private String detail;
   
}
实体类   
package com.kuang.dao;

import com.kuang.pojo.Books;
import java.util.List;

public interface BookMapper {

   //增加一个Book
   int addBook(Books book);

   //根据id删除一个Book
   int deleteBookById(int id);

   //更新Book
   int updateBook(Books books);

   //根据id查询,返回一个Book
   Books queryBookById(int id);

   //查询全部Book,返回list集合
   List queryAllBook();

}
mapper接口

  编写mapper接口的mybaits配置文件

  
<?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">



   
   
      insert into ssmbuild.books(bookName,bookCounts,detail)
      values (#{bookName}, #{bookCounts}, #{detail})
   

   
   
      delete from ssmbuild.books where bookID=#{bookID}
   

   
   
      update ssmbuild.books
      set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}
      where bookID = #{bookID}
   

   
   

   
   

BookMapper.xml

6.service层的接口和实现类

  接口:

  
package com.kuang.service;

import com.kuang.pojo.Books;

import java.util.List;

//BookService:底下需要去实现,调用dao层
public interface BookService {
   //增加一个Book
   int addBook(Books book);
   //根据id删除一个Book
   int deleteBookById(int id);
   //更新Book
   int updateBook(Books books);
   //根据id查询,返回一个Book
   Books queryBookById(int id);
   //查询全部Book,返回list集合
   List queryAllBook();
}
service层接口

  实现类:定义mapper私有属性,并设置set方法。方便spring的管理

  
package com.kuang.service;

import com.kuang.dao.BookMapper;
import com.kuang.pojo.Books;
import java.util.List;

public class BookServiceImpl implements BookService {

   //调用dao层的操作,设置一个set接口,方便Spring管理
   private BookMapper bookMapper;

   public void setBookMapper(BookMapper bookMapper) {
       this.bookMapper = bookMapper;
  }
   
   public int addBook(Books book) {
       return bookMapper.addBook(book);
  }
   
   public int deleteBookById(int id) {
       return bookMapper.deleteBookById(id);
  }
   
   public int updateBook(Books books) {
       return bookMapper.updateBook(books);
  }
   
   public Books queryBookById(int id) {
       return bookMapper.queryBookById(id);
  }
   
   public List queryAllBook() {
       return bookMapper.queryAllBook();
  }
}
service实现类

7.Spring整合mybaits。注意数据源配置要用classpath:

  注意配置文件中  不要用username  要用user

  
<?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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">

   
   
   <context:property-placeholder location="classpath:database.properties"/>

   
   
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       
       <property name="driverClass" value="${jdbc.driver}"/>
       <property name="jdbcUrl" value="${jdbc.url}"/>
       <property name="user" value="${jdbc.username}"/>
       <property name="password" value="${jdbc.password}"/>

       
       <property name="maxPoolSize" value="30"/>
       <property name="minPoolSize" value="10"/>
       
       <property name="autoCommitOnClose" value="false"/>
       
       <property name="checkoutTimeout" value="10000"/>
       
       <property name="acquireRetryAttempts" value="2"/>
   bean>

   
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       
       <property name="dataSource" ref="dataSource"/>
       
       <property name="configLocation" value="classpath:mybatis-config.xml"/>
   bean>

   
    
   
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
       
       <property name="basePackage" value="com.kuang.dao"/>
   bean>

beans>
Spring整合mybaits

===================================以上spring-dao层结束===========================

8.Spring-service层

  
<?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"
      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">

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

   
   <bean id="BookServiceImpl" class="com.kuang.service.BookServiceImpl">
       <property name="bookMapper" ref="bookMapper"/>
   bean>

   
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       
       <property name="dataSource" ref="dataSource" />
   bean>

beans>
Spring-service层配置

===================================以上spring-service层结束=========================

9.Spring-mvc层

  9.1web项目:注意这里的DispatcherServlet配置中的spring配置文件是总的配置文件applicationContext.xml

  
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
        version="4.0">

   
   <servlet>
       <servlet-name>DispatcherServletservlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
       <init-param>
           <param-name>contextConfigLocationparam-name>
             
           <param-value>classpath:applicationContext.xmlparam-value>
       init-param>
       <load-on-startup>1load-on-startup>
   servlet>
   <servlet-mapping>
       <servlet-name>DispatcherServletservlet-name>
       <url-pattern>/url-pattern>
   servlet-mapping>

   
   <filter>
       <filter-name>encodingFilterfilter-name>
       <filter-class>
          org.springframework.web.filter.CharacterEncodingFilter
       filter-class>
       <init-param>
           <param-name>encodingparam-name>
           <param-value>utf-8param-value>
       init-param>
   filter>
   <filter-mapping>
       <filter-name>encodingFilterfilter-name>
       <url-pattern>/*url-pattern>
   filter-mapping>
   
   
   <session-config>
       <session-timeout>15session-timeout>
   session-config>
   
web-app>
web.xml配置

  9.2spring-mvc.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.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc
   https://www.springframework.org/schema/mvc/spring-mvc.xsd">

   
   
   <mvc:annotation-driven />
   
   <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/jsp/" />
       <property name="suffix" value=".jsp" />
   bean>

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

beans>
spring-mvc.xml

  三层架构很明显  

   后续功能只需要写前台跟后台的controller即可