Eclipse : Photon Release (4.8.0)
JDK : 1.7.0_80
Maven : 3.6
项目是在eclipse中使用maven构建,创建好所需的文件夹。
1.contorller:控制层,主要处理外部请求;
2.dao:数据层,直接进行数据库的读写操作;
3. entity:实体层,主要存放一些实体类;
4. mapper: mybatis中的映射文件;
5. service: 业务层,用来实现业务逻辑。
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>ssm</groupId>
<artifactId>ssm</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ssm Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!--Unit Test - 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!--Spring transaction-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!--Spring Web + Spring MVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>3.7.3</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>0.9.1</version>
</dependency>
<!--mysql jdbc-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--c3p0-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<!-- <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency> -->
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<!-- <dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency> -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!--NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config-->
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--file upload jar package-->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!--json-->
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<!--json serialize and deserialization-->
<!-- 引入fastjson依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>
<!-- 引入gson依赖 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
<!--Base64 加解密-->
<!-- https://mvnrepository.com/artifact/net.iharder/base64 -->
<dependency>
<groupId>net.iharder</groupId>
<artifactId>base64</artifactId>
<version>2.3.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!--log4j-->
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations-java5</artifactId>
<version>RELEASE</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
<build>
<finalName>ssm</finalName>
</build>
</project>
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: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-3.1.xsd">
<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven >
</mvc:annotation-driven>
<!-- 启动包扫描功能,以便注册带有@Controllers、@service、@repository、@Component等注解的类成为spring的bean -->
<context:component-scan base-package="com.ssm.controller" />
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/> <!-- 前缀 -->
<property name="suffix" value=".jsp"/> <!-- 后缀 -->
</bean>
<!-- 访问静态文件(jpg,js,css)的方法 -->
<!--<mvc:resources location="/files/" mapping="/files/**" />-->
<!--<mvc:resources location="/scripts/" mapping="/scripts/**" />-->
<!--<mvc:resources location="/styles/" mapping="/styles/**" />-->
<!--<mvc:resources location="/Views/" mapping="/Views/**" />-->
</beans>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<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>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>CharacterFilter</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>
<!-- Spring MVC配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--Spring-servlet.xml config-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法) -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--Spring MVC配置结束 -->
</web-app>
配置log4j是为了方便我们调试程序,并且在配置上也是比较简单的。
log4j.properties
log4j.rootLogger=INFO,CONSOLE,File
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.DatePattern = '.'yyyy-MM-dd
#log4j.appender.File.File=${catalina.base}/logs/ssm/rizhi.log
log4j.appender.File.File=E:/logs/ssm/rizhi.log
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[ssm] %d{HH\:mm\:ss} [%t] %c{1} - %m%n
log4j.appender.File.Threshold = INFO
log4j.appender.File.Append = TRUE
log4j.appender.File.Encoding = UTF-8
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[ssm] %d{HH\:mm\:ss} [%t] %c{1} - %m%n
log4j.appender.CONSOLE.Threshold = INFO
log4j.appender.CONSOLE.ImmediateFlush = TRUE
log4j.appender.CONSOLE.Target = System.out
log4j.appender.CONSOLE.Encoding = UTF-8
log4j.logger.jdbc.sqlonly=DEBUG
log4j.additivity.jdbc.sqltiming=false
首先,编写TestController.java文件,使用注解配置访问路径
package com.ssm.controller;
import java.util.Date;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ssm/test")
public class TestController {
@GetMapping()
public String TestString(){
Date d = new Date();
return d.toString();
}
}
之后就可以运行测试,因为是maven项目,所以首先我们应该打包编译项目,项目右键——run as——maven install
成功编译之后,部署到tomcat中运行。
最后,打开浏览器,输入 http://localhost:8080/ssm/test 访问得到当前时间。
至此,SpingMVC的整合已经完成。
接着整合spring与mybatis
jdbc.properties:配置中使用的是c3p0连接池,也可以更换成dbcp或者现在比较火的阿里开发的druid连接池,所需的包都在pom.xml中注释掉了,需要那个就开启那个。
#mysql
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/ssm?characterEncoding=utf8
jdbc.username=root
jdbc.password=root
c3p0.minPoolSize=10
c3p0.maxPoolSize=100
c3p0.acquireIncrement=3
c3p0.acquireRetryAttempts=60
c3p0.acquireRetryDelay=1000
c3p0.autoCommitOnClose=false
c3p0.checkoutTimeout=3000
c3p0.idleConnectionTestPeriod=120
c3p0.maxIdleTime=600
c3p0.testConnectionOnCheckin=false
c3p0.maxStatements=8
c3p0.maxStatementsPerConnection=5
c3p0.unreturnedConnectionTimeout=25
spring-mybatis.xml
引入jdbc配置文件,配置数据源,其他配置注释中有说明。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.propertiesvalue>
list>
property>
bean>
<bean id="dataSource" 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="${c3p0.minPoolSize}">property>
<property name="acquireIncrement" value="${c3p0.acquireIncrement}">property>
<property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}">property>
<property name="acquireRetryDelay" value="${c3p0.acquireRetryDelay}">property>
<property name="autoCommitOnClose" value="${c3p0.autoCommitOnClose}">property>
<property name="checkoutTimeout" value="${c3p0.checkoutTimeout}">property>
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}">property>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}">property>
<property name="testConnectionOnCheckin" value="${c3p0.testConnectionOnCheckin}">property>
<property name="maxStatements" value="${c3p0.maxStatements}">property>
<property name="maxStatementsPerConnection" value="${c3p0.maxStatementsPerConnection}">property>
<property name="unreturnedConnectionTimeout" value="${c3p0.unreturnedConnectionTimeout}">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.dao">property>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
beans>
spring配置文件applicationContext.xml ,引入mybatis的配置文件
<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"
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.xsd">
<context:annotation-config />
<context:component-scan base-package="com.ssm" />
<import resource="spring-mybatis.xml" />
beans>
完整web.xml配置,加入application.xml文件。
DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<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 Applicationdisplay-name>
<welcome-file-list>
<welcome-file>/index.jspwelcome-file>
welcome-file-list>
<filter>
<filter-name>CharacterFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<servlet>
<servlet-name>springservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springservlet-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>
web-app>
mysql创建测试表
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`Uid` int(36) DEFAULT NULL COMMENT 'Uid',
`Name` varchar(20) NOT NULL,
`Age` int(3) NOT NULL,
`ClassId` int(3) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
package com.ssm.entity;
public class Student {
private Integer uid;
private String name;
private Integer age;
private Integer classid;
/*get/set省略*/
}
dao层:StudentDao.java
package com.ssm.dao;
import org.springframework.stereotype.Repository;
import com.ssm.entity.Student;
import java.util.List;
@Repository
public interface StudentDao {
int deleteByPrimaryKey(int uid);
int insert(Student record);
int insertSelective(Student record);
Student selectByPrimaryKey(byte[] uid);
List<Student> selectByCondition(Student record);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
}
mapper层: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.ssm.dao.StudentDao">
<resultMap id="BaseResultMap" type="com.ssm.entity.Student">
<id column="Uid" jdbcType="INTEGER" property="uid" />
<result column="Name" jdbcType="VARCHAR" property="name" />
<result column="Age" jdbcType="INTEGER" property="age" />
<result column="ClassId" jdbcType="INTEGER" property="classid" />
</resultMap>
<sql id="Base_Column_List">
Uid, Name, Age, ClassId
</sql>
<select id="selectByPrimaryKey" parameterType="int" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from student
where Uid = #{uid,jdbcType=INTEGER}
</select>
<select id="selectByCondition" parameterType="com.ssm.entity.Student" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
from student
<where>
1=1
<if test="uid != null">
and Uid=#{uid,jdbcType=INTEGER}
</if>
<if test="name != null">
and Name=#{name,jdbcType=VARCHAR}
</if>
<if test="age != null">
and Age=#{age,jdbcType=INTEGER}
</if>
<if test="classid != null">
and ClassId=#{classid,jdbcType=INTEGER}
</if>
</where>
</select>
<delete id="deleteByPrimaryKey" parameterType="int">
delete from student
where Uid = #{uid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.ssm.entity.Student">
insert into student (Uid, Name, Age,
ClassId)
values (#{uid,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
#{classid,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.ssm.entity.Student">
insert into student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="uid != null">
Uid,
</if>
<if test="name != null">
Name,
</if>
<if test="age != null">
Age,
</if>
<if test="classid != null">
ClassId,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="uid != null">
#{uid,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
<if test="classid != null">
#{classid,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.ssm.entity.Student">
update student
<set>
<if test="name != null">
Name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
Age = #{age,jdbcType=INTEGER},
</if>
<if test="classid != null">
ClassId = #{classid,jdbcType=INTEGER},
</if>
</set>
where Uid = #{uid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.ssm.entity.Student">
update student
set Name = #{name,jdbcType=VARCHAR},
Age = #{age,jdbcType=INTEGER},
ClassId = #{classid,jdbcType=INTEGER}
where Uid = #{uid,jdbcType=INTEGER}
</update>
</mapper>
service层:
IStudentService.java
package com.ssm.service;
import java.util.List;
import com.ssm.entity.Student;
public interface IStudentService {
int deleteByPrimaryKey(int uid);
int insert(Student record);
int insertSelective(Student record);
Student selectByPrimaryKey(byte[] uid);
List<Student> selectByCondition(Student record);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
}
StudentService.java
package com.ssm.service.impl;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssm.dao.StudentDao;
import com.ssm.entity.Student;
import com.ssm.service.IStudentService;
import java.util.List;
@Service
public class StudentService implements IStudentService {
@Autowired
private StudentDao studentDao;
public static final Logger logger = LogManager.getLogger(StudentService.class);
public int deleteByPrimaryKey(int uid) {
// TODO Auto-generated method stub
return studentDao.deleteByPrimaryKey(uid);
}
public int insert(Student record) {
// TODO Auto-generated method stub
return studentDao.insert(record);
}
public int insertSelective(Student record) {
// TODO Auto-generated method stub
return studentDao.insertSelective(record);
}
public Student selectByPrimaryKey(byte[] uid) {
// TODO Auto-generated method stub
return studentDao.selectByPrimaryKey(uid);
}
public List<Student> selectByCondition(Student record) {
List<Student> studentL = studentDao.selectByCondition(record);
for(Student student : studentL) {
System.out.println(student.toString());
}
logger.info("run as StudentService.. selectByCondition"+studentL.size());
return studentDao.selectByCondition(record);
}
public int updateByPrimaryKeySelective(Student record) {
// TODO Auto-generated method stub
return studentDao.updateByPrimaryKeySelective(record);
}
public int updateByPrimaryKey(Student record) {
// TODO Auto-generated method stub
return studentDao.updateByPrimaryKey(record);
}
}
contorller层:
package com.ssm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ssm.entity.Student;
import com.ssm.service.IStudentService;
@RestController
@RequestMapping(value="/ssm/Student")
public class StudentController {
@Autowired
private IStudentService iStudentService;
@GetMapping(produces="text/html; charset=UTF-8")
public String Get() {
List<Student> student = iStudentService.selectByCondition(new Student());
return student.toString();
}
}
完成之后,maven打包编译运行,访问地址:http://localhost:8080/ssm/Student
需要多配置文件多了解一下,并且要自己多动手尝试配置不同的功能,在运行起来之后,也可以按照自己的理解修改修改,别怕异常,解决异常会更快的掌握。