SSM整合


SSM整合

一、整合 spring与springMVC

Ⅰ)整合web层

1、创建web动态工程

2、导入spring包与配置文件

spring包
  - com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
  - com.springsource.org.aopalliance-1.0.0.jar
  - com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
  - com.springsource.org.apache.commons.logging-1.1.1.jar
  - com.springsource.org.apache.commons.pool-1.5.3.jar
  - com.springsource.org.apache.log4j-1.2.15.jar
  - com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
  - druid-1.1.9.jar
  - hamcrest-core-1.3.jar
  - spring-aop-5.0.7.RELEASE.jar
  - spring-aspects-5.0.7.RELEASE.jar
  - spring-beans-5.0.7.RELEASE.jar
  - spring-context-5.0.7.RELEASE.jar
  - spring-core-5.0.7.RELEASE.jar
  - spring-expression-5.0.7.RELEASE.jar
  - spring-jdbc-5.0.7.RELEASE.jar
  - spring-orm-5.0.7.RELEASE.jar
  - spring-test-5.0.7.RELEASE.jar
  - spring-tx-5.0.7.RELEASE.jar
  - spring-web-5.0.7.RELEASE.jar
配置文件 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>

    

    
    

    
    

3、在web.xml当中配置spring监听器

<?xml version="1.0" encoding="UTF-8"?>

    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
         contextConfigLocation
        classpath:applicationContext.xml
    

4、添加springMVC相关jar包与配置文件

相关jar包

  - spring-webmvc-5.0.7.RELEASE.jar

配置文件 springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>


    
    
        
        
    

    
    
    

    
    
        
        
    

5、在web.xml当中配置springMVC前端控制器和编码



    encoding
    org.springframework.web.filter.CharacterEncodingFilter
    
    
        encoding
        UTF-8
    



    encoding
    /*




    mySpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
    
    
        contextConfigLocation
        classpath:springmvc.xml
    
    1



    mySpringMVC
    
    /

6、测试springMVC

创建form表单

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  
    index
  
  
  

客户名称:

客户职业:

客户电话:

客户邮件:

CustomerController

@Controller
public class CustomerController {

    @RequestMapping("addcustomer")
    public String addCustomer(){
        return "/result.jsp";
    }

}

result.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


    

结果页

Ⅱ)整合 Service 层

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  
    index
  
  
  

客户名称:

客户职业:

客户电话:

客户邮件:

Customer

@Getter@Setter@ToString
public class Customer {
    private Integer cust_id;
    private String cust_name;
    private String cust_profession;
    private String cust_phone;
    private String email;
}

CustomerService

public interface CustomerService {
    void saveCustomer(Customer customer);
}

CustomerServiceImpl

@Service("CustomerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {

    @Override
    public void saveCustomer(Customer customer) {
        System.out.println("来到了业务层--" + customer);
    }
}

CustomerController

@Controller
public class CustomerController {

    /* 注入业务层 */
    @Autowired
    private CustomerService customerService;

    @RequestMapping("addcustomer")
    public String addCustomer(Customer customer){
        System.out.println("来到web层--" + customer);
        customerService.saveCustomer(customer);
        return "/result.jsp";
    }
}

Ⅲ )整合 dao 层

1、添加Mybatis相关jar包

  - ant-1.10.3.jar
  - ant-launcher-1.10.3.jar
  - asm-7.0.jar
  - cglib-3.2.10.jar
  - commons-logging-1.2.jar
  - javassist-3.24.1-GA.jar
  - log4j-1.2.17.jar
  - log4j-api-2.11.2.jar
  - log4j-core-2.11.2.jar
  - mybatis-3.5.3.jar
  - mybatis-spring-1.3.2.jar
  - ognl-3.2.10.jar
  - slf4j-api-1.7.26.jar
  - slf4j-log4j12-1.7.26.jar

2、添加Mybatis配置文件和数据库属性文件

sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>


    
        
    
    
        
        
    


db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

3、创建CustomerMapper

CustomerMapper

public interface CustomerMapper {
    void insertCustomer(Customer customer);
}

CustomerMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>




    
        insert into `customer`(cust_name,cust_profession,cust_phone,email)
        values (#{cust_name},#{cust_profession},#{cust_phone},#{email})
    



4、在applicationContext配置文件中添加Mybatis数据库相关配置信息

<?xml version="1.0" encoding="UTF-8"?>



    
    
    
    
    
    
    
    
        
        
        
        
        
    
    
    
        
        
    
    
    

    
    
        
        
        
        
        
    
    
    
        
        
    

5、调用dao层

@Service("CustomerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void saveCustomer(Customer customer) {
        System.out.println("来到了业务层--" + customer);
        // 调用dao层
        customerMapper.insertCustomer(customer);
    }
}