SSM多模块----Vue+Iview

https://github.com/Loudsa/student项目地址

项目结构目录

 在根目录的pom文件引入以下几个包:

 <properties>
                <spring-version>5.1.0.RELEASE</spring-version>
                <jackson-version>2.9.7</jackson-version>
                <jstl-version>1.2</jstl-version>
                <mybatis-version>3.4.6</mybatis-version>
                <mybatis-spring-version>1.3.2</mybatis-spring-version>
                <c3p0-version>0.9.5.2</c3p0-version>
                <mysql-version>8.0.12</mysql-version>
            </properties>

            <dependencies>
                <!--1.Spring包-->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                    <version>${spring-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                    <version>${spring-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                    <version>${spring-version}</version>
                </dependency>

                <!--1.Spring项目跟mybatic中融合需要的2个东西-->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-tx</artifactId>
                    <version>${spring-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jdbc</artifactId>
                    <version>${spring-version}</version>
                </dependency>

                <!--2.SpringMVC的包-->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                    <version>${spring-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-webmvc</artifactId>
                    <version>${spring-version}</version>
                </dependency>

                <!--2.SpringMVC中,ResponseBody时要用到的默认的JSON解析引擎-->
                <dependency>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-databind</artifactId>
                    <version>${jackson-version}</version>
                </dependency>
                <dependency>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-core</artifactId>
                    <version>${jackson-version}</version>
                </dependency>
                <dependency>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-annotations</artifactId>
                    <version>${jackson-version}</version>
                </dependency>


                <!--4.JSP的JSTL依赖,如果不用,可以不写-->
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>jstl</artifactId>
                    <version>${jstl-version}</version>
                </dependency>

                <!--5.MyBatis的依赖包-->
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                    <version>${mybatis-version}</version>
                </dependency>

                <!--
                5.
                1.MyBatis的SqlSession提供指定的方法来处理编程式的事务(手动写commit或rollback等代码)
                2.但当使用mybatis-spring这个组件时,bean会使用Spring管理的SqlSession。
                Spring通常都是处理事务(不用程序员来写commit)

                -->
                <dependency>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                    <version>${mybatis-spring-version}</version>
                </dependency>

                <dependency>
                    <groupId>com.mchange</groupId>
                    <artifactId>c3p0</artifactId>
                    <version>${c3p0-version}</version>
                </dependency>
                <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.18</version>
                </dependency>
            </dependencies>

在dao层中引入entity-module模块

如上图操作,在service层引入 entitydao

在action层引入entity层和dao

将web层下的src目录删除,因为用不着

 在web层的 pom目录中

 然后新建resources资源目录,以及webapp网页资源目录

紧接着,在web目录的pom文件继续添加

<build>
         <resources>
             <resource>
                 <directory>resources</directory>
             </resource>
         </resources>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-war-plugin</artifactId>
                 <version>3.2.2</version>
                 <configuration>
                     <webResources>
                         <resource>
                             <directory>webapp</directory>
                         </resource>
                     </webResources>
                 </configuration>
             </plugin>
         </plugins>
     </build>

在webapp目录下新建WEB-INF在WEB-INF目录下新建web.xml,插入如下代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

    <!--这是springmvc的配置-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--这是spring的配置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- Spring监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 字符编码过滤器 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 允许返回html视图 -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>

resources新建applicationContext.xml、jdbc.properties、springmvc.xml

分别插入如下代码

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 自动扫描 -->
    <context:component-scan base-package="com.nf" />
    <!-- 引入配置文件,spring2.5以后的写法,之前的是bean class=org.springframework.beans.factory.config.PropertyPlaceholderConfigurer -->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!--配置C3P0 dataSource-->
    <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <!-- 连接池中保留的最小连接数,默认为:3 -->
        <property name="minPoolSize" value="3" />
        <!-- 连接池中保留的最大连接数。默认值: 15 -->
        <property name="maxPoolSize" value="15" />
        <!-- 初始化连接池中的连接数,取值应在minPoolSize与maxPoolSize之间,默认为3 -->
        <property name="initialPoolSize" value="3"/>
    </bean>

    <!--
    此处,SqlSessionFactory以及MapperScannerConfigurer就是书本12.4章的内容
    作用:实现书本第3章代码,一行都不用写,直接生成dao的实现类,不用任何编码
    也不需要写一行mybatis的代码。
    ps.这就是依赖于spring神的帮助
    -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"></property>
        <property name="mapperLocations" value="classpath*:mapper/*.xml"></property>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--扫描dao层的接口interface-->
        <property name="basePackage" value="com.nf.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!--mapper xml文件就像实现类,它是怎么知道对应哪个dao层的接口的?-->

    <!--声明式事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="myDataSource"></property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"></tx:annotation-driven>

</beans>

jdbc.properties

jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/myschool?serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=123456

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <context:component-scan base-package="com.nf">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>

    <!--mvc:annotation-driven 是告诉springmvc需要解析:@ResponseBody-->
    <!--如果使用jackson,可以不写里面的bean,因为是默认-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".html"></property>
    </bean>
    <mvc:annotation-driven/>
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/"></mvc:resources>
    <mvc:resources mapping="/js/**" location="/WEB-INF/js/"></mvc:resources>
    <mvc:resources mapping="/img/**" location="/WEB-INF/img/"></mvc:resources>

</beans>

/**Student实体类**/
package com.nf.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;

import java.sql.Date;

public class Student {
     
    private Integer id;
    private String userName;
    private String sex;
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date birt;
    private Integer age;
    
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
// 这个注解是为了将json格式的日期转为正常格式
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    public Date getBirt() {
        return birt;
    }
    public void setBirt(Date birt) {
        this.birt = birt;
    }
    
    
    
}

StudentDao接口

public interface StudentDao {
    
    public List<Student> getAllStudents();

}

 StudentMapper.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nf.dao.StudentDao">


    <resultMap id="stuAll" type="com.nf.entity.Student">
        <id property="userName" column="user_name"/>
        <result property="sex" column="sex"/>
        <result property="birt" column="birt"/>
        <result property="age" column="age"/>
    </resultMap>
    <select id="getAllStudents" resultMap="stuAll">
        select * from student
    </select>
</mapper>

的service

public interface StudentService {
    public List<Student> getAllStudents();
}
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;

    public List<Student> getAllStudents() {

        List<Student> stuList = studentDao.getAllStudents();
        
        
        Calendar calendar = Calendar.getInstance();//储存的时间,就是现在
        int year_now = calendar.get( Calendar.YEAR );
        int month_now =calendar.get( Calendar.MONTH )+1;
        int day_now = calendar.get(Calendar.DAY_OF_MONTH);
        
        System.out.println(year_now);
        System.out.println(month_now);
        System.out.println(day_now);
        for (Student stu:stuList){
            calendar.setTime(stu.getBirt());
            int year_pass = calendar.get( Calendar.YEAR );
            int month_pass =calendar.get( Calendar.MONTH )+1;
            int day_pass = calendar.get(Calendar.DAY_OF_MONTH);
            int age = year_now - year_pass;
            //还没生日
            if (month_now<month_pass) {
                age--;
            }else if((month_now==month_pass)&&(day_now<day_pass)){
                //不满月
                age--;
            }
            stu.setAge(age);
        }
        return stuList;
    }

}
StudentAction
@Controller
public class StudentAction {

    @Autowired
    private StudentService studentService;
    @ResponseBody
    @RequestMapping("stuAll")
    private List getAllStudent() {
        return studentService.getAllStudents();
    }
    @RequestMapping("/index")
    private String index(){
        return "index";
    }
}

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title></title>
</head>
<link rel="stylesheet" type="text/css" href="../css/iview.css"/>
<style>
    body {
        padding: 10px;
    }
</style>
<script src="../js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/vue.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/iview.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">

    $(function () {
        var myModel = {
            mycount: 100, mycols: [{title: '编号', key: 'id'}, {title: '姓名', key: 'userName'},
                {title: '性别', key: 'sex'}, {title: '出生日期', key: 'birt'}, {title: '年龄', key: 'age'}], stuList: []
        };
        var myViewModel = new Vue({
            data: myModel,
            el: "#myView"
        });

        $.ajax({
            type: "get",
            url: "stuAll",
            dataType: "json",
            success: function (res) {
                myModel.stuList = res;
            },
            error: function () {

            }
        });
    })
</script>
<body>
<div id="myView" style="100%;height:100%;">
    <i-table stripe border :columns="mycols" :data="stuList">

    </i-table>
</div>
</body>
</html>

emm,说实话我觉得这篇文章写得相对乱,唉,不管了,反正就是贴代码,反正只是怕自己忘记,你们的想法不重要

https://github.com/Loudsa/student 项目已经提交GitHub,想试试的朋友可以看下,

原文地址:https://www.cnblogs.com/inthecloud/p/11692015.html