Mybatis, Spring , SpringMVC框架SSM整合


SSM整合

Maven依赖包

        
        
            junit
            junit
            4.13
            test
        

        
        
        
            org.springframework
            spring-webmvc
            5.3.16
        

        
        
        
            org.aspectj
            aspectjweaver
            1.9.4
        

        
        
        
            org.mybatis
            mybatis-spring
            2.0.6
        

        
        
            mysql
            mysql-connector-java
            8.0.21
        

        
        
        
            org.springframework
            spring-jdbc
            5.3.16
        

        
        
            com.mchange
            c3p0
            0.9.5.2
        

        
        
            org.mybatis
            mybatis
            3.5.7
        

        
        
            javax.servlet
            servlet-api
            2.5
        
        
            javax.servlet.jsp
            jsp-api
            2.1
        
        
            javax.servlet
            jstl
            1.2
        

Maven资源过滤文件


   
       
           src/main/java
           
               **/*.properties
               **/*.xml
           
           false
       
       
           src/main/resources
           
               **/*.properties
               **/*.xml
           
           false
       
   

pojo实体类

实体类User

package com.pojo;

public class User {
    private int id;
    private String name;
    private String pwd;

    public User() {
    }

    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }

 /* getter 和 setter  */
}

dao层

数据库相关配置与Mybatis

数据库配置文件

database.properties

# 注意! 使用了 c3p0 数据库链接池的属性要设置为 jdbc.属性名,不加会报错!
jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
mybati核心配置

mybatis-config.xml , 也可以进行mybatis其他的设置,如起别名等

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


    
        
        
    

Spring中dao层配置文件

spring-dao.xml



    
    
    

    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
    

    
    
    
        
        
        
        
    

dao接口 UserMapper

package com.dao;
import com.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;

public interface UserMapper {
    List getAllUser();

    int addUser(User user);

    int deleteUser(@Param("id") int id);

    int updateUser(User user);

    User getUser(@Param("id") int id);
}

接口映射器(sql配置)

如果是配置文件,最好和接口同包同名 , 也可以在接口上使用注解

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


    
        insert into `user`
        values (#{id}, #{name}, #{pwd});
    
    
    
        delete from `user` where id=#{id}
    

    
        update `user`
        set id = #{id},name = #{name},pwd = #{pwd}
        where id = #{id}
    

    

    

service层(业务层)

Service接口 ,非必需,根据项目情况而定

package com.service;

import com.pojo.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

interface UserService {
    List getAllUser();

    int addUser(User user);

    int deleteUser(@Param("id") int id);

    int updateUser(User user);

    User getUser(@Param("id") int id);
}

Service实现类

ServiceImp

package com.service;

import com.dao.UserMapper;
import com.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImp implements UserService {

    /*
    业务层调用dao层(Mapper对象)来执行对应的数据库操作; 对应接口的MapperBean由Spring的MapperScannerConfigurer生成
    由于UserMapper的映射器对象已经在spring-dao.xml配置中用扫描器注册了,所以可以使用自动装配
    此外: // 官方不建议字段自动装配,而是推荐setter注入更灵活
     */
    @Autowired
    @Qualifier("userMapper")
    private UserMapper userMapper;
    
    @Override
    public List getAllUser() {
        return userMapper.getAllUser();
    }
    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }
    @Override
    public int deleteUser(int id) {
        return userMapper.deleteUser(id);
    }
    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }
    @Override
    public User getUser(int id) {
        return userMapper.getUser(id);
    }
}

Spring中Service层配置文件

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


    
    
    

    
    
    
        
        
    

controller层

控制器

UserController.java

package com.controller;

import com.pojo.User;
import com.service.UserServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    /* controller层调用业务层,
     业务层通过 @Service 注解在Spring中注入了
     这里自动装配 业务层实现类(bean对象)
     控制层通过调用业务层实现类来进行对应的业务操作
     */
    @Autowired  // 官方不建议字段注入 自动装配,而是推荐setter注入更灵活更安全
    @Qualifier("userServiceImp")
    private UserServiceImp mapperImp;

    //由于类也有url,所以请求的url为:user/getAllUser
    @RequestMapping(value = "/getAllUser",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String getAllUser(){

        String date ="";
        List users = mapperImp.getAllUser();
        for (User user:users){
            date+=user+"
" ; System.out.println(user); } //返回数据 return date; } }

Spring中的控制层配置文件

spring-controller.xml

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


    
    

    
    
    
    
    

    
    
        
        
        
    

Spring整合所有应用上下文配置

applicationContext.xml

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

    
       
         
        

web配置

配置整个web应用程序的配置文件``web.xml`

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


    
    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            
            classpath:applicationContext.xml
        
        1
    
    
        DispatcherServlet
        /
    

    
    
        encodingFilter
        
            org.springframework.web.filter.CharacterEncodingFilter
        
        
            encoding
            utf-8
        
    
    
        encodingFilter
        /*
    

    
    
        15
    

至此,所有的配置都做完了,完结撒花 ??????