ssm原始碼

ssm原始碼

ssm專案原始碼,有需要的可以聯絡我
這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述

關鍵程式碼如下
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"
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.ssm.gf" />
<!-- 引入配置檔案 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${uname}" />
<property name="password" value="${pwd}" />
<!-- 初始化連線大小 -->
<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/ssm/gf/mapping/*.xml"></property>
</bean>
<!-- DAO介面所在包名,Spring會自動查詢其下的類 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.gf.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</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"
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.ssm.gf.controller" />
<!--避免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 name="suffix" value=".jsp" />
</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:interceptors>  
<!-- 多個攔截器,順序執行 -->  
<!-- 登入認證攔截器 -->  
<mvc:interceptor>  
<mvc:mapping path="/**"/>  
<bean class="com.ssm.gf.Interceptor.LoginInterceptor"/>  
</mvc:interceptor>  
</mvc:interceptors>  
</beans>

jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/jf3q_ssm?useUnicode=true&characterEncoding=utf-8
uname=xxx
pwd=xxx
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000

以廣告管理為例
Ad.java實體

package com.ssm.gf.model;
public class Ad {
private Integer aid;
private String adimg;
private String cts;
private Integer status;
private String link;
private String title;
private String note;
private String asort;
public Integer getAid() {
return aid;
}
public void setAid(Integer aid) {
this.aid = aid;
}
public String getAdimg() {
return adimg;
}
public void setAdimg(String adimg) {
this.adimg = adimg;
}
public String getCts() {
return cts;
}
public void setCts(String cts) {
this.cts = cts;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getAsort() {
return asort;
}
public void setAsort(String asort) {
this.asort = asort;
}
/*以下是輔助欄位,用於分頁*/
private Integer pageSize;
private Integer startRow;
private Integer pageNo;
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getStartRow() {
return startRow;
}
public void setStartRow(Integer startRow) {
this.startRow = startRow;
}
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
/*以下是輔助欄位,用於批量*/
private String ids;
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
}

AdMapper.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.gf.dao.AdMapper">
<sql id="field"> aid,adimg,status,cts,title,link,note,asort </sql>
<resultMap id="map" type="com.ssm.gf.model.Ad">
<id column="aid" property="aid" />
<result column="adimg" property="adimg" />
<result column="status" property="status" />
<result column="cts" property="cts" />
<result column="title" property="title" />
<result column="link" property="link" />
<result column="note" property="note" />
<result column="asort" property="asort" />
</resultMap>
<insert id="add" parameterType="com.ssm.gf.model.Ad">
insert into ad (adimg,cts,title,link,note,asort)
values(#{adimg},#{cts},#{title},#{link},#{note},#{asort})
</insert>
<select id="queryList" resultType="map" parameterType="com.ssm.gf.model.Ad">
select
<include refid="field"></include>
from ad where 1=1
<if test="title != null and title != ''">
and title like #{title}
</if>
order by cts desc limit #{startRow},#{pageSize}
</select>
<select id="queryCount" resultType="java.lang.Long" parameterType="com.ssm.gf.model.Ad">
select count(aid) from ad where 1=1
<if test="title != null and title != ''">
and title like #{title}
</if>
</select>
<select id="queryById" resultType="com.ssm.gf.model.Ad"
parameterType="java.lang.String">
select
<include refid="field"></include>
from ad where aid=#{0}
</select>
<update id="update" parameterType="com.ssm.gf.model.Ad">
update ad
<trim prefix="set" suffixOverrides=",">
<if test="title!=null and title != ''">title=#{title},</if>
<if test="adimg!=null and adimg != ''">adimg=#{adimg},</if>
<if test="note!=null and note != ''">note=#{note},</if>
<if test="link!=null and link != ''">link=#{link},</if>
<if test="status!=null">status=#{status},</if>
<if test="asort!=null and asort!=''">asort=#{asort},</if>
</trim>
where aid=#{aid}
</update>
<delete id="deleteById" parameterType="java.lang.String">
delete from ad where aid=#{0}
</delete>
</mapper>

dao層的AdMapper.java

package com.ssm.gf.dao;
import java.util.List;
import com.ssm.gf.model.Ad;
public interface AdMapper {
public void add(Ad a);
public List<Ad> queryList(Ad a);
public Long queryCount(Ad a);
public Ad queryById(String aid);
public void update(Ad a);
public void deleteById(String aid);
}

service層

package com.ssm.gf.service;
import java.util.List;
import com.ssm.gf.model.Ad;
public interface AdService {
public void add(Ad a); 
public List<Ad> queryList(Ad a);
public Long queryCount(Ad a);
public Ad queryById(String aid);
public void update(Ad a);
public void deleteById(String aid);
}

service層impl

 import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ssm.gf.dao.AdMapper;
import com.ssm.gf.model.Ad;
import com.ssm.gf.service.AdService;
@Service("adService")
public class AdServiceImpl implements AdService{
@Resource
private AdMapper adMapper;
@Override
public void add(Ad a) {
adMapper.add(a);
}
@Override
public List<Ad> queryList(Ad a) {
return adMapper.queryList(a);
}
@Override
public Long queryCount(Ad a) {
return adMapper.queryCount(a);
}
@Override
public Ad queryById(String aid) {
return adMapper.queryById(aid);
}
@Override
public void update(Ad a) {
adMapper.update(a);
}
@Override
public void deleteById(String aid) {
adMapper.deleteById(aid);
}
}

controller

package com.ssm.gf.controller.admin;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.ssm.gf.cons.Sys;
import com.ssm.gf.model.Ad;
import com.ssm.gf.model.Page;
import com.ssm.gf.service.AdService;
import com.ssm.gf.untils.DateUtils;
import com.ssm.gf.untils.LikeQuery;
import com.ssm.gf.untils.UploadFile;
@Controller
@RequestMapping("/admin/ad")
public class AdminAdController {
@Resource
private AdService adService;
Page page=new Page();
@RequestMapping("list")
public String list(Ad ad,Model m){
String title=ad.getTitle();
ad.setTitle(LikeQuery.add(title));
if(ad.getPageNo()==null)ad.setPageNo(1);
if(ad.getPageSize()==null)ad.setPageSize(Sys.Common.pageSize);
page.setPageNo(ad.getPageNo());
page.setPageSize(ad.getPageSize());
page.setStartRow(page.getPageNo(), page.getPageSize());
page.setCount(adService.queryCount(ad));
page.setPages(page.getCount(), page.getPageSize());
ad.setStartRow(page.getStartRow());
List<Ad> li=adService.queryList(ad);
ad.setTitle(title);
m.addAttribute("ad", ad);
m.addAttribute("page", page);
m.addAttribute("li", li); 
return Sys.Common.admin "/ad_list";
}
@RequestMapping("add")
public String add(){
return Sys.Common.admin "/ad_add";
}
@RequestMapping("added")
public String added(Ad ad,@RequestParam(value = "file_adimg",required = true) MultipartFile file,HttpServletRequest request,Model m){
String adimg=UploadFile.upimg(file, request);
ad.setAdimg(adimg);
ad.setCts(DateUtils.DateTimeToString(new Date())); 
adService.add(ad);
return list(ad, m);
}
@RequestMapping("update")
public String update(@RequestParam String aid,Model m){
Ad ad=adService.queryById(aid);
m.addAttribute("ad", ad);
return Sys.Common.admin "/ad_update";
}
@RequestMapping("updated")
public String updated(Ad ad,@RequestParam(value = "file_adimg",required = false) MultipartFile file,HttpServletRequest request,Model m){
String adimg="";
try {
if(file!=null&&file.getBytes().length>0){
adimg=UploadFile.upimg(file, request);
}
} catch (IOException e) {
e.printStackTrace();
}
if(StringUtils.isNotBlank(adimg)){
String oldimg=ad.getAdimg();
UploadFile.deleteFile(request, oldimg);
ad.setAdimg(adimg);
}
adService.update(ad);
return list(ad, m);
}
@RequestMapping("isuse")
public String isUse(Ad ad,Model m){
Ad oldA=adService.queryById(ad.getAid() "");
Ad a=new Ad();
if(oldA.getStatus()==Sys.Common.USE)a.setStatus(Sys.Common.NO_USE);
else a.setStatus(Sys.Common.USE);
a.setAid(ad.getAid());
adService.update(a);
return list(ad, m);
}
@RequestMapping("del")
public String del(Ad ad,HttpServletRequest request,Model m){
Ad oldA=adService.queryById(ad.getAid() "");
UploadFile.deleteFile(request, oldA.getAdimg());
adService.deleteById(ad.getAid() "");
return list(ad, m);
}
@RequestMapping("pluse")
public String plUse(Ad ad,Model m){
String[] idArr=ad.getIds().split(",");
for(String id:idArr){
Ad a=new Ad();
a.setAid(Integer.parseInt(id));
a.setStatus(Sys.Common.USE);
adService.update(a);
}
return list(ad, m);
}
@RequestMapping("plnouse")
public String plNoUse(Ad ad,Model m){
String[] idArr=ad.getIds().split(",");
for(String id:idArr){
Ad a=new Ad();
a.setAid(Integer.parseInt(id));
a.setStatus(Sys.Common.NO_USE);
adService.update(a);
}
return list(ad, m);
}
@RequestMapping("pldel")
public String pldel(Ad ad,HttpServletRequest request,Model m){
String[] idArr=ad.getIds().split(",");
for(String id:idArr){
Ad oldA=adService.queryById(id "");
UploadFile.deleteFile(request, oldA.getAdimg());
adService.deleteById(id);
}
return list(ad, m);
}
}