<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">这个项目是一个网页版的标签管理系统,用来管理网页标签,使用maven来管理jar包。</span></span>
下面进行项目的搭建整合:
1、创建一个maven项目,并在pom.xml中添加jar包依赖:
添加jar包依赖
<span style="font-size:18px;"> <!-- 单元测试的包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 日志包 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
<!-- 引入Spring的依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 引入Spring mvc的依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- jackson需要的包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.3</version>
</dependency>
<!-- 引入jsr250注解的依赖包 -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<!-- spring整合jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- mybatis支持包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.1</version>
</dependency>
<!-- spring整合mybatis支持包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mysql的驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- j2ee支持包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>1.2</version>
</dependency>
<!-- redis的java框架 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>opensymphony</groupId>
<artifactId>webwork</artifactId>
<version>2.2.5</version>
</dependency></span>
2、搭建项目框架
3、在web.xml中配置spring和spring mvc支持
<span style="font-size:18px;"><!-- web容器的上下文的参数,这个参数指定了spring的配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring.xml</param-value>
</context-param>
<!-- 启动spring的监听 器以初始化spring容器 = 先写spring的listener,在写自己的listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping></span>
4、配置spring.xml文件
<span style="font-size:18px;"><!-- 包扫描以加载带了注解的要托管bean -->
<context:component-scan base-package="com.yc.*"/>
<!-- 启用事务注解驱动 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- spring 整合mybatis -->
<!-- 1.1因为要创建数据源,要知道数据库的连接配置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath*:db.properties" />
</bean>
<!-- 1.2使用spring2.5的属性文件配置方案 -->
<!-- <context:property-placeholder location="classpath:db.properties"/> -->
<!-- 2、获取数据源:官方有三种:jdbc,dbcp,c3p0 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- 3、配置mybatis整合spring插件 创建mybatis的SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.yc.bean">
</property>
<property name="mapperLocations">
<array>
<value>classpath*:com/yc/dao/mapper/WebsiteMapper.xml</value>
<value>classpath*:com/yc/dao/mapper/TagMapper.xml</value>
</array>
</property>
<property name="configurationProperties">
<props>
<prop key="logImpl">LOG4J</prop>
</props>
</property>
</bean>
<!-- 4、创建SqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- 添加事务 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</span>
<span style="font-size:18px;"><!-- 开启注解-->
<mvc:annotation-driven />
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.yc.web.*"/>
<!--注解映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 处理静态资源导入 -->
<mvc:default-servlet-handler />
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 配置视图解析 InternalResourceViewResolver 通过prefix + return + suffix得到实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean></span>
6、编写相应的Listener,用于加载项目中频繁访问的公共资源和初始化资源。
<span style="font-size:18px;">package com.yc.web.listeners;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.yc.bean.Tag;
import com.yc.biz.TagBiz;
import com.yc.util.YcConstants;
/**
* Application Lifecycle Listener implementation class InitListener
*
*/
@WebListener
public class InitListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce){
// 取出spring容器
ApplicationContext ac = WebApplicationContextUtils
.getWebApplicationContext(sce.getServletContext());
TagBiz tb = (TagBiz) ac.getBean("tagBizImpl");
Map<String,Tag> map = new HashMap<String, Tag>();
map = tb.getAllTag();
sce.getServletContext().setAttribute(YcConstants.ALLTAG, map);
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO 自动生成的方法存根
}
}
</span>
因为我们的类都是使用spring进行了托管,要想在Listener中获取类的实体就要用
<span style="font-size:18px;">ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());</span>
<span style="font-size:18px;">来获取spring的容器,再通过</span><pre name="code" class="java"><span style="font-size:18px;">TagBiz tb = (TagBiz) ac.getBean("tagBizImpl");</span>
获取实例对象。
7、编写Controller 控制器。
<span style="font-size:18px;">package com.yc.web.action;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import com.opensymphony.webwork.ServletActionContext;
import com.yc.bean.Tag;
import com.yc.bean.Website;
import com.yc.biz.WebsiteBiz;
import com.yc.util.YcConstants;
import com.yc.web.model.JsonModel;
@Controller
@Scope(value="prototype")
public class WebTagAction{
private static final long serialVersionUID = -1668688449916335110L;
private WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
private ServletContext application= webApplicationContext.getServletContext();
private JsonModel jsonModel;
private Website website;
private WebsiteBiz websiteBiz;
@RequestMapping(value="/getTags", method=RequestMethod.POST)
@ResponseBody
public JsonModel getTags(){
@SuppressWarnings("unchecked")
Map<String, Tag> map = (Map<String, Tag>) application.getAttribute(YcConstants.ALLTAG);
jsonModel = new JsonModel();
if(map != null){
jsonModel.setCode(1);
jsonModel.setObj(map);
}else{
jsonModel.setCode(0);
jsonModel.setMsg("没有取到数据。");
}
return jsonModel;
}
@RequestMapping(value="/getWebByTag", method=RequestMethod.POST)
@ResponseBody
public JsonModel getWebByTag(String tname){
website = new Website();
website.setTname(tname);
List<Website> list = websiteBiz.getWebsitesByTagName(website);
jsonModel = new JsonModel();
jsonModel.setCode(1);
jsonModel.setObj(list);
return jsonModel;
}
@RequestMapping(value="/addWebTag", method=RequestMethod.POST)
@ResponseBody
public JsonModel addWebTag(Website website){
Map<String, Tag> map = (Map<String, Tag>) application.getAttribute(YcConstants.ALLTAG);
jsonModel = new JsonModel();
try {
websiteBiz.add(website, map);
jsonModel.setCode(1);
jsonModel.setObj("添加成功!");
} catch (Exception e) {
jsonModel.setCode(1);
jsonModel.setMsg("添加失败!");
}
return jsonModel;
}
@Resource(name="websiteBizImpl")
public void setWebsiteBiz(WebsiteBiz websiteBiz) {
this.websiteBiz = websiteBiz;
}
}
</span>
用@RequestMapping(value="/getTags", method=RequestMethod.POST) 注解访问的地址和请求方式。 如果使用 jackson 来传递接送数据 就要使用@ResponseBody注解
<span style="font-size:18px;">使用<span style="font-family: Arial, Helvetica, sans-serif;">@Resource(name="websiteBizImpl") ,进行注入操作。</span></span>
<span style="font-size:18px;"> @Resource(name="websiteBizImpl")
public void setWebsiteBiz(WebsiteBiz websiteBiz) {
this.websiteBiz = websiteBiz;
}</span>
8、编写业务层 使用@Service注解方式
<span style="font-size:18px;">package com.yc.biz.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.yc.bean.Tag;
import com.yc.bean.Website;
import com.yc.bean.Wt;
import com.yc.biz.WebsiteBiz;
import com.yc.dao.TagDao;
import com.yc.dao.WebsiteDao;
@Service("websiteBizImpl")
public class WebsiteBizImpl implements WebsiteBiz {
private WebsiteDao websiteDao;
private TagDao tagDao;
@Transactional(readOnly = true, isolation = Isolation.DEFAULT, rollbackForClassName = { "java.lang.RuntimeException" }, propagation = Propagation.NOT_SUPPORTED)
public Website add(Website website,Map<String, Tag> tagMap) {
String tags = website.getTags().trim();
List<Wt> wts = new ArrayList<Wt>();
websiteDao.add(website);
if(tags != null && !"".equals(tags)){
String[] ts= tags.split(",");
if(ts != null && ts.length > 0){
for (String t : ts) {
Wt wt = new Wt();
wt.setWid(website.getWid());
Tag tag;
if(tagMap != null && tagMap.size() > 0 && tagMap.containsKey(t)){
//更新操作
tag = tagMap.get(t);
tag.setTcount(tag.getTcount() + 1);
tagDao.update(tag);
wt.setTid(tag.getTid());
}else {
//添加操作
tag = new Tag();
tag.setTname(t);
tag.setTcount(1);
tagDao.add(tag);
wt.setTid(tag.getTid());
tagMap.put(t, tag);
}
wts.add(wt);
}
websiteDao.addWt(wts);
}
}
return website;
}
@Transactional(readOnly = true, isolation = Isolation.DEFAULT, rollbackForClassName = { "java.lang.RuntimeException" }, propagation = Propagation.NOT_SUPPORTED)
public List<Website> getWebsitesByTagName(Website website) {
if(website == null){
return null;
}
if("未分类".equals(website.getTname())){
return websiteDao.searchUnType();
}
if("全部".equals(website.getTname())){
website.setTname(null);
}
return websiteDao.search(website);
}
@Resource(name = "websiteDaoImpl")
public void setWebsiteDao(WebsiteDao websiteDao) {
this.websiteDao = websiteDao;
}
@Resource(name = "tagDaoImpl")
public void setTagDao(TagDao tagDao) {
this.tagDao = tagDao;
}
}
</span>
使用 @Transactional 进行控制事务管理9、进行dao层编写 使用@Repository 注解
<span style="font-size:18px;">package com.yc.dao.impl;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;
import com.yc.bean.Website;
import com.yc.bean.Wt;
import com.yc.dao.WebsiteDao;
@Repository
public class WebsiteDaoImpl extends SqlSessionDaoSupport implements WebsiteDao {
//重写了父类 SqlSessionDaoSupport 方法实现注入 sqlSessionTemplate
@Resource(name="sqlSession")
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
super.setSqlSessionTemplate(sqlSessionTemplate);
}
@Override
public int add(Website site) {
super.getSqlSession().insert("com.yc.dao.mapper.WebsiteMapper.add",site);
return site.getWid();
}
@Override
public void addWt(List<Wt> wts) {
super.getSqlSession().insert("com.yc.dao.mapper.WebsiteMapper.addWt",wts);
}
@Override
public List<Website> search(Website site) {
List<Website> Websites = new ArrayList<Website>();
Websites = super.getSqlSession().selectList("com.yc.dao.mapper.WebsiteMapper.search",site);
return Websites;
}
@Override
public List<Website> searchUnType() {
List<Website> Websites = new ArrayList<Website>();
Websites = super.getSqlSession().selectList("com.yc.dao.mapper.WebsiteMapper.searchUnType");
return Websites;
}
}
</span>
继承 SqlSessionDaoSupport 重写了父类 SqlSessionDaoSupport 方法实现注入 sqlSessionTemplate
现在项目就基本整合完成了,在ssm整合的过程中有道德问题将在下一篇文章中详细介绍。