在Spring文件中对数据库的配置以及结合mybatis的配置,还有事务的配置(配置了注解)
配置的代码如下所示:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 加载文件 -->
<context:property-placeholder location="classpath:basic/database.properties"/>
<!-- 配置数据库 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${driver}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${name}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
</bean>
<!-- 开启注解模式 -->
<context:annotation-config/>
<!-- 自动扫描基本包 -->
<context:component-scan base-package="com.music.dao*,com.music.service*"/>
<!-- 添加事务控制 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- spring结合mybatis的配置,在mybatis中使用的是sqlSessionFactory,因此spring利用sqlsessionFactoryBean来管理mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- mybatis的实体映射文件路径 -->
<property name="mapperLocations" value="classpath:mybatis/*/*.xml"/>
<!-- <property name="mapperLocations" value="classpath:mybatis/manager/manager.xml"/> -->
</bean>
<!-- sqlSession是通过sqlsessionTemplate来实现的 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0">
<ref bean="sqlSessionFactory"/>
</constructor-arg>
</bean>
<!-- 事务 需要三件事才可以 一具体的事务实现着 二事务管理器 三 aop 事务控制 不起作用-->
<!-- 这里使用 全注释 事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
SpringMVC的配置:
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
default-autowire="byName">
<!-- springmvc开启扫描模式 -->
<tx:annotation-driven/>
<!-- springmvc扫描的包 -->
<context:component-scan base-package="com.music.action"/>
<!-- 对文件进行操作 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000"/>
<property name="maxInMemorySize" value="10240" />
</bean>
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/manager/**"/>
<mvc:mapping path="/*/*.action"/>
<mvc:mapping path="/*.action"/>
<!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 -->
<mvc:exclude-mapping path="/managerInfo/managerLogin.action"/><!--该请求将不会被拦截 -->
<bean class="com.music.utils.manager.InterceptLogin"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
Mybatis的配置如下:
<?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="manager.info">
<!-- 管理员登陆查询 -->
<select id="queryManager" parameterType="com.music.dto.manager.ManagerDTO" resultType="java.lang.Integer">
select count(*) from manager where managername=#{managerName} and password=#{password}
</select>
<!-- 查询超级管理员 -->
<select id="queryRoot" parameterType="java.lang.String" resultType="java.lang.Integer">
select root from manager where managername=#{managerName}
</select>
<!-- 查询所有的用户的基本信息,包括用户的管理权限 -->
<select id="userQuery" parameterType="com.music.dto.manager.PageQuery" resultType="com.music.dto.manager.UserBasicInfo">
select*from users limit #{startNum},#{endNum}
</select>
</mapper>
最后是web.xml的配置了:
<?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>
<!-- spring容器的加载路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/spring.xml,classpath:config/springmvc.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:config/springmvc.xml</param-value>
</init-param>
<!-- 设置启动顺序为1 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 过滤器就是请求到达web资源之前 获取请求信息并进行处理,之后继续向后执行 -->
<!-- 字符编码过滤器 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>user/jsp/login.jsp</welcome-file>
</welcome-file-list>
</web-app>
这样SSM框架的配置基本就结束了,剩下的就是敲代码了。