2018用IDEA搭建SSM框架(Spring+SpringMVC+Mybatis) (2)

mybatis.cfg.xml <?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"> <!--MyBatis主配置文件--> <configuration> <!--为java po类起类别名--> <typeAliases> <package/> </typeAliases> </configuration> 添加Spring配置文件

在recources文件下新建spring文件夹,并且在其下新建下文件

applicationContext-dao.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" xsi:schemaLocation="http://www.springframework.org/schema/beans "> <!--加载数据库配置文件--> <context:property-placeholder location="classpath:db.properties"/> <!--配置数据源c3p0连接池--> <bean> <property value="${jdbc.driver}"/> <property value="${jdbc.url}"/> <property value="${jdbc.username}"/> <property value="${jdbc.password}"/> </bean> <!--配置SqlSessionFactory--> <bean> <!--加载mybatis配置文件--> <property value="classpath:mybatis/mybatis.cfg.xml"/> <!--数据源--> <property ref="dataSource"/> </bean> <!--Mapper批量扫描,从Mapper包扫描接口,自动创建代理对象,并在Spring容器中自动注册 使用 Mybatis与Spring整合包的这个 Mapper 扫描器后, Mybatis 配置文件里的扫描器,就可以取消掉了 遵循的规范 不变 自动扫描出来的Mapper的bean的id为Mapper类名(首字母小写) --> <bean> <!--如果需要扫描多个报下的mapper,每个包中间使用半角逗号分开--> <property value="com.flyme.mapper"/> <property value="sessionFactory"/> </bean> </beans> 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" xsi:schemaLocation="http://www.springframework.org/schema/beans "> <!--组件扫描,如果想要类被组件扫描,扫描到,并在Spring容器中注册的话 必须在类名上添加上注解 @Repository、@Service、@Controller、@Component (这四个注解功能一样,名字不同只是为了区分不同功能) @Component 是通用组件 --> <context:component-scan base-package="com.flyme.service.impl"></context:component-scan> </beans> applicationContext-trsaction.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans "> <bean> <!--配置数据源--> <property ref="dataSource"></property> </bean> <!--开启注解(通知)--> <tx:advice transaction-manager="transactionManager"> <tx:attributes> <!--传播行为--> <tx:method propagation="REQUIRED"/> <tx:method propagation="REQUIRED"/> <tx:method propagation="REQUIRED"/> <tx:method propagation="REQUIRED"/> <tx:method propagation="SUPPORTS" read-only="true"/> <tx:method propagation="SUPPORTS" read-only="true"/> <tx:method propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--AOP--> <aop:config> <!--设置切入点--> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.flyme.service.impl.*.*(..))"/> </aop:config> </beans> 添加springmvc配置文件

在resources/spring文件下,新建springmvc.xml文件

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/mvc/spring-mvc.xsd"> <!--加载静态资源--> <mvc:default-servlet-handler/> <!--1.组件扫描,可以扫描 controller、Service、... 并注册添加到 spring 容器中 这里扫描 controller,指定controller的包 --> <context:component-scan base-package="com.flyme.controller"/> <!--开启注解注解驱动--> <mvc:annotation-driven/> <!--视图解析器--> <bean> <property value="/WEB-INF/jsp/"/> <property value=".jsp"/> </bean> </beans> 重新编写web.xml文件

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgwps.html