欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Spring與Mybatis的整合方法(基于Eclipse的搭建)

 更新時(shí)間:2020年10月17日 11:58:35   作者:Jektong  
這篇文章主要介紹了Spring與Mybatis的整合方法(基于Eclipse的搭建),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

項(xiàng)目工程總覽:

在這里插入圖片描述

項(xiàng)目路徑建的包不是唯一,只要之后配置的路徑映射正確即可

Emp.java

 <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>
    <!-- 測(cè)試包 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <!-- 表示開(kāi)發(fā)的時(shí)候引入,發(fā)布的時(shí)候不會(huì)加載此包 -->
      <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>
    <!-- 導(dǎo)入Mysql數(shù)據(jù)庫(kù)鏈接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>

EmpMapper.java 與EmpMapper.xml配置

package com.jektong.dao;
import java.util.List;
import com.jektong.entity.Emp;
/**
 * @author jektong 
 * @Date 2020-10-16 10:13:12
 */
public interface EmpMapper {	
	List<Emp> selectAllEmps();
}

EmpMapper.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jektong.dao.EmpMapper">
	<resultMap type="emp" id="empMap">
		<id property="id" column="e_id" jdbcType="INTEGER" />
		<result property="name" column="e_name" jdbcType="VARCHAR" />
		<result property="salary" column="e_salary" jdbcType="DOUBLE" />
		<result property="bonus" column="e_bonus" jdbcType="DOUBLE" />
		<result property="hiredate" column="e_hiredte" jdbcType="DATE" />
		<result property="deptno" column="e_deptno" jdbcType="INTEGER" />
	</resultMap>
	<!-- 查詢所有Emp -->
	<select id="selectAllEmps" resultType="list" resultMap="empMap">
		select * from t_emp
	</select>
</mapper>

配置數(shù)據(jù)源db.properties文件

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

配置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 ">
    

	<!-- 加載配置文件并配置數(shù)據(jù)庫(kù)連接池 -->
	<util:properties id="db" location="classpath:db.properties"/>
  <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
    <property name="url" value="#{db.url}"/>
    <property name="password" value="#{db.password}"/>
    <property name="username" value="#{db.username}"/>
    <property name="driverClassName" value="#{db.driver}"/>
  </bean>
  
  <!-- 配置SqlSessionFactoryBean -->
	 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 引用數(shù)據(jù)源的組件 -->
  	 <property name="dataSource" ref="dataSource"/> 
  	<!-- 映射文件指定 -->
  	<property name="mapperLocations" value="classpath:com/jektong/mapper/*.xml"/>
  	<!-- 類(lèi)型別名 -->
  	<property name="typeAliasesPackage" value="com.jektong.entity"/>
	</bean> 
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.jektong.dao"/>
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
	</bean>
</beans>

test

package com.jektong.test;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jektong.dao.EmpMapper;
import com.jektong.entity.Emp;
/**
 * @author jektong
 * @Date 2020-10-15 16:31:58
 */
public class TestOne {
	@Test
	public void t() throws Exception {
	  ApplicationContext ac =
	        new ClassPathXmlApplicationContext("applicationContext.xml");
	  EmpMapper mapper = (EmpMapper) ac.getBean("empMapper");
		List<Emp> selectAllEmps = mapper.selectAllEmps();
		for (Emp emps : selectAllEmps) {
			System.out.println(emps.getName());
		}
	}
}

到此這篇關(guān)于詳解Spring與Mybatis的整合方法(基于Eclipse的搭建)的文章就介紹到這了,更多相關(guān)Spring與Mybatis的整合內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java多線程的用法詳細(xì)介紹

    Java多線程的用法詳細(xì)介紹

    這篇文章主要介紹了Java多線程的用法詳細(xì)介紹的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • idea tomcat亂碼問(wèn)題的解決及相關(guān)設(shè)置的步驟

    idea tomcat亂碼問(wèn)題的解決及相關(guān)設(shè)置的步驟

    這篇文章主要介紹了idea tomcat亂碼問(wèn)題的解決及相關(guān)設(shè)置的步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • JAVA數(shù)字千分位和小數(shù)點(diǎn)的現(xiàn)實(shí)代碼(處理金額問(wèn)題)

    JAVA數(shù)字千分位和小數(shù)點(diǎn)的現(xiàn)實(shí)代碼(處理金額問(wèn)題)

    這篇文章主要介紹了JAVA數(shù)字千分位和小數(shù)點(diǎn)的現(xiàn)實(shí)代碼(處理金額問(wèn)題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • springboot+springJdbc+postgresql 實(shí)現(xiàn)多數(shù)據(jù)源的配置

    springboot+springJdbc+postgresql 實(shí)現(xiàn)多數(shù)據(jù)源的配置

    本文主要介紹了springboot+springJdbc+postgresql 實(shí)現(xiàn)多數(shù)據(jù)源的配置,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 使用opencsv文件讀寫(xiě)CSV文件

    使用opencsv文件讀寫(xiě)CSV文件

    這篇文章主要為大家詳細(xì)介紹了用opencsv文件讀寫(xiě)CSV文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 簡(jiǎn)單談?wù)凧ava垃圾回收

    簡(jiǎn)單談?wù)凧ava垃圾回收

    本文是看了James Gosling的<<Java程序設(shè)計(jì)語(yǔ)言>>后結(jié)合自己的一些項(xiàng)目經(jīng)驗(yàn),簡(jiǎn)單總結(jié)下關(guān)于java的垃圾回收問(wèn)題的看法,有需要的小伙伴可以參考下
    2016-05-05
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(35)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(35)

    下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你
    2021-07-07
  • 本地jvm執(zhí)行flink程序帶web ui的操作

    本地jvm執(zhí)行flink程序帶web ui的操作

    這篇文章主要介紹了本地jvm執(zhí)行flink程序帶web ui的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 深入理解java final不可變性

    深入理解java final不可變性

    本文主要介紹了講講java final不可變性,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java中Thread類(lèi)基本用法詳解

    Java中Thread類(lèi)基本用法詳解

    Java中的Thread類(lèi)是用于創(chuàng)建和管理線程的類(lèi),Thread類(lèi)提供了許多方法來(lái)管理線程,包括啟動(dòng)線程、中斷線程、暫停線程等,下面這篇文章主要給大家介紹了關(guān)于Java中Thread類(lèi)基本用法的相關(guān)資料,需要的朋友可以參考下
    2023-06-06

最新評(píng)論