基於ssm三大框架實現登入註冊功能的配置過程
第一步 ssm jar包整合,本人的下載資源裡面有整合的jar包
主要有:ssm整合jar包 ,jstl,資料庫連線,josn junit,等jar包
第二步,建立各類包和配置檔案,儘量把各個配置檔案分開,統一放在一個自己建立的config資料夾中,容易區分,後面也好檢查更改
主要配置,mybatis,spring—mybatis,和springmvc,以及web.xml和jdbc(資料庫連線資訊)
jdbc配置檔案
jdbc.DriverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/hello
jdbc.username=root
jdbc.password=518189
mybatis配置檔案
說明:如何單獨使用mybatis時,mybatis配置檔案中需要配置環境,也就是標籤<environment>裡面的資訊
ssm中這些事,由spring來做,在mybatis中只需要類的別名,用法如下
<typeAliases> 設定這個包下面的所有類的別名 預設是類名小寫,也就是這個包裡面所有的類就可以使用類名的小寫代替自己了 <package name="cn.itsource.domain"/> 設定單個類的別名 alias:取的別名 type:這個別名所對應的Java類 別名使用的時候與大小寫無關<typeAlias alias="Product" type="cn.itsource.domain.Product"/> </typeAliases> 總結;
①typeAlias:為某個java類起名②package:為某個包下所有類批量起別名(常用)
③@Alias:批量起別名的情況下使用,為某個型別指定新的別名(避免別名重複)
配置檔案
<?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>
<!-- 注意事項:單獨使用mybatis的時候,需要配置環境(資料來源和事務管理器)
但是當mybatis和spring進行整合的時候,交給spring容器來進行管理
-->
<typeAliases>
<package name="cn.edu360.ssm.model"/>
</typeAliases>
</configuration>
spring—mybatis配置檔案,裡面包含了spring和mybatis的整合配置
我們知道單獨使用mybatis時,需要底下這樣來,來建立 SqlSessionFactoryBuilder(),實現一個會話,呼叫mapper操作資料庫,現在通過spring的控制反轉,讓spring來做,但是需要配置。
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sessionFactory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
mapper.deleteStudent("1007");
session.commit();//提交
spring—mybatis配置檔案
<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 ">
<!-- 載入配置檔案,載入資料的連線資訊 -->
<context:property-placeholder location="classpath:jdbc/jdbc.properties" />
<!-- 資料來源,使用dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.DriverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- sqlSessinFactory在mybatis單獨使用的時候
咱們封裝了一個工具類來獲取該單例,但是和spring整合之後
完全交給spring的ioc容器來例項化該物件
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 載入mybatis的配置檔案 -->
<property name="configLocation" value="classpath:mybatis/mybatis.xml" />
<!-- 資料來源 的資訊-->
<property name="dataSource" ref="dataSource"/>
<!-- 獲取所有,mapper的資訊 注意後面的萬用字元-->
<property name="mapperLocations" value="classpath:cn/edu/ssm/mapper/*.xml"></property>
</bean>
<!-- spring和mybatis整合的jar包,提供了
自動生成mapper代理物件的機制,
只需要配置mapper介面的包路徑
和sqlsessionfactory
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.edu.ssm.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
springmvc配置檔案
由於springmvc就屬於spring所以不需要整合,之前怎麼配置,現在還怎麼配置
<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">
<!-- 註解的對映器和介面卡 -->
<mvc:annotation-driven/>
<!-- 附件掃描器 會掃描指定包下面的controller、service、repository等註解
springIoc容器會幫助咱們建立和依賴注入po,注意這個包的範圍儘量大點不能只具體到controller層。
-->
<context:component-scan base-package="cn.edu.ssm"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路徑的字首 -->
<property name="prefix" value="/WEB-INF/pages/" />
<!-- 配置jsp路徑的字尾 -->
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources location="/html/" mapping="/html/**"/>
</beans>
web.xml
web.xml需要把spring的配置檔案和springmvc的配置檔案載入進來。
<?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_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 配置上下文引數,指定spring配置檔案的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/spring-mybatis.xml
</param-value>
</context-param>
<!-- 配置spring監聽器 ,載入上下文引數,監聽springIOC容器的建立工作 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Springmvc的核心控制器 -->
<servlet>
<servlet-name>dispatchServlet</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>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- <servlet>
<servlet-name>addPic</servlet-name>
<servlet-class>/WEB-INF/pages/enterpriseManage/addPic.jsp</servlet-class>
</servlet> -->
<servlet-mapping>
<servlet-name>dispatchServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
以上就是ssm配置檔案的全過程,由於原始碼太多就不在這一一添加了,只要配置檔案會配置,註解會使用,其實ssm很簡單的。