三大框架ssm(Spring+SpringMVC+Mybatis)的基础整合

5 篇文章 0 订阅
4 篇文章 0 订阅

主要是各个配置文件的配置

4.1log4j.properties日记配置文件

4.2jdbc.properties属性配置文件

4.3Mybatis核心配置文件

4.4SpringMVC需要的配置文件

4.5Spring需要的配置文件 application.xml

4.6、逆向工程配置文件BookMapper.xml 


1、测试数据库


drop database if exists ssm;

create database ssm;

use ssm; 

##创建图书表
create table t_book(
	`id` int(11) primary key auto_increment, 	## 主键
	`name` varchar(50) not null,				## 书名 
	`author` varchar(50) not null,				## 作者
	`price` decimal(11,2) not null,				## 价格
	`sales` int(11) not null,					## 销量
	`stock` int(11)								## 库存
);


## 插入初始化测试数据
insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , 'java从入门到放弃' , '国哥' , 80 , 9999 , 9 );

insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '数据结构与算法' , '严敏君' , 78.5 , 6 , 13 );

insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '怎样拐跑别人的媳妇' , '龙伍' , 68, 99999 , 52 );

insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '木虚肉盖饭' , '小胖' , 16, 1000 , 50 );

insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , 'C++编程思想' , '刚哥' , 45.5 , 14 , 95 );

insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '蛋炒饭' , '周星星' , 9.9, 12 , 53 );
 
insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '赌神' , '龙伍' , 66.5, 125 , 535 );

insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , 'Java编程思想' , '阳哥' , 99.5 , 47 , 36 );

insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , 'JavaScript从入门到精通' , '婷姐' , 9.9 , 85 , 95 );

insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , 'cocos2d-x游戏编程入门' , '国哥' , 49, 52 , 62 );

insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , 'C语言程序设计' , '谭浩强' , 28 , 52 , 74 );

insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , 'Lua语言程序设计' , '雷丰阳' , 51.5 , 48 , 82 );

insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '西游记' , '罗贯中' , 12, 19 , 9999 );

insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '水浒传' , '华仔' , 33.05 , 22 , 88 );
 
insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '操作系统原理' , '刘优' , 133.05 , 122 , 188 );
 
insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '数据结构 java版' , '封大神' , 173.15 , 21 , 81 );
 
insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , 'UNIX高级环境编程' , '乐天' , 99.15 , 210 , 810 );
 
insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , 'javaScript高级编程' , '国哥' , 69.15 , 210 , 810 );
 
insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '大话设计模式' , '国哥' , 89.15 , 20 , 10 );
 
insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`) 
values(null , '人月神话' , '刚哥' , 88.15 , 20 , 80 ); 


## 查看表内容
select id,name,author,price,sales,stock from t_book;

2、创建一个动态Web工程


3、导入整合Spring+SpringMVC+Mybatis的所有jar

 

Spring的核心包

spring-beans-4.0.0.RELEASE.jar

spring-context-4.0.0.RELEASE.jar

spring-core-4.0.0.RELEASE.jar

spring-expression-4.0.0.RELEASE.jar

commons-logging-1.1.3.jar

 

Spring切面包

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

spring-aop-4.0.0.RELEASE.jar

spring-aspects-4.0.0.RELEASE.jar

 

log4j日记包

log4j-1.2.17.jar

 

mysql驱动和数据库连接池包

c3p0-0.9.1.2.jar

mysql-connector-java-5.1.37-bin.jar

 

Spring的数据库及事务包

spring-jdbc-4.0.0.RELEASE.jar

spring-orm-4.0.0.RELEASE.jar

spring-tx-4.0.0.RELEASE.jar

 

SpringMVC的包

spring-web-4.0.0.RELEASE.jar

spring-webmvc-4.0.0.RELEASE.jar

 

SpringHiberante验证包

hibernate-validator-annotation-processor-5.0.0.CR2.jar

hibernate-validator-5.0.0.CR2.jar

validation-api-1.1.0.CR1.jar

jboss-logging-3.1.1.GA.jar

classmate-0.8.0.jar

 

文件上传包

commons-fileupload-1.2.1.jar

commons-io-1.4.jar

 

Spring中的Json处理包

jackson-annotations-2.1.5.jar

jackson-core-2.1.5.jar

jackson-databind-2.1.5.jar

 

MyBatis以及整合包

mybatis-3.4.1.jar

mybatis-spring-1.3.0.jar

 

JSTL标签库

taglibs-standard-impl-1.2.1.jar

taglibs-standard-spec-1.2.1.jar

4、各种配置文件

1) log4j.properties日记配置文件

2) jdbc.properties属性配置文件

3) jdbc.properties属性配置文件

4) SpringMVC需要的配置文件

5) Spring需要的配置文件

6) 逆向工程配置文件BookMapper.xml

4.1log4j.properties日记配置文件


# Global logging configuration
log4j.rootLogger=INFO, 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

4.2jdbc.properties属性配置文件 

jdbc.user=root
jdbc.password=123456
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.driver=com.mysql.jdbc.Driver

4.3Mybatis核心配置文件

<?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>
	<settings>
		<!-- 打开延迟加载的开关 -->
		<setting name="lazyLoadingEnabled" value="true" />
		<!-- 将积极加载改为消极加载 按需加载 -->
		<setting name="aggressiveLazyLoading" value="false" />
	</settings>
<!-- 引入sql语句对应的配置文件 可以再spring.xml中配置
	<mappers>
		<package name="com.tcent.dao"/>
	</mappers> -->
</configuration>

4.4SpringMVC需要的配置文件

application-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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

	<!-- 扫描springMVC的类和异常处理 -->
	<context:component-scan base-package="com.tcent" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	</context:component-scan>

	<!-- 视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/book/" />
		<property name="suffix" value=".jsp"/>
	</bean>

	<!-- SpringMVC标签的两个mvc标签 -->
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven/>
</beans>

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"
	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>ssm</display-name>
<!-- 将Spring整合到web中 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!--	解决乱码的Filter过滤器	 -->
	<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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 支持restful风格的Filter -->
	<filter>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>	
	</filter>
	<filter-mapping>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
		
	<!-- 整合Spring到Web的监听器	-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- SpringMVC的前端控制器 -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:application-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

4.5Spring需要的配置文件 application.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
	<!-- 扫描除SpringMVC之外所有组件 -->
	<context:component-scan base-package="com.tcent">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
		<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
	</context:component-scan>
	
	<!-- 加载jdbc.properties属性配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	
	<!-- 配置数据库连接池对象 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}" />
		<property name="password" value="${jdbc.password}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="driverClass" value="${jdbc.driver}" />
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- Mybatis整合Spring的核心配置之一 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<property name="mapperLocations" value="classpath:com/tcent/dao/*.xml" />
	</bean>
	<!-- Mybatis整合Spring的核心配置之二		老式的将Mapper接口注入到SpringIOC容器中
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.tcent.dao"></property>
	</bean>	 -->
	<!-- Mybatis整合Spring的核心配置之二	扫描并将Mapper接口注入到SpringIOC容器中 -->
	<mybatis-spring:scan base-package="com.tcent.dao" />

	<!-- 配置事务属性 -->
	<tx:advice id="tx_ssm" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- 配置对哪些对象进行代理,添加事务 -->
	<aop:config>
		<aop:advisor advice-ref="tx_ssm" pointcut="execution(* com.tcent.service.impl.*.*(..))"/>
	</aop:config>
</beans>

4.6、逆向工程配置文件BookMapper.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.tcent.dao.BookMapper" >
  <resultMap id="BaseResultMap" type="com.tcent.pojo.Book" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="author" property="author" jdbcType="VARCHAR" />
    <result column="price" property="price" jdbcType="DECIMAL" />
    <result column="sales" property="sales" jdbcType="INTEGER" />
    <result column="stock" property="stock" jdbcType="INTEGER" />
  </resultMap>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from t_book
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.tcent.pojo.Book" >
    insert into t_book (id, name, author, 
      price, sales, stock
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, 
      #{price,jdbcType=DECIMAL}, #{sales,jdbcType=INTEGER}, #{stock,jdbcType=INTEGER}
      )
  </insert>
  <update id="updateByPrimaryKey" parameterType="com.tcent.pojo.Book" >
    update t_book
    set name = #{name,jdbcType=VARCHAR},
      author = #{author,jdbcType=VARCHAR},
      price = #{price,jdbcType=DECIMAL},
      sales = #{sales,jdbcType=INTEGER},
      stock = #{stock,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select id, name, author, price, sales, stock
    from t_book
    where id = #{id,jdbcType=INTEGER}
  </select>
  <select id="selectAll" resultMap="BaseResultMap" >
    select id, name, author, price, sales, stock
    from t_book
  </select>
</mapper>

逆向工程详见: Mybatis-详解3

项目下载





  • 2
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 基于SSMSpring+SpringMVC+MyBatis)的管理系统是一种常见的Web应用程序,它使用Spring框架作为应用程序的核心,SpringMVC框架作为Web层的控制器,MyBatis框架作为数据访问层的ORM框架。这种管理系统可以用于各种企业级应用程序,例如人力资源管理系统、客户关系管理系统、库存管理系统等。它具有易于扩展、高效、安全等优点,是现代企业信息化建设的重要组成部分。 ### 回答2: SSM是指基于SpringSpringMVCMyBatis这三个框架技术实现的一种Web应用开发模式。在SSM框架中,Spring框架主要作为IoC容器和Bean工厂,提供依赖注入、事务管理、AOP等功能,SpringMVC框架主要负责Web层的控制器部分,处理HTTP请求和响应,而MyBatis框架则用于持久层的数据访问和管理,提供了高效且易于维护的数据库访问操作。 基于SSM框架的管理系统可以分为前台和后台两个系统。前台主要面向普通用户,提供浏览、查询、注册、登录等功能,让用户能够方便地使用系统。后台主要面向管理员或管理人员,提供对系统中各种业务数据的管理、修改、删除等功能,让管理人员能够对系统运行情况进行有效的监控和控制。 在基于SSM框架的管理系统中,需要进行技术选型、模块设计、业务实现等工作。首先,需要根据具体需求选择合适的技术方案和架构模式。其次,需要对各个模块进行梳理,按照功能划分,确定模块之间的关系和交互方式,最终实现整个系统的业务逻辑。同时,需要注意系统的可扩展性、可维护性和安全性,保证系统的数据安全,同时能够满足系统的不断发展和升级的需要。 总之,基于SSM框架的管理系统,可以通过合理运用SpringSpringMVCMyBatis等技术,实现系统中的各种业务逻辑。开发人员需要具备扎实的Java技术和Web开发经验,同时需要熟悉相关的数据库技术和网络协议,才能够顺利地完成系统的设计和开发。 ### 回答3: SSM是目前非常流行的一种技术架构,它是SpringSpringMVCMyBatis三个框架的结合,每个框架具有自己的优势和功能,通过整合,可以快速地搭建一个高效、可维护的管理系统。 在SSM框架下,可以将系统分为三层:表现层、业务层和持久层。表现层由SpringMVC处理用户请求和响应,业务层由Spring实现,而持久层则使用MyBatis实现数据库交互。 在搭建一个基于SSM的管理系统时,首先需要进行配置。Spring的配置包括Spring的核心配置文件、数据源的配置和事务的配置;SpringMVC的配置包括MVC配置文件、拦截器等;MyBatis的配置包括数据库连接池、Mapper文件和MyBatis的配置文件等。这些都需要进行详细的配置。 在系统开发中,可以通过Maven对项目进行管理,比如添加依赖、打包等。同时,也可以使用Spring Security对系统进行安全性的保护,实现权限控制等功能。 在具体的业务实现中,可以根据需要添加各种插件、拦截器和过滤器等,还可以使用Redis等缓存技术,提高系统的性能和效率。 总的来说,基于SSM的管理系统具有以下优点:首先,框架整合可以提高开发效率,减少重复代码。其次,各个框架都具有良好的扩展性和可维护性,方便对系统进行升级和调整。最后,使用Maven进行项目管理,可以更好地管理依赖,提高项目效率。 当然,也需要注意SSM框架的缺点,比如框架整合带来的额外配置和调试成本,以及MyBatis可能存在的一些瓶颈等问题。综上所述,基于SSM的管理系统适合中小型项目,能够提高开发效率,降低开发难度,实现快速迭代和维护,是一种非常实用的技术方案。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值