这是我第一次接触到ssm框架,之前一直想学习java的框架知识,这次有机会跟着学校的实训课程学习了这个框架。写的不好,但很适合初学者。
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.xkpgroupId>
<artifactId>ssmdemoartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<name>ssmdemo Maven Webappname>
<url>http://www.example.comurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.7maven.compiler.source>
<maven.compiler.target>1.7maven.compiler.target>
<springframework.version>4.3.18.RELEASEspringframework.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${springframework.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>5.1.2version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.13version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-coreartifactId>
<version>2.9.5version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.5version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-annotationsartifactId>
<version>2.9.5version>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-validatorartifactId>
<version>5.4.1.Finalversion>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
dependencies>
<build>
<finalName>ssmdemofinalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-pluginartifactId>
<version>3.1.0version>
plugin>
<plugin>
<artifactId>maven-resources-pluginartifactId>
<version>3.0.2version>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.0version>
plugin>
<plugin>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.1version>
plugin>
<plugin>
<artifactId>maven-war-pluginartifactId>
<version>3.2.2version>
plugin>
<plugin>
<artifactId>maven-install-pluginartifactId>
<version>2.5.2version>
plugin>
<plugin>
<artifactId>maven-deploy-pluginartifactId>
<version>2.8.2version>
plugin>
plugins>
pluginManagement>
build>
project>
创建数据库
建立entity,dao层(接口),service层(接口+实现类).
entity层:
Club.java:
package com.xkp.ssm.entity;
import java.util.Date;
public class Club {
private Integer id;
private String name;
private String loc;
private String stadium;
private Integer rank;
private Integer score;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public String getStadium() {
return stadium;
}
public void setStadium(String stadium) {
this.stadium = stadium;
}
public Integer getRank() {
return rank;
}
public void setRank(Integer rank) {
this.rank = rank;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
@Override
public String toString() {
return "Club{" +
"id=" + id +
", name='" + name + '\'' +
", loc='" + loc + '\'' +
", stadium='" + stadium + '\'' +
", rank=" + rank +
", score=" + score +
'}';
}
}
Dao层(纯接口)采用mapper代理:
ClubDao.java:
package com.xkp.ssm.dao;
import com.xkp.ssm.entity.Club;
import java.util.List;
public interface ClubDao {
//数据库基本操作
/**
* 添加球队记录
* @param club
* @throws Exception
*/
void addClub(Club club) throws Exception;
/**
* 修改一条球队
* @param club
* @throws Exception
*/
void updateClub(Club club) throws Exception;
/**
* 删除一条球队记录
* @param id
* @throws Exception
*/
void deleteClub(int id) throws Exception;
/**
* 根据主键查询用户信息
* @param id
* @return
* @throws Exception
*/
Club getClubById(int id) throws Exception;
/**
* 查询所有用户信息
* @return
*/
List<Club> getClubs();
}
Service层:
ClubService.java(接口):
package com.xkp.ssm.service;
import com.github.pagehelper.PageInfo;
import com.xkp.ssm.entity.Club;
public interface ClubService {
void addClub(Club club) throws Exception;
void updateClub(Club club) throws Exception;
void deleteUser(int id) throws Exception;
Club getClubById(int id) throws Exception;
/**
* 分页查询
*/
PageInfo<Club> getClubs(int page, int pageSize) throws Exception;
}
ClubServiceImpl.java(接口的实现类):
package com.xkp.ssm.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xkp.ssm.dao.ClubDao;
import com.xkp.ssm.entity.Club;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ClubServiceImpl implements ClubService {
@Autowired
private ClubDao clubDao;
@Override
public void addClub(Club club) throws Exception {
clubDao.addClub(club);
}
@Override
public void updateClub(Club club) throws Exception {
clubDao.updateClub(club);
}
@Override
public void deleteUser(int id) throws Exception {
clubDao.deleteClub(id);
}
@Override
public Club getClubById(int id) throws Exception {
return clubDao.getClubById(id);
}
@Override
public PageInfo<Club> getClubs(int page, int pageSize) throws Exception {
PageHelper.startPage(page,pageSize);
List<Club> clubList = clubDao.getClubs();
PageInfo<Club> pageInfo = new PageInfo<>(clubList);
return pageInfo;
}
}
log4j日志:
src/main/resources/log4j.properties
#指定logger
#设定log4j的日志级别和输出的目的地
#INFO日志级别 ,Console和logfile输出的目的地
#等级 OFF,FATAL,ERROR,WARN,INFO,DEBUG,TRACE
log4j.rootLogger=INFO,Console,logfile
#指定appender
#设定Logger的Console,其中Console为自定义名称,类型为控制台输出
log4j.appender.Console=org.apache.log4j.ConsoleAppender
#设定Logger的logfile,其中logfile为自定义名称,类型为文件
#org.apache.log4j.FileAppender文件
#org.apache.log4j.RollingFileAppender文件大小到达指定尺寸后产生一个新的文件
#org.apache.log4j.DailyRollingFileAppender每天产生一个日志文件
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
#设定文件的输出路径
log4j.appender.logfile.File=D:/log/ssm.log
#设定文件最大尺寸 单位可以使KB,MB,GB
log4j.appender.logfile.MaxFileSize=2048KB
#输出格式
#设定appender布局Layout
# %d 输出日志的日期和时间,指定格式:%d{yyyy-MM-dd HH:mm:ss SSS}
# %p 输出的日志级别
# %c 输出所属类的全类名
# %M 方法名
# %m 输出代码中指定消息
# %n 一个换行符
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %p %c.%M() --%m%n
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p %c.%M() --%m%n
数据库基本信息:
src/main/resources/db.properties
jdbc.mysql.url = jdbc:mysql://localhost:3306/java?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
jdbc.mysql.className = com.mysql.cj.jdbc.Driver
jdbc.mysql.username = root
jdbc.mysql.password = root
#druid德鲁伊
druid.initialSize=3
druid.minIdle=2
druid.maxActive=15
druid.maxWait=10000
druid.filters=stat,wall,log4j
druid.timeBetweenEvictionRunsMillis=60000
druid.minEvictableIdleTimeMillis=30000
druid.validationQuery=select 1 from dual
druid.testWhileIdle=true
druid.testOnBorrow=false
druid.testOnReturn=false
druid.poolPreparedStatements=true
druid.maxOpenPreparedStatements=20
Mybatis的映射文件:(增删改查基本操作)
src/main/resources/mybatis/mapper/ClubMapper.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.xkp.ssm.dao.ClubDao">
<insert id="addClub" parameterType="club">
insert into tb_club(tb_club.name,tb_club.loc,tb_club.stadium,tb_club.rank,tb_club.score) values(#{name},#{loc},#{stadium},#{rank},#{score})
</insert>
<delete id="deleteClub" parameterType="int">
delete from tb_club where id=#{id}
</delete>
<update id="updateClub" parameterType="club">
update tb_club set tb_club.name=#{name},tb_club.loc=#{loc},tb_club.stadium=#{stadium},tb_club.rank=#{rank},tb_club.score=#{score}
where tb_club.id=#{id}
</update>
<select id="getClubById" parameterType="int" resultType="club">
SELECT
tb_club.id,
tb_club.name,
tb_club.loc,
tb_club.stadium,
tb_club.rank,
tb_club.score
FROM
tb_club
where
tb_club.id= #{id}
</select>
<select id="getClubs" resultType="club">
SELECT
tb_club.id,
tb_club.name,
tb_club.loc,
tb_club.stadium,
tb_club.rank,
tb_club.score
FROM
tb_club
order by tb_club.rank asc
</select>
</mapper>
mybatis的全局配置:
src/main/resources/mybatis/mybatis-config.xml
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
<typeAliases>
<package name="com.xkp.ssm.entity"/>
typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="reasonable" value="true"/>
plugin>
plugins>
configuration>
Spring的配置:
src/main/resources/spring/applicationContext.xml
<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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.xkp.ssm">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.mysql.className}"/>
<property name="url" value="${jdbc.mysql.url}"/>
<property name="username" value="${jdbc.mysql.username}"/>
<property name="password" value="${jdbc.mysql.password}"/>
<property name="initialSize" value="${druid.initialSize}"/>
<property name="minIdle" value="${druid.minIdle}"/>
<property name="maxActive" value="${druid.maxActive}"/>
<property name="maxWait" value="${druid.maxWait}"/>
<property name="filters" value="${druid.filters}"/>
<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}"/>
<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}"/>
<property name="validationQuery" value="${druid.validationQuery}"/>
<property name="testWhileIdle" value="${druid.testWhileIdle}"/>
<property name="testOnBorrow" value="${druid.testOnBorrow}"/>
<property name="testOnReturn" value="${druid.testOnReturn}"/>
<property name="poolPreparedStatements" value="${druid.poolPreparedStatements}"/>
<property name="maxOpenPreparedStatements" value="${druid.maxOpenPreparedStatements}"/>
bean>
<bean id="sqlSessoinFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xkp.ssm.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessoinFactory"/>
bean>
beans>
src/main/resources/spring/applicationContext-tx.xml
<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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" isolation="READ_COMMITTED"/>
<tx:method name="add*" isolation="READ_COMMITTED"/>
<tx:method name="save*" isolation="READ_COMMITTED"/>
<tx:method name="update*" isolation="READ_COMMITTED"/>
<tx:method name="edit*" isolation="READ_COMMITTED"/>
<tx:method name="delete*" isolation="READ_COMMITTED"/>
<tx:method name="del*" isolation="READ_COMMITTED"/>
<tx:method name="remove*" isolation="READ_COMMITTED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="list*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="load*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="*"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="exp" expression="execution(* com.xkp.ssm.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="exp"/>
aop:config>
beans>
SpringMVC的配置
src/main/resources/springmvc/springmvc.xml
<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.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">
<context:component-scan base-package="com.xkp.ssm.controller"/>
<mvc:annotation-driven conversion-service="conversionService"/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
bean>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.xkp.ssm.converter.DateConverter">bean>
set>
property>
bean>
beans>
web的配置
src/main/webapp/WEB-INF/web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<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>
<filter>
<filter-name>HiddenHttpMethodFilterfilter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter>
<filter-name>HttpPutFormContentFilterfilter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilterfilter-class>
filter>
<filter-mapping>
<filter-name>HttpPutFormContentFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc/springmvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/applicationContext*.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>StatViewServletservlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
<init-param>
<param-name>loginUsernameparam-name>
<param-value>zhangsanparam-value>
init-param>
<init-param>
<param-name>loginPasswordparam-name>
<param-value>123456param-value>
init-param>
<init-param>
<param-name>allowparam-name>
<param-value>127.0.0.1param-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>StatViewServletservlet-name>
<url-pattern>/druid/*url-pattern>
servlet-mapping>
<filter>
<filter-name>WebStatFilterfilter-name>
<filter-class>com.alibaba.druid.support.http.WebStatFilterfilter-class>
<init-param>
<param-name>exclusionsparam-name>
<param-value>/druid/*,*.jsp,*.js,*.css,/bootstrap/*,/font-awesome/*,*.jpg,*.png,*.gifparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>WebStatFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/login.jspwelcome-file>
welcome-file-list>
web-app>
src/main/webapp/WEB-INF/jsp/login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
首页
足球俱乐部管理
src/main/webapp/WEB-INF/jsp/clublist.jsp
<%--
Created by IntelliJ IDEA.
User: bsx
Date: 2020/12/7
Time: 10:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
Title
src/main/webapp/WEB-INF/jsp/clubadd.jsp
<%--
Created by IntelliJ IDEA.
User: bsx
Date: 2020/12/7
Time: 10:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
新增俱乐部
新增俱乐部