2019IDEA整合(Spring+SpringMVC+Mybatis+Maven)及常见异常处理

git地址:https://github.com/UserFengFeng/Maven-Spring-SpringMVC-Mybatis-.git

追加遗漏:创建person表,字段:id,name,gender,address,birthday

                  person实体类跟mapping映射文件没有贴出来是因为逆向工程就自动生成了,并且贴了git地址,可自行download

1.创建项目

2019IDEA整合(Spring+SpringMVC+Mybatis+Maven)及常见异常处理_第1张图片

 2019IDEA整合(Spring+SpringMVC+Mybatis+Maven)及常见异常处理_第2张图片

 2019IDEA整合(Spring+SpringMVC+Mybatis+Maven)及常见异常处理_第3张图片

2019IDEA整合(Spring+SpringMVC+Mybatis+Maven)及常见异常处理_第4张图片

2019IDEA整合(Spring+SpringMVC+Mybatis+Maven)及常见异常处理_第5张图片

第一次创建下载依赖比较慢,耐心等待下载完毕。

我的目录结构

2019IDEA整合(Spring+SpringMVC+Mybatis+Maven)及常见异常处理_第6张图片

2.pom文件添加依赖包




    4.0.0

    zhou
    SSM
    1.0-SNAPSHOT
    war

    SSM Maven Webapp
    
    http://www.example.com

    
        5.0.2.RELEASE
        1.6.6
        1.2.12
        8.0.16
        3.4.5
    

    
        
        
            org.aspectj
            aspectjweaver
            1.6.8
        

        
            org.springframework
            spring-aop
            ${spring.version}
        

        
            org.springframework
            spring-context
            ${spring.version}
        

        
            org.springframework
            spring-web
            ${spring.version}
        

        
            org.springframework
            spring-webmvc
            ${spring.version}
        

        
            org.springframework
            spring-test
            ${spring.version}
        

        
            org.springframework
            spring-tx
            ${spring.version}
        

        
            org.springframework
            spring-jdbc
            ${spring.version}
        

        
            junit
            junit
            4.12
        

        
            mysql
            mysql-connector-java
            ${mysql.version}
        

        
            javax.servlet
            servlet-api
            2.5
            provided
        

        
            javax.servlet.jsp
            jsp-api
            2.0
            provided
        

        
            jstl
            jstl
            1.2
        

        
        
            log4j
            log4j
            ${log4j.version}
        

        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        

        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        

        
            org.mybatis
            mybatis-spring
            1.3.0
        

        
        
            com.alibaba
            druid
            1.1.10
        

    

    
        ssm
        
            
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    3.2
                    
                        1.8
                        1.8
                        UTF-8
                        true
                    
                
            
        
    

3.spring-mybatis.xml




    
        
        
    


    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

4.mybatis逆向生成 generatorConfig.xml配置(便可执行生成映射文件即可https://blog.csdn.net/qq_36957885/article/details/98473244这篇中有讲如何生成)

 






    
    


    
    

    

        
        
            
            
            
        

        
        
        

        
        
            
        

        
        
            
            
        


        
        
            
        

        
        
        
        
        

        
        

        

5.数据库配置文件generator.properties

#jar路径配置本地的
jdbc.driverLocation=C:\\Users\\MrZhou\\.m2\\repository\\mysql\\mysql-connector-java\\8.0.16\\mysql-connector-java-8.0.16.jar
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
jdbc.userId=root
jdbc.password=root

6.log文件配置log4j.properties

# Configure logging for testing: optionally with log file
log4j.rootLogger=WARN, stdout
# log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.stdout.Encoding=UTF-8

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

7.beans.xml配置



    
    
    
    

    
    

    
    
        
        
        
        
    

    
    
        
        

        
        
    

    
    
        
        
    

    
    
    
        
    

    
    
        
        
            
            
            
            
            
            
        
    

    
    
        
    


8.spring-mvc.xml

 

 




    
    


    
    
        
        
    

    

    
    

9.web.xml配置

 





    Archetype Created Web Application

    
    
        contextConfigLocation
        classpath:beans.xml
    

    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        
        1
    
    
        dispatcherServlet
        /
    

    
    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        characterEncodingFilter
        /*
    

10.dao层接口PersonDao

package com.dao;

import com.model.Person;

import java.util.List;

public interface PersonDao {
    List selectAll();
}

11.service层PersonService

package com.service;

import com.model.Person;

import java.util.List;

public interface PersonService {
    List selectAll();
}

12.ServcieImpl层PersonServiceImpl

package com.service.impl;

import com.dao.PersonDao;
import com.model.Person;
import com.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//名字可以不加,默认就是首字母小写的类名
//@Service("personService")
@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonDao personDao;

    @Override
    public List selectAll() {

        return personDao.selectAll();
    }
}

13.controller层PersonController

package com.controller;

import com.model.Person;
import com.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/aa")
public class TestController {
    @Autowired
    private PersonService personService;

    @RequestMapping("/test")
    public String test(){
        List list = personService.selectAll();
        for (Person p: list
             ) {
            System.out.println(p);
        }
        return "test";
    }
}

14.测试类

package com.controller;

import com.dao.PersonDao;
import com.model.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:beans.xml"})
public class PersonDaoTest {

    @Autowired
    private PersonDao personDao;

    @Test
    public void selectAll() {
        List list = personDao.selectAll();
        for (Person p: list
        ) {
            System.out.println(p.toString());
        }
    }
}

15.运行

2019IDEA整合(Spring+SpringMVC+Mybatis+Maven)及常见异常处理_第7张图片

2019IDEA整合(Spring+SpringMVC+Mybatis+Maven)及常见异常处理_第8张图片

2019IDEA整合(Spring+SpringMVC+Mybatis+Maven)及常见异常处理_第9张图片2019IDEA整合(Spring+SpringMVC+Mybatis+Maven)及常见异常处理_第10张图片

 

 

忽略此处log乱码,懒得没改编码

2019IDEA整合(Spring+SpringMVC+Mybatis+Maven)及常见异常处理_第11张图片

16.遇到异常的处理

https://blog.csdn.net/qq_36957885/article/details/99344688

看到这了,点个赞呗!!!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(java)