淘淘商城系列(二)—— SSM框架整合之Dao层(五)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lkp1603645756/article/details/84320366

整合思路

1.Dao层

mybatis整合spring,通过spring管理SqlSessionFactory、mapper代理对象。需要mybatis和spring的整合包,由spring创建数据库连接池。

整合内容

对应工程

Pojo

Taotao-manger-pojo

Mapper映射文件

Taotao-manger-dao

Mapper接口

Taotao-mangaer-dao

SqlMapConfig.xml(全局配置)

Taotao-manager-service

applicationContext-dao.xml(数据库连接池)

Taotao-manager-service

2.Service层:

所有的实现类都放到spring容器中管理。并有spring管理事务;发布dubbo服务

整合内容

对应工程

Service接口

Taotao-mangaer-interface

Service实现类

Taotao-mangaer-service

applicationContext-service.xml

Taotao-manager-service

applicationContext-trans.xml

Taotao-manager-service

web.xml 配置:配置加载spring容器

3.表现层:

Springmvc整合spring框架,由springmvc管理controller;引入dubbo服务

整合内容

对应工程

springmvc.xml

Taotao-manager-web

Controller

Taotao-manager-web

web.xml 的配置:前端控制器的配置,配置URL拦截形式。

 

Dao层整合

整合后的目录结构如下:后面的配置文件 可以参考此图片创建目录:

 

1、我们在taotao-manager-service工程的resources目录下面,创建一个文件夹,名字叫做mybatis

在mybatis文件夹下面创建SqlMapConfig.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">
<configuration>

</configuration>

2、Spring整合MyBatis

我们还是在该工程的resources目录下面,创建一个spring文件夹

在spring文件夹中创建applicationContext-dao.xml,并添加如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

	<!-- 数据库连接池 -->
	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:properties/*.properties" />
	<!-- 数据库连接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="maxActive" value="10" />
		<property name="minIdle" value="5" />
	</bean>
	<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.taotao.mapper" />
	</bean>
</beans>

3、创建数据库配置文件

在该工程的resources目录下面,创建一个properties文件夹

在properties文件夹中创建db.properties数据库配置文件,并导入如下配置:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

如果你的mysql数据库在本机,则不用修改连接地址,如果其他的地方,则需要将localhost替换为你数据库所在的地址。

用户名和密码按照你自己数据库的来填写。

都创建好之后是这样的

到这里我们的dao层就整合完毕了!

备注:

Druid是目前最好的数据库连接池,在功能、性能、扩展性方面,都超过其他数据库连接池,包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。

Druid已经在阿里巴巴部署了超过600个应用,经过多年多生产环境大规模部署的严苛考验。

 

 

如果有错误的地方,还请不吝赐教,非常感谢~


欢迎访问我的CSDN博客,让我们一同成长!

“不管做什么,只要坚持下去就会看到不一样!”

猜你喜欢

转载自blog.csdn.net/lkp1603645756/article/details/84320366