二、SSM的整合(Dao、Mybatis、Service、MVC、ApplicationContext)
一、整合总体结构

二、ApplicationContext(应用上下文)
连接各(Dao、Mybatis、Service、MVC)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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="classpath:spring-dao.xml"></import> <import resource="classpath:spring-service.xml"></import> <import resource="spring-mvc.xml"></import> </beans>
三、Dao(数据库层)
连接数据库的配置(database)
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false jdbc.username=root jdbc.password=123456
spring-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:aop="http://www.springframework.org/schema/context" 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"> <!-- 1、 关联数据库配置文件database--> <context:property-placeholder location="classpath:database.properties"></context:property-placeholder> <!-- 2、 建立连接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 3、关联sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 绑定Mybatis的配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- 4、配置dao接口扫描包,动态实现Dao接口可以注入到spring容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <!-- 扫描包并注入--> <property name="basePackage" value="dao"></property> </bean> </beans>
四、Mybatis配置
<?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> <!-- 配置数据库资源,交给spring去做--> <!--实体类绑定--> <typeAliases> <package name="pojo"/> </typeAliases> <!-- 绑定sql语句映射文件--> <mappers> <mapper class="dao.BooksMapper"></mapper> <mapper class="dao.UserBooks"></mapper> <mapper class="dao.MarketMapper"></mapper> </mappers> </configuration>
五、Service(服务层)
<?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"> <!-- 1、 扫描service的包--> <context:component-scan base-package="service"></context:component-scan> <!-- 2、将我们的所有业务类,注入到spring,可以通过配置,或者注解实现--> <bean id="BooksServiceImpl" class="service.BooksServiceImpl"> <property name="booksMapper" ref="booksMapper"></property> </bean> <bean id="UserService" class="service.UserServiceImpl"> <property name="userBooks" ref="userBooks"></property> </bean> <!-- MatketServiceImpl控制层的@Qualifier的bean引用变量 marketMapper服务层的dao引用的对象--> <bean id="MarketService" class="service.MatketServiceImpl"> <property name="marketMapper" ref="marketMapper"></property> </bean> <!-- 3、声明式事务配置--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 4、aop事务支持--> </beans>
六、MVC(JavaMVC的配置)
<?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"> <!-- 1、 扫描service的包--> <context:component-scan base-package="service"></context:component-scan> <!-- 2、将我们的所有业务类,注入到spring,可以通过配置,或者注解实现--> <bean id="BooksServiceImpl" class="service.BooksServiceImpl"> <property name="booksMapper" ref="booksMapper"></property> </bean> <bean id="UserService" class="service.UserServiceImpl"> <property name="userBooks" ref="userBooks"></property> </bean> <!-- MatketServiceImpl控制层的@Qualifier的bean引用变量 marketMapper服务层的dao引用的对象--> <bean id="MarketService" class="service.MatketServiceImpl"> <property name="marketMapper" ref="marketMapper"></property> </bean> <!-- 3、声明式事务配置--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 4、aop事务支持--> </beans>
浙公网安备 33010602011771号