本日的工作主要是对ssm框架下的用户管理系统项目和代码结构以及配置的熟悉,附带开发登录功能。接下来主要针对主要配置文件进行解析。
Spring和MyBatis的配置文件applicationContext.xml
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 2.配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描bean包 使用别名 -->
<property name="typeAliasesPackage" value="com.maxing.bean"></property>
<!--配置加载映射文件 UserMapper.xml-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 自动生成dao,mapper-->
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.maxing.dao"/>
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--自动扫描-->
<context:component-scan base-package="com.maxing"/>
<!-- 配置事务-->
<!-- 5.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 6.开启事务注解-->
<tx:annotation-driven></tx:annotation-driven>
</beans>
注:首先是加载并配置数据源,然后配置SQL会话工厂(SQLSession封装JDBC连接,通过其执行被映射的SQL语句) ,最后是声明和注解事务。
web项目的配置文件:web.xml
<!-- 配置加载类路径的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- 解决中文乱码过滤器 -->
<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>
<!-- 前端控制器(加载classpath:spring-mvc.xml 服务器启动创建servlet) -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
spring-mvc.xml
<!-- 1.注解扫描位置-->
<context:component-scan base-package="com.maxing.controller" />
<!-- 2.配置映射处理和适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 3.视图的解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
接着我们来关注一个页面的请求是如何被响应和执行的。首先是SpringMVC,它在项目中拦截用户请求,其中DispatcherSerlvet负责拿着用户请求取匹配Controller控制层,Controller里我们编写了具体要执行的操作,以do作为后缀,这个在上面的web.xml中已有配置。来看一个具体的login函数:
@RequestMapping("/login.do")
public String login(userinfo user) {
boolean tag = userInfoService.login(user);
if(tag) {
return "main";
}
else {
return "../failer";
}
}
这个login函数里当然也不可能直接查询数据库,我们通过service业务层的login函数来实现,而业务层的login函数是这样的。
@Override
public boolean login(userinfo user) {
userinfo userinfo = userInfoDao.login(user);
if(userinfo != null)
return true;
else
return false;
}
终于,我们看到了持久层userInfoDao,Dao层的每一个函数都有其在对应mapper.xml中映射的sql语句。简单来说就是:应用层让持久层去查找,持久层则通过映射的sql语句执行查找(附该函数的映射语句)。
<select id="login" resultType="com.mx.bean.userinfo">
select * from userinfo
<where>
<if test="username != ''">
and username = #{username}
</if>
<if test="password != ''">
and password = #{password}
</if>
</where>
</select>
这里的where是动态SQL,不仅充当sql的"where”,同时解决and前缀问题。