1.逆向工程搭建步骤
<1>修改 db.properties中的账号密码以及数据库的链接方式<2>修改 generatorConfig.xml 中的账号密码
<3>运行 GeneratorSqlmap.java中的main方法
refresh (F5) 刷新
利用逆向工程生成pojo和mapper
2.SSM框架搭建
2 导包 导入pojo和mapper
3 配置web.xml
web.xml配置步骤
<!--1.放在src目录的com.hope.config包下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/hope/config/application*.xml</param-value>
</context-param>
<!-- 配置Spring核心监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等)
如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml)
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/hope/config/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析
第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析
使用此种方式可以实现 RESTful风格的url
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- post乱码过滤器 -->
<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>
4.配置db.properties
db.properties配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student
jdbc.username=root
jdbc.password=root
5 配置 SqlMapConfig
<?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="com.hope.pojo"/>
</typeAliases>
6 配置springmvc.xml
<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 ">
<!-- 对于注解的Handler可以单个配置,实际开发中建议使用组件扫描-->
<!-- 可以扫描controller、service、...这里让扫描controller,指定controller的包 -->
<context:component-scan base-package="com.hope.controller"></context:component-scan>
<!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了,
如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
实际开发时使用mvc:annotation-driven
-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器:解析jsp,默认使用jstl标签,classpath下的得有jstl的包 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路径的前缀 -->
<property name="prefix" value="/WEB-INF/"/>
<!-- 配置jsp路径的后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置拦截器 -->
<mvc:interceptors>
<!-- 多个拦截器顺序 -->
<mvc:interceptor>
<!-- /**/:表示所有url包括子url路径 -->
<mvc:mapping path="/**/" />
<!-- 定义的拦截器1 -->
<bean class="com.hope.util.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
7 配置applicationContext.xml
<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:com/hope/config/db.properties"/>
<!-- 数据源,使用dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- sqlSessinFactory,在mybatis-spring-1.2.3.jar包下-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="classpath:com/hope/config/SqlMapConfig.xml" />
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- mapper扫描,从mapper包中扫描mapper接口,自动创建代理对象并且在spring容器注入
遵循规范:mapper接口类名和mapper.xml映射文件名称保持一致,且在一个目录中。
自动扫描出来的mapper的bean的id为类名(首字母必须小写) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定扫描的包名,多个包,每个包中间使用半角逗号隔开 -->
<property name="basePackage" value="com.hope.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 配置登录的service -->
<bean id="loginService" class="com.hope.service.Impl.LoginServiceImpl"></bean>
<!-- 事务管理器 :对mybatis操作数据库事务控制,spring使用jdbc的事务控制类-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务传播特性 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!--那些包下的那些类参与了事务 -->
<aop:config>
<aop:advisor advice-ref="advice" pointcut="execution(* com.hope.service.Impl.*.*(..))"/>
</aop:config>
</beans>
8. 导入util工具类
9.创建service,serviceImpl 创建控制器
注意点serviceImpl中 实例化mapper 注解 @Autowired
控制器中类 上打@Controller 实例化@Autowired 在控制器中实例化需要在spring中进行区别
在方法上 注解 @RequestMapping("/login.action")