Eclipse中创建SSM框架项⽬步骤先看⼀下总体的⽬录结构
1.导⼊SSM框架所需的jar包(免费的哦~)
2.编写pojo层,mapper层,service层,controller层(省略..............)
3.编写配置⽂件
这个⽂件主要是对数据库连接,SqlSessionFactory,mapper接⼝,事务的相关配置
是mybatis和spring的组合(包括了原来在mybatis中要配置的l⽂件)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:aop="/schema/aop"
xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/aop
/schema/aop/spring-aop.xsd
/schema/tx
/schema/tx/spring-tx.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.lxw"/>
<!--加载属性⽂件 -->
<context:property-placeholder location="classpath:database.properties" />
<!--配置连接池-->
<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.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置SqlSessionFactory⼯⼚-->
<bean id="sqlSessionFactory" class="batis.spring.SqlSessionFactoryBean">
<!--加载数据库资源  -->
<property name="dataSource" ref="dataSource"></property>
<!--别名映射  -->
<property name="typeAliasesPackage" value="com.lxw.pojo"></property>
</bean>
<!--配置mapper接⼝-->
<bean id="mapperScannerConfigurer" class="batis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lxw.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!--配置spring声明式事务管理-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find" read-only="true"/>
<tx:method name="*" isolation="DEFAULT"></tx:method>
</tx:attributes>
</tx:advice>
<!--配置AOP增强-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.lxw.serviceImpl.*ServiceImpl.*(..))"></aop:advisor>    </aop:config>
</beans>
database.properties⽂件
数据库连接(根据需要修改数据库名和密码)
jdbc.sql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/book
jdbc.username=root
jdbc.password=root
这个配置⽂件主要是针对Controller层的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:mvc="/schema/mvc"
xmlns:context="/schema/context"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="
/schema/beans
/schema/beans/spring-beans.xsd
/schema/mvc
/schema/mvc/spring-mvc.xsd
/schema/context
/schema/context/spring-context.xsd">
<!--扫描注解包-->
<context:component-scan base-package="ller"></context:component-scan>
<!--开启注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--过滤静态资源-->
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
</beans>
这是⼀个主要的⽂件⽤来把之前两个配置⽂件加载到类中,并配置了,前端控制器等
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="java.sun/xml/ns/javaee"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="java.sun/xml/ns/javaee
java.sun/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Archetype Created Web Application</display-name>
<!--配置spring,默认加载WEB-INF⽬录下的l⽂件-->
<listener>
<listener-class>org.t.ContextLoaderListener</listener-class>  </listener>
<!--设置配置⽂件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</param-value>
</context-param>
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载l配置⽂件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</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>
<!--配置过滤器解决中⽂乱码-->
<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>
怎么把项目导入到eclipse</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
到此SSM框架配置完成开始编写吧~

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。