欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Spring与Mybatis整合方法(基于IDEA中的Maven整合)

程序员文章站 2022-03-11 14:45:31
项目结构项目路径可以自己定义,只要路径映射正确就可以pom.xml 5.1.5.release

项目结构

详解Spring与Mybatis整合方法(基于IDEA中的Maven整合)

项目路径可以自己定义,只要路径映射正确就可以

pom.xml

 <properties>
 <spring.version>5.1.5.release</spring.version>
 <mybatis.version>3.4.6</mybatis.version>
 <log4j.version>1.2.17</log4j.version>
 </properties>
 <dependencies>
 <!-- 测试包 -->
 <dependency>
  <groupid>junit</groupid>
  <artifactid>junit</artifactid>
  <version>4.11</version>
  <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
  <scope>test</scope>
 </dependency>
 <dependency>
  <groupid>org.projectlombok</groupid>
  <artifactid>lombok</artifactid>
  <version>1.18.14</version>
  <scope>provided</scope>
 </dependency>
 <!-- spring核心包 -->
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-core</artifactid>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-web</artifactid>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-oxm</artifactid>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-tx</artifactid>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-jdbc</artifactid>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-webmvc</artifactid>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-aop</artifactid>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-context</artifactid>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-test</artifactid>
  <version>${spring.version}</version>
 </dependency>
 <!-- mybatis核心包 -->
 <dependency>
  <groupid>org.mybatis</groupid>
  <artifactid>mybatis</artifactid>
  <version>${mybatis.version}</version>
 </dependency>
 <!-- mybatis/spring包 -->
 <dependency>
  <groupid>org.mybatis</groupid>
  <artifactid>mybatis-spring</artifactid>
  <version>1.3.2</version>
 </dependency>
 <!-- 导入mysql数据库链接jar包 -->
 <dependency>
  <groupid>mysql</groupid>
  <artifactid>mysql-connector-java</artifactid>
  <version>5.1.17</version>
 </dependency>
 <dependency>
  <groupid>com.mchange</groupid>
  <artifactid>c3p0</artifactid>
  <version>0.9.5.5</version>
 </dependency>

 <dependency>
  <groupid>jstl</groupid>
  <artifactid>jstl</artifactid>
  <version>1.2</version>
 </dependency>
 <dependency>
  <groupid>org.apache.commons</groupid>
  <artifactid>commons-dbcp2</artifactid>
  <version>2.7.0</version>
 </dependency>
 <dependency>
  <groupid>commons-io</groupid>
  <artifactid>commons-io</artifactid>
  <version>2.4</version>
 </dependency>
 <dependency>
  <groupid>commons-logging</groupid>
  <artifactid>commons-logging</artifactid>
  <version>1.2</version>
 </dependency>
 <dependency>
  <groupid>org.junit.jupiter</groupid>
  <artifactid>junit-jupiter</artifactid>
  <version>release</version>
  <scope>compile</scope>
 </dependency>
 </dependencies>

emp.java,empmapper.java, empmapper.xml

https://www.jb51.net/article/197594.htm

db.properties

# mysql
jdbc.url=jdbc:mysql://localhost:3306/jektong?useunicode=true&characterencoding=utf-8
jdbc.driver=com.mysql.jdbc.driver
jdbc.username=jektong
jdbc.password=123456

这里的key值不能写url,driver,username,password,不然后面使用${url},${username}会报错

applicationcontext.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:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:util="http://www.springframework.org/schema/util"

 xsi:schemalocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
 http://www.springframework.org/schema/jdbc
 http://www.springframework.org/schema/jdbc ">
 

	<!-- 加载配置文件并配置数据库连接池 -->
 <context:property-placeholder location="classpath:db.properties"/>

	<!--这里不要使用与name相同的属性名比如写成url-->
 <bean id="datasource" class="org.apache.commons.dbcp2.basicdatasource">
 <property name="url" value="${jdbc.url}"/>
 <property name="password" value="${jdbc.password}"/>
 <property name="username" value="${jdbc.username}"/>
 <property name="driverclassname" value="${jdbc.driver}"/>
 </bean>
 
 <!-- sqlsessionfactorybean --> 
	 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
		<!-- 引用数据源的组件 -->
 	 <property name="datasource" ref="datasource"/> 
 	<!-- 映射文件指定 -->
 	<property name="mapperlocations" value="classpath*:mapper/*.xml"/>
 	<!-- 类型别名 -->
 	<property name="typealiasespackage" value="com.jektong"/>
	</bean> 
	
	<!-- sqlsessionfactory已经过时,使用sqlsessionfactorybeanname,ref改为value -->
	<bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
 <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"/>
 <property name="basepackage" value="com.jektong.dao"/>
	</bean>
</beans>

到此这篇关于详解spring与mybatis整合方法(基于idea中的maven整合)的文章就介绍到这了,更多相关spring与mybatis整合内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!