1.创建Maven工程
- 创建ssm_parent父工程(打包方式选择pom,必需的)
- 创建ssm_web子模块(打包方式war包)
- 创建ssm_service子模块(打包方式jar包)
- 创建ssm_dao子模块(打包方式jar包)
- 创建ssm_pojo子模块(打包方式jar包)
- 创建ssm_utils子模块(打包方式jar包)
2.创建表和实体类
3.Dao层的编写
- dao层接口的编写(根据具体操作编写,这里我省略了代码)
- 连接数据库的jdbc.properties文件的编写(如下)
- Spring和MyBatis的整合----->添加配置文件applicationContext-dao.xml(如下)
#数据库类型
jdbc.driver=com.mysql.jdbc.Driver
#数据库要连接的数据库名
#jdbc.url=jdbc:mysql://localhost:3306/数据库名
jdbc.url=jdbc:mysql://localhost:3306/ssm
#数据库用户名
jdbc.user=root
#数据库密码
jdbc.password=123456
<?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"
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">
<!--引入jdbc属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--创建数据源对象-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--SqlSessionFactoryBean对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源对象-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--扫描dao包,创建dao、接口的动态代理对象:MapperScannerConfigurer-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定dao层的包路径-->
<property name="basePackage" value="cn.zf.dao"></property>
</bean>
</beans>
4.Service层的编写
- service层接口的编写(省略代码啦)
- 实现类的编写(省略代码啦)
- Spring的配置文件applicationContext-service.xml(如下)
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--引入外部文件(因为要使用applicationContext-dao.xml配置文件中的DataSource)-->
<import resource="classpath*:spring/applicationContext-dao.xml"></import>
<!--开启注解扫描包,只扫描service注解-->
<context:component-scan base-package="cn.zf.service"></context:component-scan>
<!--事务管理器对象-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--事务通知对象-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="query*" read-only="true" propagation="SUPPORTS"/>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
<tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--aop切面配置-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.zf.service.impl.*.*(..))"></aop:advisor>
</aop:config>
</beans>
5.controller层的编写
- controller层代码的编写(省略代码啦)
- springmvc.xml配置文件的编写(如下)
<?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"
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">
<!--开启注解扫描,只扫描Controller注解-->
<context:component-scan base-package="cn.zf.controller"></context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--开启MVC注解-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--静态资源全部放行-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
6.web.xml的编写
<?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_2_5.xsd" version="2.5">
<display-name>ssm_pages</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</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>
<display-name>Archetype Created Web Application</display-name>
<!--配置一个全局的参数 指定spring容器的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
</context-param>
<!--解决中文乱码的过滤器-->
<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>
<!--配置监听器 创建spring对象-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc.xml配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--启动服务器,创建该servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
下面是各个配置文件的位置:
注:web层和service层,service层和dao层要创建依赖关系。web依赖于service,service依赖于dao,dao依赖于pojo
web层的pom.xml的依赖配置:
service层的pom.xml的依赖配置:
dao层的pom.xml的依赖配置: