SSM(spring+spring mvc+mybatis)框架搭建

版本:spring-4.3.3     mybatis-3.4.2     mybatis-spring-1.3.1      jdk-1.8     tomcat-8.0

一、数据库

创建ssmdemo1数据库,创建 category 表,并向表中添加几条数据。category 表只有两个属性:id 和 name 


二、创建web工程,引入jar包

所有jar包及工程结构如图所示


三、实体类 Category.java

package com.cxh.entity;
/**
 * 对应数据库的实体类
 */
public class Category {

	private int id;
	private String name;
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

四、mapper 类 CategoryMapper.java (代替dao)层

package com.cxh.mapper;

import java.util.List;
import com.cxh.entity.Category;

/**
 * 对应xml中的接口方法
 */
public interface CategoryMapper {

	public void add(Category category);
	public void delete(int id);
	public Category get(int id);
	public void update(Category category);
	public List<Category> list();
	public int count();
}

五、同一个包下建立xml文件 Category.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">

<!-- namaspace必须写接口的完整类名 -->
<mapper namespace="com.cxh.mapper.CategoryMapper">
	
	<insert id="add" parameterType="com.cxh.entity.Category">
		insert into CATEGORY (name) values (#{name})
	</insert>
	
	<delete id="delete" parameterType="int">
		delete from CATEGORY where id = #{_parameter}
	</delete>

	<select id="get" parameterType="int" resultType="com.cxh.entity.Category">
		select id,name from CATEGORY where id = #{_parameter}
	</select>
	
	<update id="update" parameterType="com.cxh.entity.Category">
		update CATEGORY set name = #{name} where id = #{id}
	</update>
	
	<select id="list" resultType="com.cxh.entity.Category">
		select id,name from CATEGORY
	</select>
	
	<select id="count" resultType="int">
		select count(*) from CATEGORY
	</select>
</mapper>

六、service 层 CategoryService.java 和实现类 CategoryServiceImpl.java

package com.cxh.service;

import java.util.List;
import com.cxh.entity.Category;

public interface CategoryService {

	List<Category> list();
}


package com.cxh.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cxh.entity.Category;
import com.cxh.mapper.CategoryMapper;

@Service
public class CategoryServiceImpl implements CategoryService {

	@Autowired
	private CategoryMapper categoryMapper;
	
	@Override
	public List<Category> list() {
		return categoryMapper.list();
	}

}

七、controller 层 CategoryController.java


package com.cxh.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.cxh.entity.Category;
import com.cxh.service.CategoryService;

// 告诉spring mvc 这是一个控制器类
@Controller
// 访问地址映射
@RequestMapping("")
public class CategoryController {

	@Autowired
	CategoryService categoryService;

	@RequestMapping("listCategory")
	public ModelAndView listCategory() {
		ModelAndView mav = new ModelAndView();

		List<Category> cs = categoryService.list();

		// 放入转发参数
		mav.addObject("cs", cs);
		// 放入jsp路径
		mav.setViewName("listCategory");
		return mav;
	}
}

八、配置 web.xml 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SSMDemo1</display-name>
  
  <!-- spring的配置文件 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- spring mvc核心:分发servlet -->
  <servlet>
  	<servlet-name>mvc-dispatcher</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	
  	<!-- spring mvc的配置文件 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>mvc-dispatcher</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

九、src目录下新建 applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     
     <context:annotation-config/>
     <context:component-scan base-package="com.cxh.service"/>
     
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     	<property name="driverClassName">
     		<value>com.mysql.jdbc.Driver</value>
     	</property>
     	<property name="url">
     		<value>jdbc:mysql://localhost:3306/ssmdemo1?characterEncoding=UTF-8</value>
     	</property>
     	<property name="username">
     		<value>root</value>
     	</property>
     	<property name="password">
     		<value>root</value>
     	</property>
     </bean>
     
     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
     	<property name="typeAliasesPackage" value="com.cxh.entity"></property>
     	<property name="dataSource" ref="dataSource"></property>
     	<property name="mapperLocations" value="classpath:com/cxh/mapper/*.xml"></property>
     </bean>
     
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     	<property name="basePackage" value="com.cxh.mapper"></property>
     </bean>
     
     
</beans>

十、src目录下新建 springmvc.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
        
     <context:annotation-config/>
     <context:component-scan base-package="com.cxh.controller">
     	<context:include-filter type="annotation"
     		 expression="org.springframework.stereotype.Controller"></context:include-filter>
     </context:component-scan>
     
     <mvc:annotation-driven></mvc:annotation-driven>
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    
    <!--视图定位 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="viewClass"
    		value="org.springframework.web.servlet.view.JstlView"></property>
    	<property name="prefix" value="/WEB-INF/jsp/"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>      
        
</beans>

十一、WEB-INF 下建个jsp文件夹,里面新建 listCategory.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<table align='center' border='1' cellspacing='0'>
		<tr>
			<td>id</td>
			<td>name</td>
		</tr>
		<c:forEach items="${cs}" var="c" varStatus="st">
			<tr>
				<td>${c.id}</td>
				<td>${c.name}</td>

			</tr>
		</c:forEach>
	</table>

</body>
</html>

十二、 src目录下新建 log4j.properties

log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
#只输出有用信息
log4j.logger.org.apache=DEBUG


部署在 tomcat 中,然后在浏览器中输入 
http://127.0.0.1:8080/SSMDemo1/listCategory






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值