毕设项目SSM框架搭建笔记
在毕业设计项目中第一次使用SSM框架,在网上搜了许多资料,阅读了许多博客,磕磕碰碰总算是成功了,由此记录,以便以后查看。
工程目录结构:
以下是配置文件举例:
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpotShare</groupId>
<artifactId>SpotShare</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpotShare</name>
<description />
<properties>
<webVersion>3.1</webVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.14.RELEASE</spring.version>
<mybatis.version>3.4.1</mybatis.version>
<jackson.version>2.9.4</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring 包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis 包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- MySQL 包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.20</version>
</dependency>
<!-- dbcp 包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.2.0</version>
</dependency>
<!-- json 包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
spring-mybatis.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" xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<context:annotation-config /> <!-- 此行语句使得resource autowired 等四个注解可以使用 -->
<!-- 配置数据库连接参数及连接池 -->
<!-- 配置destroy-method="close" 是因为出现关闭TomCat时出现警告:
This is very likely to create a memory leak. Stack trace of thread:java.lang.Object.wait(Native Method)
出现此问题后 更换了mysql-connector版本 然后在dao层加上@Repository 注解,同时配置上destroy-method="close"
虽然并不知道是哪个起了作用,但是还是解决问题了-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <!-- 连接所用驱动 -->
<property name="url" value="jdbc:mysql:///spotshare"></property> <!-- jdbc:mysql://localhost:3306/spotshare -->
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 配置mapper扫描 Dao接口所在包 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.spotshare.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解扫描 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
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" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<!-- 启动注解驱动 -->
<!--自定义消息转换器的编码,解决后台传输json回前台时,中文乱码问题 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=utf-8</value>
<value>text/html;charset=utf-8</value>
<!-- application 可以在任意 form 表单里面 enctype 属性默认找到 -->
<value>application/x-www-form-urlencoded</value>
</list>
</property>
</bean>
<!-- 解决ResponseBody返回的json数据中时间类型的数据未格式化且只返回时间戳的问题 -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"></constructor-arg>
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler />
<!-- 组件扫描 -->
<context:component-scan base-package="com.spotshare" />
<!-- 对静态资源文件的访问,因为Spring MVC会拦截所有请求,导致jsp页面中对js和CSS的引用也被拦截,配置后可以把对资源的请求交给项目的
默认拦截器而不是Spring MVC -->
<mvc:resources mapping="/static/**" location="/WEB-INF/static/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
以下是个别文件举例:
mapper
<?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.spotshare.dao.SpotDao">
<select id="getSpots" parameterType="int" resultType="com.spotshare.entity.Spot">
select *
from spot where DELETEFLAG != '1'
order by click desc limit #{offset},#{pageSize}
</select>
<select id="getCountSpot" resultType="int">
select count(*) from spot where DELETEFLAG != '1'
</select>
<select id="getSpot" parameterType="String" resultType="com.spotshare.entity.Spot">
select *
from spot where KEYID = #{keyId}
</select>
<update id="updateSpot" parameterType="com.spotshare.entity.Spot">
update spot set NAME =
#{name}, DISCRIPTION = #{discription}, IMAGEURL =
#{imageUrl},
PROVINCE
= #{province}, CITY = #{city}, CREATEDATE = #{createDate},
UPDATEDATE =
#{updateDate}, CREATEUSER = #{createUser}, UPDATEUSER =
#{updateUser},
DELETEFLAG = #{deleteFlag}, CLICK = #{click}
where KEYID = #{keyId}
</update>
<insert id="addSpot" parameterType="com.spotshare.entity.Spot">
insert into spot
(KEYID,NAME,DISCRIPTION,IMAGEURL,PROVINCE,CITY,CREATEDATE,UPDATEDATE,CREATEUSER,UPDATEUSER,DELETEFLAG,CLICK)
values
(#{keyId},#{name},#{discription},#{imageUrl},#{province},#{city},#{createDate},#{updateDate},#{createUser},#{updateUser},#{deleteFlag},#{click})
</insert>
</mapper>
dao
package com.spotshare.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import com.spotshare.entity.Spot;
@Repository
public interface SpotDao {
public List<Spot> getSpots(@Param("offset")int offset, @Param("pageSize")int pageSize);
public void addSpot(Spot spot);
public void deleteSpot(Spot spot);
public void updateSpot(Spot spot);
public Spot getSpot(String keyId);
public List<Spot> getSpotList(String spotName);
public int getCountSpot();
}
ServiceImpl
package com.spotshare.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.spotshare.dao.SpotDao;
import com.spotshare.entity.Spot;
@Service
public class SpotServiceImpl implements SpotService {
@Resource
private SpotDao spotDao;
@Override
public void addSpot(Spot spot) {
// TODO Auto-generated method stub
spotDao.addSpot(spot);
}
}
Controller
package com.spotshare.controller;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.spotshare.entity.Spot;
import com.spotshare.service.SpotService;
import com.spotshare.tools.*;
@Controller
@RequestMapping("/spot")
public class SpotController {
@Resource
SpotService spotService;
@ResponseBody
@RequestMapping("/spotContent.do")
public Map<String, Object> spotContent(String keyId) {
Spot spot = spotService.getSpot(keyId);
spotService.click(spot);
return MyUtils.createResultMap(true, spot, null);
}
}