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

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

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

項目下載





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章