SSM搭建1

这里是SSM框架配置文件中内容的介绍。

连接数据库这里使用的是BasicDataSource。


1.需要在项目中导入jar包

Springmvc

Mybatis

Spring

2.配置文件

SpringMVC使用的配置文件是springmvc.xml;

MyBatis使用的配置文件是sqlMapConfig.xml;

Spring使用的配置文件是applicationContext.xml。

另外需要在项目的web.xml中进行配置。

(1)springmvc.xml

扫描controller和service,三大组件的扫描,视图解析器(前缀后缀)

<?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: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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- controller层,service层的扫描,包括子包 -->
<context:component-scan base-package="cn.bishe">
</context:component-scan>
<!-- 三大组件注解驱动配置 -->
<mvc:annotation-driven></mvc:annotation-driven> 
<!-- 视图解析器前缀和后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/jsp/"></property>
	<property name="suffix" value=".jsp"></property>
</bean>
</beans>

(2)sqlMapConfig.xml

pojo类的别名

<?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>
	<!-- 设置别名,避免映射配置文件中类要写全名的情况 -->
	<typeAliases>
		<package name="cn.bishe.pojo"/>
	</typeAliases>
</configuration>

(3)applicationContext.xml

数据库连接,Mybatis的工厂sqlSessionFactory,加载sqlMapConfig.xml文件,扫描Mapper,事务管理器transactionManager,事务扫描。

连接数据库这里使用的是BasicDataSource

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
	<!-- c3p0连接池 -->
	<context:property-placeholder location="classpath:db.properties"/>
	<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="url" value="${jdbc.url}"></property>
		<property name="driverClassName" value="${jdbc.driverClassName}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	<!-- 配置Mybatis的工厂 -->
	<bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
	</bean>
	<!-- 直接扫描Mapper接口 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		 <property name="basePackage" value="cn.bishe.dao"></property>
	</bean>
	<!-- 配置Mybatis的工厂 -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 事务管理的注解开启 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

db.properties数据库连接的文件:

jdbc.url=jdbc:mysql:///bishe
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=mysql

(4)web.xml

监听spring,springmvc前端控制器的配置,字符编码

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <!-- 配置一启动服务器就监听spring创建的监听器及初始化参数 -->
 <listener>
 	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <context-param>
 	<param-name>contextConfigLocation</param-name>
 	<param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 <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:springmvc.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
 <filter>
 	<filter-name>encoding</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>encoding</filter-name>
  	<url-pattern>*.action</url-pattern>
  </filter-mapping>
  
  <welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.action</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>
注意:编码的配置放在springmvc配置的前面。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值