https://github.com/Loudsa/student项目地址
项目结构目录
在根目录的pom文件引入以下几个包:
<properties> <spring-version>5.1.0.RELEASEspring-version> <jackson-version>2.9.7jackson-version> <jstl-version>1.2jstl-version> <mybatis-version>3.4.6mybatis-version> <mybatis-spring-version>1.3.2mybatis-spring-version> <c3p0-version>0.9.5.2c3p0-version> <mysql-version>8.0.12mysql-version> properties> <dependencies> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-beansartifactId> <version>${spring-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>${spring-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-coreartifactId> <version>${spring-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-txartifactId> <version>${spring-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>${spring-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>${spring-version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>${spring-version}version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>${jackson-version}version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-coreartifactId> <version>${jackson-version}version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-annotationsartifactId> <version>${jackson-version}version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>jstlartifactId> <version>${jstl-version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>${mybatis-version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>${mybatis-spring-version}version> dependency> <dependency> <groupId>com.mchangegroupId> <artifactId>c3p0artifactId> <version>${c3p0-version}version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>8.0.18version> dependency> dependencies>
在dao层中引入entity-module模块
如上图操作,在service层引入 entity和dao层
在action层引入entity层和dao层
将web层下的src目录删除,因为用不着
在web层的 pom目录中
然后新建resources资源目录,以及webapp网页资源目录
紧接着,在web目录的pom文件继续添加
<build> <resources> <resource> <directory>resourcesdirectory> resource> resources> <plugins> <plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-war-pluginartifactId> <version>3.2.2version> <configuration> <webResources> <resource> <directory>webappdirectory> 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"> <servlet> <servlet-name>springmvcservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:springmvc.xmlparam-value> init-param> servlet> <servlet-mapping> <servlet-name>springmvcservlet-name> <url-pattern>/url-pattern> servlet-mapping> <context-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:applicationContext.xmlparam-value> context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> listener> <filter> <filter-name>CharacterEncodingFilterfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>utf-8param-value> init-param> filter> <filter-mapping> <filter-name>CharacterEncodingFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping> <servlet-mapping> <servlet-name>defaultservlet-name> <url-pattern>*.htmlurl-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" /> <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder> <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> <property name="minPoolSize" value="3" /> <property name="maxPoolSize" value="15" /> <property name="initialPoolSize" value="3"/> bean> <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"> <property name="basePackage" value="com.nf.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property> bean> <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>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 ListgetAllStudents(); }
StudentMapper.xml
的service
public interface StudentService { public ListgetAllStudents(); }
@Service
public class StudentServiceImpl implements StudentService { @Autowired private StudentDao studentDao; public ListgetAllStudents() { List 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="width:100%;height:100%;">
<i-table stripe border :columns="mycols" :data="stuList">
i-table>
div>
body>
html>
emm,说实话我觉得这篇文章写得相对乱,唉,不管了,反正就是贴代码,反正只是怕自己忘记,你们的想法不重要
https://github.com/Loudsa/student 项目已经提交GitHub,想试试的朋友可以看下,