前言
SSM(Spring、SpringMVC和Mybatis)+Mysql/Oracle 是作为后台的搭建的主要框架,现在主要的常见的开发集成工具有eclipse和IDEA,为了避免配置繁琐的jar包,我们使用eclipse创建Maven工程 。
1.1、Spring
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
1.2、SpringMVC
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
1.3、MyBatis
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
实现流程
1.Mysql数据表结构
2.创建Maven工程
项目创建好之后,默认的目录是这样的,至此项目就创建成功了.
3.项目配置
0.配置项目web.xml
新建项目的web.xml文件可能是2.3版本的,如下:
但我们通常需要的是3.0版本的
我们需要修改.setting的配置文件
将里面的1.5配置成1.6或者更高(我配置的是1.8)
配置完成后,项目->右键->properties->Project facets,看到以图表示配置成功
1.maven项目是应该有
src/main/resources
src/main/java
src/test/java
这几个Source Folder的,我们新建的项目却只有src/main/resources一个文件夹,当我们右键->new-> source folder时,输入src/main/java时却提示已存在.但是目录中并没有显示这个文件夹,我们右键->build path->configure build path->sources,会发现这两个文件夹其实已经建好了,但显示的红叉,后面标注着missing.
此时我们再切回libraries,选中JRE System library,点击edit
选择workspace default JRE,点击finish,再点击OK
此时我们会神奇的发现,那两个文件夹又显示出来了(不知道为什莫)
在java文件夹新建如下文件夹
4.Maven需要引入的jar包
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 组别名称 -->
<groupId>com.wmj</groupId>
<!-- 项目名称 -->
<artifactId>angularDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<!-- Maven 项目标识 -->
<name>angularDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- spring版本号 -->
<spring.version>4.0.2.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<scope>provided</scope>
<version>7.0</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 日志文件管理包 -->
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上传组件包 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>9090</port>
<path>/angularDemo</path>
<uriEncoding>UTF-8</uriEncoding>
<finalName>angularDemo</finalName>
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<!-- 配置结束标识 -->
<finalName>angularDemo</finalName>
</build>
</project>
5.Spring与MyBatis的整合
所有需要的JAR包都引入以后,首先进行Spring与MyBatis的整合
5.1建立JDBC属性文件
jdbc.properties(文件编码修改为utf-8)
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo
username=root
password=wmj
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000
5.1 建立spring-mybatis.xml配置文件
这个文件就是用来完成spring和mybatis的整合的。这里面也没多少行配置,主要的就是自动扫描,自动注入,配置数据库。
代码
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.wmj.SSM" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/wmj/SSM/**/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wmj.SSM.Dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
5.3、Log4j的配置
log4j.properties 为了方便调试,一般都会使用日志来输出信息,Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。
log4j.rootLogger=INFO,Console,File
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
log4j.appender.File = org.apache.log4j.RollingFileAppender
log4j.appender.File.File = logs/ssm.log
log4j.appender.File.MaxFileSize = 10MB
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
6、整合SpringMVC
上面已经完成了2大框架的整合,SpringMVC的配置文件单独放,然后在web.xml中配置整合。
6.1配置spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.wmj.SSM" />
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=""></property>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
</beans>
6.2在web.xml中进行整合
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- Spring和mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- Spring MVC servlet -->
<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:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
<!-- 配置SESSION超时,单位是分钟 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
<error-page>
<error-code>404</error-code>
<location>/</location>
</error-page>
</web-app>
7.具体代码过程
7.1 在Pojo中建立实体类 studentInfo.java
public class studentInfo {
private Integer stuNum;
private String stuName;
private Integer stuAge;
public Integer getStuAge() {
return stuAge;
}
public void setStuAge(Integer stuAge) {
this.stuAge = stuAge;
}
private String stuMajor;
public Integer getStuNum() {
return stuNum;
}
public void setStuNum(Integer stuNum) {
this.stuNum = stuNum;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuMajor() {
return stuMajor;
}
public void setStuMajor(String stuMajor) {
this.stuMajor = stuMajor;
}
}
7.2在持久层中Dao文件夹建立studentInfoDao.java
import java.util.List;
import com.wmj.SSM.Pojo.studentInfo;
public interface studentInfoDao {
//获取所有学生信息
List<studentInfo> getAllStudentInfo();
//根据学号获取学生信息并返回list
List<studentInfo> getStudentInfoByNumReturnList(int stuNum);
//根据学号获取学生信息
studentInfo getStudentInfoByNum(Integer stuNum);
//根据姓名获取学生信息
studentInfo getStudentInfoByName(String stuName);
//根据学号获取学生信息
studentInfo getStudentInfoByMajor(String stuMajor);
//修改学生信息
int updateStudentInfo(studentInfo stuInfo);
//添加学生信息
int addStudentInfo(studentInfo stuInfo);
//根据学号删除学生信息
int deleteStudentInfo(Integer num);
}
7.3在Mapper文件下新建stuInfoMapper.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.wmj.SSM.Dao.studentInfoDao" >
<resultMap id="stuResultMap" type="com.wmj.SSM.Pojo.studentInfo" >
<id column="stuNum" property="stuNum" jdbcType="INTEGER" />
<result column="stuName" property="stuName" jdbcType="VARCHAR" />
<result column="stuAge" property="stuAge" jdbcType="INTEGER" />
<result column="stuMajor" property="stuMajor" jdbcType="VARCHAR" />
</resultMap>
<select id="getAllStudentInfo" parameterType="java.lang.Integer" resultMap="stuResultMap">
select stuNum,stuName,stuAge,stuMajor from stu_info
</select>
<select id="getStudentInfoByNumReturnList" parameterType="java.lang.Integer" resultMap="stuResultMap">
select stuNum,stuName,stuAge,stuMajor from stu_info where stuNum = #{stuNum}
</select>
<select id="getStudentInfoByNum" parameterType="java.lang.Integer" resultMap="stuResultMap">
select stuNum,stuName,stuAge,stuMajor from stu_info where stuNum = #{stuNum}
</select>
<select id="getStudentInfoByName" parameterType="string" resultMap="stuResultMap">
select stuNum,stuName,stuAge,stuMajor from stu_info where stuName = #{stuName}
</select>
<select id="getStudentInfoByMajor" parameterType="string" resultMap="stuResultMap">
select stuNum,stuName,stuAge,stuMajor from stu_info where stuMajor = #{stuMajor}
</select>
<update id="updateStudentInfo" parameterType="com.wmj.SSM.Pojo.studentInfo">
update stu_info
<set >
<if test="stuNum != null" >
stuNum = #{stuNum},
</if>
<if test="stuName != null" >
stuName = #{stuName},
</if>
<if test="stuAge != null" >
stuAge = #{stuAge},
</if>
<if test="stuMajor != null" >
stuMajor = #{stuMajor},
</if>
</set>
where stuNum = #{stuNum}
</update>
<insert id="addStudentInfo" parameterType="com.wmj.SSM.Pojo.studentInfo" >
insert into stu_info values (#{stuNum}, #{stuName}, #{stuAge}, #{stuMajor})
</insert>
<delete id="deleteStudentInfo" parameterType="java.lang.Integer" >
delete from stu_info
where stuNum = #{stuNum}
</delete>
</mapper>
7.4在service中新建studentInfoService.java 声明服务层接口
import java.util.List;
import com.wmj.SSM.Pojo.studentInfo;;
public interface studentInfoService {
//获取所有学生信息
public List<studentInfo> getAllStudentInfo();
//根据学号获取学生信息,返回List
public List<studentInfo> getStudentInfoByNumReturnList(int num);
//根据学号获取学生信息
public studentInfo getStudentInfoByNum(Integer num);
//根据姓名获取学生信息
public studentInfo getStudentInfoByName(String name);
//根据专业获取学生信息
public studentInfo getStudentInfoByMajor(String major);
//修改学生信息
int updateStudentInfo(studentInfo stuInfo);
//添加学生信息
int addStudentInfo(studentInfo stuInfo);
//根据学号删除学生信息
int deleteStudentInfo(Integer num);
}
7.5在实现层新建impl文件夹,作为service接口的实现层,新建studentInfoImpl.java
package com.wmj.SSM.Service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.wmj.SSM.Dao.studentInfoDao;
import com.wmj.SSM.Pojo.studentInfo;
import com.wmj.SSM.Service.studentInfoService;
@Service("studentInfoService")
public class studentInfoServiceImpl implements studentInfoService {
@Resource
private studentInfoDao stuInfoDao;
@Override
public List<studentInfo> getAllStudentInfo() {
List<studentInfo> stuInfo =this.stuInfoDao.getAllStudentInfo();
return stuInfo;
}
@Override
public List<studentInfo> getStudentInfoByNumReturnList(int num) {
List<studentInfo> stuInfo =this.stuInfoDao.getStudentInfoByNumReturnList(num);
return stuInfo;
}
@Override
public studentInfo getStudentInfoByNum(Integer stuNum) {
studentInfo stuInfo =this.stuInfoDao.getStudentInfoByNum(stuNum);
return stuInfo;
}
@Override
public studentInfo getStudentInfoByName(String stuName) {
// TODO Auto-generated method stub
return null;
}
@Override
public studentInfo getStudentInfoByMajor(String stuMajor) {
// TODO Auto-generated method stub
return null;
}
@Override
public int updateStudentInfo(studentInfo stuInfo) {
return this.stuInfoDao.updateStudentInfo(stuInfo);
}
@Override
public int addStudentInfo(studentInfo stuInfo) {
return this.stuInfoDao.addStudentInfo(stuInfo);
}
@Override
public int deleteStudentInfo(Integer num) {
return this.stuInfoDao.deleteStudentInfo(num);
}
}
7.6在controller层新建stuInfoController.java文件作为与前台交互
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wmj.framework.util.JSONUtil;
import com.wmj.framework.vo.JSONVO;
import com.wmj.SSM.Pojo.studentInfo;
import com.wmj.SSM.Service.studentInfoService;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
@RestController
@RequestMapping("/stu")
public class stuInfoController {
@Resource
private studentInfoService stuInfoService;
@RequestMapping(value="/selectStu")
public List<studentInfo> selectStu(HttpServletRequest request, HttpServletResponse response, String json){
JSONObject jsonobject = JSONObject.fromObject(json);
studentInfo rule = (studentInfo) JSONObject.toBean(jsonobject, studentInfo.class);
List<studentInfo> RestuInfo = new ArrayList<studentInfo>();
if(rule.getStuNum()!=null){
RestuInfo=stuInfoService.getStudentInfoByNumReturnList(rule.getStuNum());
}else{
RestuInfo=stuInfoService.getAllStudentInfo();
}
return RestuInfo;
}
//根据学号查询信息
@RequestMapping(value="/getDataByNum")
public void getDataByNum(HttpServletRequest request, HttpServletResponse response, Integer num) {
studentInfo entity = stuInfoService.getStudentInfoByNum(num);
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
try {
response.getWriter().print(JSONObject.fromObject(entity));
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(value="/addStudentInfo")
public void addStudentInfo(HttpServletRequest request, HttpServletResponse response, String json){
JSONObject jsonobject = JSONObject.fromObject(json);
studentInfo rule = (studentInfo) JSONObject.toBean(jsonobject, studentInfo.class);
int result = stuInfoService.addStudentInfo(rule);
//这些都是灵活的返回提示,
JSONVO jsonVO = null;
if (result > 0) {
jsonVO = new JSONVO(result, "添加成功");
} else {
jsonVO = new JSONVO(result, "添加失败");
}
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
try {
response.getWriter().print(jsonVO);
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(value="deleteDataByIds")
public void deleteDataById(HttpServletRequest request, HttpServletResponse response, Integer num) {
int result = stuInfoService.deleteStudentInfo(num);
JSONVO jsonVO = null;
if (result > 0) {
jsonVO = new JSONVO(result, "删除成功");
} else {
jsonVO = new JSONVO(result, "删除失败");
}
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
try {
response.getWriter().print(jsonVO);
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(value="updateStudentInfo")
public void updateStudentInfo(HttpServletRequest request, HttpServletResponse response, String json) {
JSONObject jsonobject = JSONObject.fromObject(json);
studentInfo rule = (studentInfo) JSONObject.toBean(jsonobject, studentInfo.class);
int result = stuInfoService.updateStudentInfo(rule);
//这些都是灵活的返回提示,
JSONVO jsonVO = null;
if (result > 0) {
jsonVO = new JSONVO(result, "更新成功");
} else {
jsonVO = new JSONVO(result, "更新失败");
}
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
try {
response.getWriter().print(jsonVO);
} catch (IOException e) {
e.printStackTrace();
}
}
}
至此,ssm+maven的后台就完成了。
附带一个,自己用ExtJs +SSM+maven 在eclipse中完整的小例子:
实现效果