一个ssm框架小例子

ssm小例子:eclipse+jdk1.8+spring4.3.9+springmvc4.3.9+mybatis3.2.7 +Tomcat8.5

maven开发

jar包:
在这里插入图片描述在这里插入图片描述
一、目录
在这里插入图片描述在这里插入图片描述
二、相应的代码文件
1.src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	
  <display-name>Archetype Created Web Application</display-name>
 
  <!--配置spring容器  -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/applicationContext-*.xml</param-value>
  </context-param>
  <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:spring/springmvc.xml</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<!-- 第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析 
			   第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析使用此种方式可以实现 RESTful风格的url 
			   第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时, 仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错。 -->
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
  
	<!--post乱码过滤器  -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
  
</web-app>

2.src/main/resources/spring/applicationContext-dao.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
    	<context:property-placeholder location="classpath:db.properties"/>	
   <!--配置数据源,dbcp  --> 
   
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="30" />
		<property name="maxIdle" value="5" />
	</bean>
   
	<!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
	</bean>
	<!-- mapper扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!--扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
		<property name="basePackage" value="com.qhua.ssm.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	
</beans>

3.src/main/resources/spring/applicationContext-service.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
	

 <!-- 商品管理的service -->

<bean id="itemsService" class="com.qhua.ssm.service.impl.ItemsServiceImpl"/>  
</beans>

4.src/main/resources/spring/applicationContext-transaction.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
		
    
<!-- 事务管理器 
	对mybatis操作数据库事务控制,spring使用jdbc的事务控制类
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<!-- 数据源
	dataSource在applicationContext-dao.xml中配置了
	 -->
	<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
	<tx:attributes>
		<!-- 传播行为 -->
		<tx:method name="save*" propagation="REQUIRED"/>
		<tx:method name="delete*" propagation="REQUIRED"/>
		<tx:method name="insert*" propagation="REQUIRED"/>
		<tx:method name="update*" propagation="REQUIRED"/>
		<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
		<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
		<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
	</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
	<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.qhua.ssm.service.impl.*.*(..))"/>
</aop:config>
    		
   
</beans>

5.src/main/resources/spring/springmvc.xml

<!-- 可以扫描controller、service、...
这里让扫描controller,指定controller的包
 -->
<context:component-scan base-package="com.qhua.ssm.controller"></context:component-scan>

	
<!--注解映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
<!--注解适配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->

<!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
mvc:annotation-driven默认加载很多的参数绑定方法,
比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
实际开发时使用mvc:annotation-driven
 -->
<mvc:annotation-driven></mvc:annotation-driven>


<!-- 视图解析器
解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包
 -->
<bean
	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<!-- 配置jsp路径的前缀 -->
	<property name="prefix" value="/jsp/"/>
	<!-- 配置jsp路径的后缀 -->
	<property name="suffix" value=".jsp"/>
</bean>

<!-- 自定义参数绑定 -->
<!-- <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
	转换器
	<property name="converters">
		<list>
			日期类型转换
			<bean class="com.qhua.ssm.controller.converter.CustomDateConverter"/>
		</list>
	</property>
	

</bean>    -->

6.src/main/resources/mybatis/sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
	<!-- 全局setting配置,根据需要添加 -->

	<!-- 配置别名 -->
	<typeAliases>
		<!-- 批量扫描别名 -->
		<package name="com.qhua.ssm.po"/>
	</typeAliases>

	<!-- 配置mapper 由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。 必须遵循:mapper.xml和mapper.java文件同名且在一个目录 -->

</configuration>

7.src/main/resources/db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=1235678

8.src/main/resources/log4j.properties

# Global logging configuration\uff0c\u5efa\u8bae\u5f00\u53d1\u73af\u5883\u4e2d\u8981\u7528debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

9.com/qhua/ssm/controller/ItemsController.java

package com.qhua.ssm.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qhua.ssm.po.Items;
import com.qhua.ssm.service.ItemsService;
import com.qhua.ssm.service.impl.ItemsServiceImpl;

@Controller
public class ItemsController {

	//@Resource  //@Resource是需要写名字的,例如@Resource("userDao")。
	@Autowired   //如果你不想写名字,改用@Autowired。
	private ItemsService itemsService;

	@RequestMapping("/queryItems")
	public String getAllItems(HttpServletRequest request,Model model) {
		List<Items> list=itemsService.getAllItems();
		model.addAttribute("list",list);
		return "index";
	}
}

10.com/qhua/ssm/service/ItemsService.java

package com.qhua.ssm.service;

import java.util.List;

import com.qhua.ssm.po.Items;

public interface ItemsService {
	
	List<Items> getAllItems();
}

11.com/qhua/ssm/service/impl/ItemsServiceImpl.java

package com.qhua.ssm.service.impl;

import java.util.List;

import javax.annotation.Resource;

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

import com.qhua.ssm.mapper.ItemsMapper;
import com.qhua.ssm.po.Items;
import com.qhua.ssm.service.ItemsService;

@Service
public class ItemsServiceImpl implements ItemsService{

	//@Resource
	@Autowired
	private ItemsMapper itemsMapper;

	@Override
	public List<Items> getAllItems() {
		return itemsMapper.getAllItems();
	}

}

12.com/qhua/ssm/mapper/ItemsMapper.java

package com.qhua.ssm.mapper;

import java.util.List;

import com.qhua.ssm.po.Items;

public interface ItemsMapper {

	List<Items> getAllItems();
}

13.com/qhua/ssm/mapper/ItemsMapper.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.qhua.ssm.mapper.ItemsMapper">
    <resultMap type="com.qhua.ssm.po.Items" id="BaseResultMap">
        <id column="id" property="id" jdbcType="INTEGER" />
	    <result column="name" property="name" jdbcType="VARCHAR" />
	    <result column="price" property="price" jdbcType="REAL" />
	    <result column="pic" property="pic" jdbcType="VARCHAR" />
	    <result column="createtime" property="createtime" jdbcType="TIMESTAMP" />
   		<result column="detail" property="detail" jdbcType="LONGVARCHAR" />
	</resultMap>
    
    <!--查询所有用户  -->
    <select id="getAllItems" resultMap="BaseResultMap">
        select *from items
    </select>
    
    
</mapper>

14.com/qhua/ssm/po/Items.java

package com.qhua.ssm.po;

import java.util.Date;

public class Items {
    private Integer id;

    private String name;

    private Float price;

    private String pic;

    private Date createtime;

    private String detail;

	public Integer getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public Float getPrice() {
		return price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

	public String getPic() {
		return pic;
	}

	public void setPic(String pic) {
		this.pic = pic;
	}

	public Date getCreatetime() {
		return createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	@Override
	public String toString() {
		return "Items [id=" + id + ", name=" + name + ", price=" + price + ", pic=" + pic + ", createtime=" + createtime
				+ ", detail=" + detail + "]";
	}

}

按照这样便能顺利完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值