上一篇我使用maven创建一个web项目Maven项目的搭建 。今天将在那个的基础上进行完善,做成SSM模式的web框架.
其中log4j.properties为日志文件;
spring-mvc.xml为springMVC的配置文件;
spring-mybatis.xml为spring和mybatis的整合文件.
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>test-maven-webdisplay-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mybatis.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
listener>
<servlet>
<servlet-name>SpringMVCservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
<async-supported>trueasync-supported>
servlet>
<servlet-mapping>
<servlet-name>SpringMVCservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<context-param>
<param-name>log4jConfigLocationparam-name>
<param-value>classpath:log4j.propertiesparam-value>
context-param>
<filter>
<filter-name>characterEncodingfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
加载Spring和mybatis的配置文件
Spring监听器
防止Spring内溢出监听器
Spring MVC
字符编码过滤器
<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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
default-autowire="byName">
<context:component-scan base-package="com.cn.service" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/testsql?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123456" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/cn/mapping/*.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
beans>
<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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.cn.controller" />
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8value>
list>
property>
bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp" />
<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:default-servlet-handler/>
<mvc:annotation-driven/>
beans>
至此,SSM框架已经搭载完成了,
现在写功能进行测试吧
在此先说一下mybatis的Generator代码生成工具,可以通过数据库表,直接生成实体类,dao接口,以及mapper的增删改查,不清楚的可以查看我写的博文,使用Generator自动生成代码 。
数据库中有一张表,结构如下:
先在Service声明一个接口一个实现类:
接口代码如下:
package com.cn.service;
import com.cn.pojo.Root;
public interface RootService {
/**
* 通过id主键查询数据
* @return
*/
public Root selectByPrimaryKey(Integer id);
}
实现类通过@Autowired调用dao层接口进行数据查询
package com.cn.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cn.dao.RootMapper;
import com.cn.pojo.Root;
import com.cn.service.RootService;
@Service("rootService")
public class RootServiceImpl implements RootService {
@Autowired
private RootMapper rootMapper;
@Override
public Root selectByPrimaryKey(Integer id) {
// TODO Auto-generated method stub
return this.rootMapper.selectByPrimaryKey(id);
}
}
Controller层代码:
package com.cn.controller;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cn.pojo.Root;
import com.cn.service.RootService;
@Controller
public class RootController {
private static Logger logger = Logger.getLogger(RootController.class);
@Autowired
private RootService rootservice = null;
@RequestMapping(value = "/manage/rootId")
@ResponseBody
public String rootById(HttpServletRequest request) {
String id = request.getParameter("id");
logger.info("通过id进行Root查询,id是:" + id);
Root root = rootservice.selectByPrimaryKey(1);
if (root == null) {
return "no data!";
} else {
return root.getRootName();
}
}
}
index.jsp页面代码:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.2.1.js">script>
<title>index页面title>
head>
<body>
<h1>七夕节h1>
id:<input type="text" id="txtId"><span id="contentSpan">span><br/>
<input type="button" value="查询" id="searchBtn">
<script type="text/javascript">
$(function() {
$("#searchBtn").click(function() {
var id = $("#txtId").val();
$.post("${pageContext.request.contextPath}/manage/rootId",{"id":id}, function (data) {
$("#contentSpan").text(data);
})
})
});
script>
body>
html>