首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

SpringMVC + Spring + MyBatis 整合 SSM 框架搭建完整教程

     編輯:關於C++

項目結構

src/main/java
├── com.example.ssm
│   ├── config
│   │   ├── mybatis
│   │   └── spring
│   ├── controller
│   ├── service
│   ├── mapper
│   └── pojo
src/main/resources
├── db.properties
├── log4j.properties
└── mybatis-config.xml
src/main/webapp/WEB-INF
├── views
└── web.xml

核心配置文件

1. db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root

2. mybatis-config.xml




    

3. Spring DAO 層 applicationContext-dao.xml




    

    
    
        
        
        
        
        
        
    

    
    
        
        
    

    
    
        
        
    

4. Spring Service 層 applicationContext-service.xml

5. Spring 事務 applicationContext-transaction.xml


    


    
        
        
        
        
    


    
    

6. SpringMVC springmvc.xml




    
    

7. web.xml


    contextConfigLocation
    classpath:com/example/ssm/config/spring/applicationContext-*.xml


    org.springframework.web.context.ContextLoaderListener



    dispatcher
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:com/example/ssm/config/spring/springmvc.xml
    
    1


    dispatcher
    /



    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    encodingUTF-8

encodingFilter/*

代碼層實現示例

// POJO
public class User {
    private Integer id;
    private String username, sex;
    // getter/setter
}

// Mapper 接口
public interface UserMapper {
    List<User> findAll();
}

// Mapper XML

    


// Service
@Service
public class UserServiceImpl implements UserService {
    @Autowired UserMapper userMapper;
    public List<User> findAll() { return userMapper.findAll(); }
}

// Controller
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired UserService userService;
    @RequestMapping("/list")
    public String list(Model model) {
        model.addAttribute("users", userService.findAll());
        return "user/list";
    }
}

關鍵點總結

  • 配置文件模塊化:DAO、Service、Transaction、MVC 分離
  • MapperScannerConfigurer 自動掃描接口生成代理
  • AOP 事務攔截 Service 層方法
  • CharacterEncodingFilter 解決 POST 亂碼
  • ViewResolver 配置 JSP 物理路徑
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved