Spring 과 Mybatis 통합 방법 에 대한 자세 한 설명(IDEA 의 Maven 통합 기반)

8956 단어 SpringMybatis통합
프로젝트 구조
在这里插入图片描述
프로젝트 경 로 는 스스로 정의 할 수 있 습 니 다.경로 맵 이 정확 하면 됩 니 다.
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
이 키 값 은 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 통합 에 관 한 더 많은 내용 은 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 응원 부 탁 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기