maven工程搭建SSM框架及多种配置文件的约束

# 内容
    1. maven工程搭建SSM框架步骤
        1. 环境搭建,完善项目结构
        2. 编写sql语句,建立数据库结构
        3. 新建maven项目,编写pom.xml文件
        4. 编写实体类
        5. 编写dao层代码,并编写对应的mapper.xml文件
        6. 在src/main/resources文件夹下创建applicationContext.xml文件,并配置dao层
        7. dao层单元测试:在src/test/java,新建TestFindById类进行测试
        8. 编写service层代码,并在application中配置service
        9. service层单元测试:在src/test/java中的TestFindById类进行测试
        10. 编写web层代码:编写controller内容以及jsp页面
        11. 在src/main/resources文件夹下创建springmvc.xml文件,并作相关配置
        12. 编写web.xml文件


    2. 各种约束文件以及配置文件
        * Mybatis mapper.xml约束
        * Mybatis config.xml约束
        * applicationContext.xml约束
        * springmvc.xml约束
        * log4j.properties配置文件
        * druid.properties配置文件
        * c3p0-config.xml配置文件

 


## maven工程搭建SSM框架步骤
1. 环境搭建,完善项目结构
    * src/main/java,目录下新建domain、dao、service、service.impl、controller包
    * src/main/resources,并导入log4j.properties文件
    * src/test/java
    * src/main/webapp/pages
    * src/main/webapp/WEB-INF/web.xml

2. 编写sql语句,建立数据库结构
sql语句代码如下:

DROP TABLE IF EXISTS Student;
CREATE TABLE Student(
    id int(10) NOT NULL auto_increment,
    name VARCHAR(20),
    age int(3),
    PRIMARY KEY (id)
);
INSERT INTO student VALUES('1','老刘','20');

3. 新建maven项目,编写pom.xml文件
步骤如下:
    1. 统一管理jar包版本,用标签
    2. 锁定jar版本,用标签
    3. 导入jar包
    4. 添加tomcat插件


pom.xml代码如下:



    4.0.0

    com.lyp
    maven_ssm
    1.0-SNAPSHOT
    war

    
    
        5.0.2.RELEASE
        1.6.6
        1.2.12
        1.2.3
        8.0.16
        3.4.5
        5.0.1.RELEASE
    

    
    
        
            
                org.springframework
                spring-context
                ${spring.version}
            
            
                org.springframework
                spring-web
                ${spring.version}
            
            
                org.springframework
                spring-webmvc
                ${spring.version}
            
            
                org.springframework
                spring-tx
                ${spring.version}
            
            
                org.springframework
                spring-test
                ${spring.version}
            
            
                org.mybatis
                mybatis
                ${mybatis.version}
            
        
    

    
    
        
        
            org.aspectj
            aspectjweaver
            1.6.8
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
            org.springframework
            spring-orm
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-test
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            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
        
        
            c3p0
            c3p0
            0.9.1.2
            jar
            compile
        
        
            com.github.pagehelper
            pagehelper
            5.1.2
        
        
            org.springframework.security
            spring-security-web
            ${spring.security.version}
        
        
            org.springframework.security
            spring-security-config
            ${spring.security.version}
        
        
            org.springframework.security
            spring-security-core
            ${spring.security.version}
        
        
            org.springframework.security
            spring-security-taglibs
            ${spring.security.version}
        
        
            com.alibaba
            druid
            1.0.9
        
        
            junit
            junit
            4.12
        
    
    
    
        
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
            
        
    

4. 编写实体类
实体类代码如下:

public class Student {
    private int id;
    private String name;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

5. 编写dao层代码,并编写对应的mapper.xml文件
dao层代码:

public interface StudentDao {
    Student findById(int id);
}

mapper.xml文件代码:





    

6. 在src/main/resources文件夹下创建applicationContext.xml文件,并配置dao层
步骤如下:
    1. 导入applicationContext.xml文件的约束
    2. 开始配置dao层,包括配置数据库连接池、mapper配置
    3. 配置数据库连接池
    4. 配置mapper,包括sqlsessionfactory、mapper扫描器


applicationContext.xml文件配置dao层代码如下:




    
    
    
        
        
        
        
    

    
    
    
        
        
    

    
    
        
    
    

7. dao层单元测试:在src/test/java,新建TestFindById类进行测试
dao层测试代码如下:

public class TestFindById {
    @org.junit.Test
    public void testFindById(){
        //获取spring容器
        ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

        //dao层测试
        //获取mapper
        StudentDao studentDao=ac.getBean(StudentDao.class);
        //调用mapper方法
        Student student=studentDao.findById(1);
        System.out.println(student);

    }
}


8. 编写service层代码,并在application中配置service
配置service步骤如下:
    * 组件扫描配置
    * 配置事务管理器
    * 配置切入点
    * 配置事务的通知
    * 配置切面:切入点与通知的组合

service接口代码如下:

public interface StudentService {
    Student findById(int id);
}

serviceImpl实现类代码如下:

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    public Student findById(int id) {
        System.out.println(studentDao);
        return studentDao.findById(id);
    }
}

applicationContext.xml添加配置service层的代码如下:

    
    
    

    
    
    
        
    

    
    
        
            
            
            
            
            
        
    

    
    
        
        
    
    

9. service层单元测试:在src/test/java中的TestFindById类进行测试

public class TestFindById {
    @org.junit.Test
    public void testFindById(){
        //获取spring容器
        ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

        //service层测试
        StudentService studentService=ac.getBean(StudentService.class);
        //调用方法
        Student s=studentService.findById(1);
        System.out.println(s);
    }
}

10. 编写web层代码:编写controller内容以及jsp页面
controller代码如下:

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/findDetail")
    public String findDetail(Model model){
        Student student=studentService.findById(1);
        model.addAttribute("student",student);
        return "studentDetail";
    }
}

jsp页面代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>




    
    Insert title here


                                                                                                                                             
学生编号 ${student.id}
学生姓名 ${student.name}
学生年龄${student.age}

11. 在src/main/resources文件夹下创建springmvc.xml文件,并作相关配置
步骤如下:
    * 导入springmvc.xml的约束文件
    * 配置controller包的组件扫描
    * 配置处理器映射器、处理器设配器
    * 配置视图解析器
    * 配置释放静态资源

springmvc.xml代码如下:



    
    

    
    

    
    
        
        
    

    
    

12.编写web.xml文件
步骤如下:
    * 配置编码过滤器
    * 配置spring核心监听器
    * 重新指定spring配置文件的位置
    * 配置spring的核心servlet


web.xml代码如下:



    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        encodingFilter
        /*
    

    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        contextConfigLocation
        classpath:applicationContext.xml
    

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

13. 项目目录截图

maven工程搭建SSM框架及多种配置文件的约束_第1张图片

 


## 各种约束文件及配置文件
1. Mybatis mapper.xml导入的约束:


2. Mybatis config导入的约束:


3. applicationContext.xml导入的约束:



4. springmvc.xml导入的约束:




5. log4j.properties文件代码:

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE            debug   info   warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=C:/DevelopApplication/axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n

 6. druid.properties文件代码:

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/数据库名称?serverTimezone=UTC&characterEncoding=utf-8
username=root
password=密码
initialSize=5
maxActive=10
maxWait=3000

7. c3p0-config.xml文件代码:


  
  
  	
    com.mysql.jdbc.Driver
    jdbc:mysql://localhost:3306/数据库名称
    root
    密码
    
    
    5
    10
    3000
  

   
    
    com.mysql.jdbc.Driver
    jdbc:mysql://localhost:3306/day25
    root
    root
    
    
    5
    8
    1000
  

 

你可能感兴趣的:(maven,SpringMVC)