SpringMVC+Spring+Mybatis整合

第一步必不可少,添加相关的jar包

 
 
 

第二步:当然就连接数据库,所以我们先配置mybatis以及mybatis和spring的整合

 

1)创建一个源文件sources,然后再改文件下创建一个名为mybatis-config.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>
<!--   	配置全局属性 -->
  		<settings>
<!--   		使用jdbc的getGeneratedKeys获取数据库自增主键 -->
  		<setting name="useGeneratedKeys" value="true"/>
  		
<!--   		使用列别名替换列名  默认true
		select name as title from table
-->
		<setting name="useColumnLabel" value="true"/>
		
<!-- 		开启驼峰命名转化  table(create_time) -> entity(createTime)-->
		<setting name="mapUnderscoreToCamelCase" value="true"/>
  		</settings>

  
  </configuration>

 

2)接下来需要配置spring-dao层的整合,但是在这之前我们需要准备数据库,以及我们的连接参数的属性文件。

 
2.1这里只是为了演示我就创建一个简单的表,只包含用户名,密码,年龄(使用MySQL数据库中springmvc数据库以及user表)
mysql> create database springmvc;
Query OK, 1 row affected (0.00 sec)

mysql> use springmvc
Database changed
mysql> CREATE TABLE user(
    -> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    -> user_name VARCHAR(20) NOT NULL,
    -> password VARCHAR(15) NOT NULL,
    -> age TINYINT);
2.2接下来我们就需要一个属性文件用来保存我们的数据库连接驱动名以及用户名等参数,在resources目录下创建一个resource.properties文件(以键值对保存参数的文件),内容如下:
jdbc.name=你的数据库用户名
jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.pwd=你的数据库密码
jdbc.url=jdbc\:mysql\://localhost\:3306/springmvc?useUnicode\=true&characterEncoding\=utf8
2.3接下来我们就在resources/spring/下创建一个spring-dao.xml来整合mybatis和spring
<?xml version="1.0" encoding="UTF-8"?>  
<beans 
	xmlns="http://www.springframework.org/schema/beans" 
	xmlns:context="http://www.springframework.org/schema/context" 
	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" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xsi:schemaLocation="  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx.xsd  
    http://www.springframework.org/schema/jdbc  
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
    http://www.springframework.org/schema/cache  
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop.xsd  
    http://www.springframework.org/schema/util  
    http://www.springframework.org/schema/util/spring-util.xsd">  
<!--  	配置整合mybatis过程 -->
		
<!-- 		1配置数据库参数,引入刚才写的属性文件 -->
			<context:property-placeholder location="classpath:resource.properties"/>
<!-- 		2	数据库连接池 (c3p0)-->
			<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
			<property name="driverClass" value="${jdbc.driverclass}"></property>
	  		<property name="jdbcUrl" value="${jdbc.url}"></property>
	  		<property name="user" value="${jdbc.name}"></property>
	  		<property name="password" value="${jdbc.pwd}"></property>
	  		
<!-- 	  		c3p0私有属性 -->

			<property name="maxPoolSize" value="30"></property>
			<property name="minPoolSize" value="10"></property>
<!-- 			关闭后不自动提交 -->
			<property name="autoCommitOnClose" value="false"></property>
<!-- 			连接超时时间 -->
			<property name="checkoutTimeout" value="1000"></property>
<!-- 			连接失败重试次数 -->
			<property name="acquireRetryAttempts" value="2"></property>
			
			</bean>
			
<!-- 		3需要使用mybatis则这里需要配置sqlsessionFactory对象 -->
		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 				注入数据库连接池 
					在mybatis中是在configuration中配置的数据库连接,在这里mybatis值配置了mapper
					才能获取sqlSessionFactory对象
					所以需要为这个类注入数据库连接池
-->
					<property name="dataSource" ref="dataSource"></property>
<!-- 					mybatis全局配置文件 -->
					<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 						扫描entity包使用别名,不然前面配置的mapper直接使用的类名会出错 
 		多个包需要扫描	<property name="typeAliasesPackage" value="org.seckill.entity;org.seckill.entity2"/> -->
					<property name="typeAliasesPackage" value="org.seckill.entity"></property>
					
<!-- 					扫描sql配置文件也就是mapper里面的 -->

					<property name="mapperLocations" value="classpath:mapper/*.xml"></property>

			</bean>
			
<!-- 		4	配置扫描dao接口包,动态实现dao接口,自动注入到spring容器中 -->
			<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 				注入到sqlSessionFactory -->
				<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 				给出需要扫描的dao包 -->
				<property name="basePackage" value="org.seckill.dao"></property>
			</bean>
 </beans>

 
 
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值