Maven多模块项目搭建+SSM框架整合(三、配置文件添加,服务层测试)

四、配置文件添加

我们需要添加相关配置文件applicationContext.xml、jdbc.properties、log4j.properties、mybatis-config.xml、spring-mvc.xml

(1)applicationContext.xml




    
    

    
    

    
    
        
        
        
        
        

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
    

    
    
        
        
    

    
        
    
    
        
            
            
            
            
            

            
            
            
            

            
        
    

    
        
        
        
    

(2)jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

(3)log4j.properties

### set log levels ###
#log4j.rootLogger = debug , stdout , D , E
log4j.rootLogger = DEBUG , stdout

###  output to the console ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n

### Output to the log file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ${mytest_one.root}/WEB-INF/logs/error.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd 
log4j.appender.ServerDailyRollingFile.File=${mytest_one.root}/WEB-INF/logs/error.log
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout 
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern= %-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n 
log4j.appender.ServerDailyRollingFile.Append=true

log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.org.mybatis=DEBUG  
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdout
com.ng.mapper=DEBUG

(4)mybatis-config.xml





    
        
    

(5)spring-mvc.xml




    
    

    
    

    
    
    
    

    
    
        
        
        
    

    
    
        
            
                text/html;charset=UTF-8
                application/json;charset=UTF-8
            
        
    

    
    
    
    
    
    
    
        
            
                
                
            
        
    
    
    
        
        
    

六、写测试类

 

package com.songci.mytest_one.test;

import com.songci.mytest_one.model.Student;
import com.songci.mytest_one.service.StudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by songl on 2017/8/9.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:config/applicationContext.xml")
public class ServiceTest {
    @Resource
    private StudentService studentService;
    @Test
    public void addTest() throws Exception {
        Student student=new Student();
//        student.setId(new Integer("1"));
        student.setName("王同学");
        student.setSex(false);
        student.setAddress("北京");
        System.out.println(studentService.addStudent(student));
    }
    @Test
    public void deleteTest()throws Exception{
        System.out.println(studentService.deleteStudentById(1));

    }
    @Test
    public void updateTest()throws Exception{
        Student student=new Student();
        student.setId(new Integer("4"));
        student.setAddress("台湾");
        System.out.println(studentService.updateStudentById(student));
    }
    @Test
    public void select()throws Exception{
//        Student student=new Student();
//        student.setId(new Integer("1"));
        List list=studentService.findAllStudent(null);
        for (Student s:list){
            System.out.println(s.toString());
        }

    }
}

相关代码在GitHub上,包括数据库sql文件

GitHub地址:https://github.com/iamsongci/mytest_one

将持续更新 ~~~未完待续~~~

下篇Maven-maven多模块项目搭建+SSM框架整合(四、Ajax异步获取数据,jquery动态添加)


作者:
链接:http://www.imooc.com/article/19827
来源:慕课网
本文原创发布于慕课网 ,转载请注明出处,谢谢合作

你可能感兴趣的:(Maven多模块项目搭建+SSM框架整合(三、配置文件添加,服务层测试))