SSM框架整合(Spring+SpringMVC+MyBatis)及相关开发笔记

Spring+SpringMVC+MyBatis框架整合开发笔记,以下为SSM框架的简单案例代码及开发流程

SSM框架整合(Spring+SpringMVC+MyBatis)及相关开发笔记_第1张图片 

配置pom.xml,通过Maven注入SSM需要的相关依赖

 
        
        
            junit
            junit
            4.13.2
            test
        
        
        
            mysql
            mysql-connector-java
            8.0.25
        

        
        
            com.mchange
            c3p0
            0.9.5.5
        

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

        
        
            org.mybatis
            mybatis
            3.5.9
        
        
            org.mybatis
            mybatis-spring
            2.0.6
        

        
        
            org.springframework
            spring-webmvc
            5.3.14
        
        
            org.springframework
            spring-jdbc
            5.3.14
        
        
            org.aspectj
            aspectjweaver
            1.9.7
        
        
            org.springframework
            spring-tx
            5.3.14
        

        
        
            org.projectlombok
            lombok
            1.18.22
        
    
    

加上xml文件资源过滤,防止xml静态资源无法读取


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

配置完成pom.xml后,根据数据库编写相应的实体类就行了,如下使用lombok注解简化

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User{
    private int id;
    private String username;
}

DAO层

首先根据需求开发DAO层的接口,这里我简单的编写了一些常用的增删查改操作。

public interface UserMapper {
    
    int addUser(User user);
    
    //@Param("id")需与实体类和数据库中的字段相同
    int deleteUserById(@Param("id") int id);

    int updateUser(User user);

    User queryUserById(@Param("id") int id);

    User queryUserByName(@Param("username") String username);

    List queryAllUsers();
  
}

编写其实现类

//这里继承SqlSessionDaoSupport实现sqlSessionFactory注入
public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {

    @Override
    public int addUser(User user) {
        return getSqlSession().getMapper(UserMapper.class).addUser(user);
    }
    
    //...
}

编写MyBatis的mapper.xml






    
     
    
        insert into User (id,username)
        values (#{id},#{username});
    

   

    
    

配置database.properties数据库配置文件

#MySQL 8.0以上使用com.mysql.cj.jdbc.Driver,以下则使用com.mysql.jdbc.Driver
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/project?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456

配置MyBatis配置文件mybatis-config.xml




    
        
        
    
    
    
        
    

配置DAO层的配置文件spring-dao.xml




    
    

    
    

        
        
        
        
        

        
        
        
        
        
        
        
        
        
    

    
    

        
        

        
        

    
    
    
         
         
    

Service层

先编写service层接口及其实现类,调用Dao层即可

@Service
public class UserServiceImpl implements UserService{

    private UserMapper userMapper;
    //Spring通过set注入mapper
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }
       
    //........
}

配置Service层配置文件spring-service.xml



    
    
    
    
    
        
        
    

    
    
        
        
    

Controller层

编写controller, 首先注入service对象,然后根据需求编写需要的请求

@Controller
public class UserController {

    //自动装载service对象
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    //请求路径
    @RequestMapping("/allUsers")
    public String getAllUsers(Model model){

        //调用service层,返回值通过Model传给前端
        model.addAttribute("allUsers",userService.queryAllUsers());

        //返回视图名给视图解析器
        return "allUsers";
    }

    //........

}

配置spring-controller.xml



    
    
    

    
    

    
    

    
    
        
        
    

整合三层架构配置文件applicationContext.xml




    
    
    

配置web.xml



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

    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
    
    
        encodingFilter
        /*
    

你可能感兴趣的:(开发笔记,spring,java,springmvc,后端,mybatis)