IDEA创建Maven中的web模板应用。项目目录结构如下:
ssm-project
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─yoyling
│ │ │ ├─controller
│ │ │ │ └─ScoreController.java
│ │ │ ├─domain
│ │ │ │ └─Score.java
│ │ │ ├─mapper
│ │ │ │ └─ScoreMapper.java
│ │ │ └─service
│ │ │ ├─impl
│ │ │ │ └─ScoreServiceImpl.java
│ │ │ └─ScoreService.java
│ │ ├─resources
│ │ │ ├─com
│ │ │ │ └─yoyling
│ │ │ │ └─mapper
│ │ │ │ └─ScoreMapper.xml
│ │ │ ├─dbconfig.properties
│ │ │ ├─log4j.properties
│ │ │ ├─mybatis-generator.xml
│ │ │ ├─spring-context.xml
│ │ │ └─spring-mvc.xml
│ │ └─webapp
│ │ ├─WEB-INF
│ │ │ └─pages
│ │ │ └─demo.jsp
│ │ └─index.jsp
│ └─test
│ └─com
│ └─yoyling
│ └─ServiceTest.java
└─pom.xml
ssm_project.sql
CREATE TABLE `score` (
`score_id` int(5) NOT NULL AUTO_INCREMENT,
`xh` varchar(11) NULL DEFAULT NULL,
`kch` varchar(10) NULL DEFAULT NULL,
`cj` int(3) NULL DEFAULT NULL,
`jd` float NULL DEFAULT NULL,
`xf` float NULL DEFAULT NULL,
PRIMARY KEY (`score_id`)
);
INSERT INTO `score` VALUES (1, '1812123206', 'R3210104', 92, 4.2, 3);
pom.xml依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
<mybatis.version>3.4.5</mybatis.version>
<mybatis-spring.version>1.3.0</mybatis-spring.version>
<mysql.version>5.1.6</mysql.version>
<c3p0.version>0.9.1.2</c3p0.version>
<aspectj.version>1.6.8</aspectj.version>
<junit.version>4.12</junit.version>
<servlet-api.version>3.1.0</servlet-api.version>
<jsp-api.version>2.2</jsp-api.version>
<jstl.version>1.2</jstl.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
</properties>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.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</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-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</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>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>compile</scope>
</dependency>
<!-- jdbc数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 支持servlet和jsp的jar包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- JSP标准标签库 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- log -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</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>
<!-- MyBatis3.4.5 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
web.xml配置servlet、filter等
<!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 version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<!-- 配置前端控制器:服务器启动必须加载,需要加载spring-mvc.xml配置文件 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数,创建完DispatcherServlet对象,加载spring-mvc.xml配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置解决中文乱码的过滤器 -->
<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>
<!-- 配置Spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置加载类路径的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
</web-app>
dbconfig.properties数据相关
jdbc.url=jdbc:mysql://localhost:3306/ssm_project
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
# 通用Mapper固定配置
mapper.plugin=tk.mybatis.mapper.generator.MapperPlugin
mapper.Mapper=tk.mybatis.mapper.common.Mapper
mapper.forceAnnotation=true
# 生成文件保存位置
#javabean生成位置
targetModelPackage=com.yoyling.domain
#xml映射文件的位置,这里的值是文件夹名字
targetXMLPackage=mapper
# 这里写的是mapper文件夹所在位置,写成src/main/resources
targetResourcesProject=src/main/resources/com/yoyling/
#dao接口存放的包位置
targetMapperPackage=com.yoyling.mapper
#目标工程,写main下的src即可
targetJavaProject=src/main/java
log4j.properties日志
我配置到的是DEBUG级别,并只保存到c的log4j.log,不在控制台打印。
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=DEBUG, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=c:\log4j.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
mybatis-generator.xml
这文件可配可不配,自动生成一些mapper和pojo,那我就不配了吧,手写代码。
spring-context.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: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">
<!-- 开启注解扫描,要扫描的是service和dao层的注解,要忽略web层注解,因为web层让SpringMVC框架去管理 -->
<context:component-scan base-package="com.yoyling">
<!-- 配置要忽略的注解 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置C3P0的连接池对象 -->
<context:property-placeholder location="classpath:dbconfig.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置SqlSession的工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置扫描mapper的包 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yoyling.mapper"/>
</bean>
</beans>
spring-mvc.xml主配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描controller的注解,别的不扫描 -->
<context:component-scan base-package="com.yoyling">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- JSP文件所在的目录 -->
<property name="prefix" value="/WEB-INF/pages/" />
<!-- 文件的后缀名 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 设置静态资源不过滤 -->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!-- 开启对SpringMVC注解的支持 -->
<mvc:annotation-driven />
</beans>
Score.java实体类
package com.yoyling.domain;
import java.io.Serializable;
public class Score implements Serializable {
private Integer scoreId;
private String xh;
private String kch;
private Integer cj;
private Float jd;
private Float xf;
private static final long serialVersionUID = 1L;
public Integer getScoreId() {return scoreId;}
public void setScoreId(Integer scoreId) {this.scoreId = scoreId;}
public String getXh() {return xh;}
public void setXh(String xh) {this.xh = xh == null ? null : xh.trim();}
public String getKch() {return kch;}
public void setKch(String kch) {this.kch = kch == null ? null : kch.trim();}
public Integer getCj() {return cj;}
public void setCj(Integer cj) {this.cj = cj;}
public Float getJd() {return jd;}
public void setJd(Float jd) {this.jd = jd;}
public Float getXf() {return xf;}
public void setXf(Float xf) {this.xf = xf;}
@Override
public String toString() {
return "Score{" +
"scoreId=" + scoreId +
", xh='" + xh + '\'' +
", kch='" + kch + '\'' +
", cj=" + cj +
", jd=" + jd +
", xf=" + xf +
'}';
}
}
ScoreMapper.java数据层接口
package com.yoyling.mapper;
import com.yoyling.domain.Score;
import org.springframework.stereotype.Repository;
@Repository
public interface ScoreMapper {
Score selectByPrimaryKey(Integer scoreId);
}
ScoreMapper.xml动态sql
<?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.yoyling.mapper.ScoreMapper">
<resultMap id="BaseResultMap" type="com.yoyling.domain.Score">
<id column="score_id" jdbcType="INTEGER" property="scoreId" />
<result column="xh" jdbcType="VARCHAR" property="xh" />
<result column="kch" jdbcType="VARCHAR" property="kch" />
<result column="cj" jdbcType="INTEGER" property="cj" />
<result column="jd" jdbcType="REAL" property="jd" />
<result column="xf" jdbcType="REAL" property="xf" />
</resultMap>
<sql id="Base_Column_List">
score_id, xh, kch, cj, jd, xf
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from score
where score_id = #{scoreId,jdbcType=INTEGER}
</select>
</mapper>
ScoreService.java服务层接口
package com.yoyling.service;
import com.yoyling.domain.Score;
public interface ScoreService {
Score selectByPrimaryKey(Integer scoreId);
}
ScoreServiceImpl.java实现
package com.yoyling.service.impl;
import com.yoyling.domain.Score;
import com.yoyling.mapper.ScoreMapper;
import com.yoyling.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("scoreService")
public class ScoreServiceImpl implements ScoreService {
@Autowired
private ScoreMapper scoreMapper;
@Override
public Score selectByPrimaryKey(Integer scoreId) {
return scoreMapper.selectByPrimaryKey(1);
}
}
ServiceTest.java后端测试
package com.yoyling;
import com.yoyling.domain.Score;
import com.yoyling.service.ScoreService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceTest {
@Test
public void run1() {
ApplicationContext ac = new
ClassPathXmlApplicationContext("classpath:spring-context.xml");
ScoreService scoreService = (ScoreService) ac.getBean("scoreService");
Score score = scoreService.selectByPrimaryKey(1);
System.out.println(score);
}
}
ScoreController.java前端测试
package com.yoyling.controller;
import com.yoyling.domain.Score;
import com.yoyling.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/score")
public class ScoreController {
@Autowired
private ScoreService scoreService;
@RequestMapping("/selectByPrimaryKey")
public String selectByPrimaryKey(Model model) {
Score score= scoreService.selectByPrimaryKey(1);
model.addAttribute("score",score);
return "demo";
}
}
index.jsp
<a href="score/selectByPrimaryKey">Test</a>
demo.jsp
${score}