eclipse环境下spring整合mybatis详细教程[通俗易懂]

eclipse环境下spring整合mybatis详细教程[通俗易懂]系列目录第一篇:3分钟快速了解Mybatis的基础配置第二篇:带你3分钟了解Mybatis映射文件(sql,resultMap等映射)第三篇:三分钟带你了解mybatis关联映射(案例分析一对一,多对多)原创不易,如若喜欢,就点一点赞,关注一下吧!文章目录系列目录一、整合环境搭建-jar包准备1.spring所需要使用的jar包有(8+2):2.mybatis所需要使用的jar包有3.spring整合mybatis的中间jar二、整合环境搭建-创建项目1.eclipse环境创建2.jar添

大家好,又见面了,我是你们的朋友全栈君。

系列目录

第一篇:3分钟快速了解Mybatis的基础配置
第二篇:带你3分钟了解Mybatis映射文件(sql,resultMap等映射)
第三篇: 三分钟带你了解mybatis关联映射(案例分析一对一,多对多)

原创不易,如若喜欢,就点一点赞,关注一下吧!


一、整合环境搭建-jar包准备

1.spring所需要使用的jar包有(8+2):

  • spring-aop-5.0.4.RELEASE.jar、
  • spring-aspects-5.0.4.RELEASE.jar、
  • spring-beans-5.0.4.RELEASE.jar、
  • spring-context-5.0.4.RELEASE.jar、
  • spring-core-5.0.4.RELEASE.jar、
  • spring-expression-5.0.4.RELEASE.jar、
  • spring-jdbc-5.0.4.RELEASE.jar、
  • spring-tx-5.0.4.RELEASE-javadoc.jar
  • 以及aop和aspect的补充jar (2个)
  • aopalliance-1.0.jar
  • aspectjweaver-1.5.4.jar

2.mybatis所需要使用的jar包有

  • mybatis-3.4.6.jar
  • commons-logging-1.2.jar
  • log4j-1.2.17.jar

3.spring整合mybatis的中间jar

  • //数据库启动jar
  • mysql-connector-java-5.1.9.jar
  • //mybatis支持spring整合的中间件 jar
  • mybatis-spring-1.3.1.jar
  • // 数据源支持jar
  • commons-dbcp2-2.1.1.jar
  • commons-pooI2-2.4.2.jar

二、整合环境搭建-创建项目

1.eclipse环境创建

在eclipse环境下 创建一个spring-mybatis的项目(java项目或动态网站项目都可以),并将第一步中的jar包添加到项目类路径中。

2.jar添加到类路径的两种方式

  • java项目添加到内路径方式: 将jar包粘贴到src目录下,全选右键选择build path 点击add build path,添加完成后的截图如下:
    在这里插入图片描述
  • 动态网站添加到内路径方式: 将jar包粘贴到项目的lib目录中,并发布到类路径下。添加完成后的截图如下:
    在这里插入图片描述

三、整合环境搭建-编写配置文档

1.db.properties(数据库相关信息文档)

  • 在src目录下新建一个文件,命名为db.properties,在里面添加连接数据库的相关信息。
<!--db.properties文件内容-->
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=true
jdbc.username=root
jdbc.password=mysql200410
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

2.spring配置文件

  • 在src目录下新建一个xml文件,命名为applicationContext,在里面配置spring的相关信息
<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
		
	<!-- 加载配置文件db.properties方式1:通过类来加载
	<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="locations">
			<array>
				<value>db.properties</value>
			</array>
		</property>
	</bean>
	-->
	<!--  <!-- 加载配置文件db.properties方式2:通过context来加载-->
	<context:property-placeholder location="classpath:db.properties"/> 
	 <!--配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
		 <!--数据库驱动 -->
        <property name="driverClassName" value="${jdbc.driver}" />
        <!--连接数据库的url -->
        <property name="url" value="${jdbc.url}" />
        <!--连接数据库的用户名 -->
        <property name="username" value="${jdbc.username}" />
        <!--连接数据库的密码 -->
        <property name="password" value="${jdbc.password}" />
        <!--最大连接数 -->
        <property name="maxTotal" value="${jdbc.maxTotal}" />
        <!--最大空闲连接  -->
        <property name="maxIdle" value="${jdbc.maxIdle}" />
        <!--初始化连接数  -->
        <property name="initialSize" value="${jdbc.initialSize}" />
	</bean>
	 <!--配置事物管理  -->
	<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	 <!--开启事物注解  -->
	<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
	 <!--配置mybatis工厂  -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
	 <!--加载数据源  -->
		<property name="dataSource" ref="dataSource"/>
		 <!--加载配置文件  -->
		<property name="configLocation" value="mybatis-config.xml"></property>
	</bean>
</beans>

3.mybatis配置文件

  • 在src目录下新建一个xml文件,命名为mybatis-config.xml,在里面配置mybatis的相关信息。
<?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>
<!--设置pojo别名-->
<typeAliases><package name="org.spring.beans"/></typeAliases>
<!--加载CustomerMapper.xml文件-->
	<mappers>
		<mapper resource="org/spring/mapper/CustomerMapper.xml"/>
	</mappers>
</configuration>

四、开发整合-完成项目

  • 在上述搭建的环境中,进行项目测试,数据库表图与架构图如下:
    在这里插入图片描述

在这里插入图片描述

1.实现持久层

  • 在src目录下创建org.spring.benas 包并创建Customer类,该类中的变量与数据库中的表的字段一一对应
package org.spring.beans;

public class Customer {
	private int Id;
	private String username;
	private String jobs;
	private String phone;
	public Customer() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Customer(int Id, String username, String jobs, String phone) {
		super();
		this.Id = Id;
		this.username = username;
		this.jobs = jobs;
		this.phone = phone;
	}
	public int getId() {
		return Id;
	}
	public void setId(int Id) {
		this.Id = Id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getJobs() {
		return jobs;
	}
	public void setJobs(String jobs) {
		this.jobs = jobs;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		return "Customer [id=" + Id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]";
	}
	
}

2.创建数据库映射文件

  • 在src目录下创建org.spring.mapper包并创建CustomerMapper.xml文件,并将sql语句写入其中。
<?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="org.spring.mapper.CustomerMapper">
	<select id="querycustomer" resultType="org.spring.beans.Customer" parameterType="int">
		select * from customer where id=#{id}
	</select>
</mapper>

3.实现dao层(为了方便映射,我们将其更名为CustomerMapper.java)

  • 在src目录下的org.spring.mapper包下创建CustomerMapper.java接口,并将方法写入其中。
package org.spring.mapper;

import org.spring.beans.Customer;

public interface CustomerMapper {
	public Customer querycustomer(int id);

}

4.实现dao层的实现类

  • 在src目录下创建org.spring.dao.impl包并创建CustomerDaoImpl.java实现类,在实现整合的过程中,我们需要继承SqlSessionTemplate类或SqlSessionDaoSupport,通过这两个其中之一类的getSqlSessionO 方法来获取所需的SqlSession。
package org.spring.dao.impl;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.spring.beans.Customer;
import org.spring.mapper.CustomerMapper;

public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerMapper{

	@Override
	public Customer querycustomer(int id) {
		return this.getSqlSession().selectOne("org.spring.mapper.CustomerMapper.querycustomer", id);
	}

}

5.实现log4j.xml日志

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.org.spring=DEBUG
# Console output... 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
  • 需要注意的是,我们在粘贴log4j.xml文件时,需要修改log4j.logger.org.spring=DEBUG中的对应的包的位置。否则将出错。

五、项目测试

  • 在src目录下创建org.spring.test包并创建Test.java类,完成测试。
package org.spring.test;

import org.spring.beans.Customer;
import org.spring.mapper.CustomerMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerMapper cus = (CustomerMapper) context.getBean("customerMapper");
		Customer customer = cus.querycustomer(2);
		System.out.println(customer);
	}
}

  • 测试结果正确,完成整合工作。测试结果如下:
    在这里插入图片描述

原创不易,如若喜欢,就点一点赞,关注一下吧!求赞求关注!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134265.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • HibernateTemplate使用方法

    HibernateTemplate使用方法HibernateTemplate提供非常多的常用方法来完成基本的操作,比如通常的增加、删除、修改、查询等操作,Spring2.0更增加对命名SQL查询的支持,也增加对分页的支持。大部分情况下,使用Hibernate的常规用法,就可完成大多数DAO对象的CRUD操作。1、常用方法:   1)voiddelete(Objectentity):删除指定持久化实例   2)dele

    2022年6月16日
    23
  • <asp: DropDownList>实现事件处理「建议收藏」

    <asp: DropDownList>实现事件处理「建议收藏」需求:从上面的截图中,可以看到这是两个控件实现的界面,现在的需求是这样的,实现当选择第一个下拉控件并选择了相应的数据后,那么此时在第二个中进行绑定他的子类在此显示,从而实现页面两级菜单实现数据统一绑定。解决办法:  tr>                   th>服务大类th>                    td class=”pro_title_css”>     

    2022年9月12日
    0
  • MacBook 屏幕录制 soundflower 只录内屏声音 无外界声音

    MacBook 屏幕录制 soundflower 只录内屏声音 无外界声音MacBook屏幕录制只包含内屏声音无外界录音目的录屏方法办法目的用Mac自带的QuickTimePlayer录制屏幕的时候(或者按快捷键⇧+⌘+5),三个选项:1)无声音2)选外置扬声器。电脑外放,确实能录到内屏声音,但是扬声器收录的人声、环境音也会录进来3)插耳机后,可以选择耳机。这样内屏声音也没了,只有耳机口的收音被录进来录屏方法办法下载插件soundflower:soundflower下载地址一开始可能下载失败,提示“来自开发者MATTINGALLS的系统软件已被阻止载

    2022年5月2日
    562
  • 极兔速递电子面单API接口-快递鸟[通俗易懂]

    极兔速递电子面单API接口-快递鸟[通俗易懂]目录1.完成前期准备工作2.API接口3.请求完整报文(示例)4.成功返回报文(示例)5.失败返回报文(示例)6.分步讲解(C#版本)7.极兔速递电子面单打印模板内容(HTML)8.关于签名前言J&T极兔速递是一家科技创新型互联网快递物流企业,致力于为用户带来优质的快递和物流体验。2015年8月由印尼首都雅加达作为起点,进入快递物流市场,目前覆盖了印度尼西亚、越南、马来西亚、泰国、菲律宾、柬埔寨及新加坡七个国家,成为东南亚超过5.5亿人口信赖的综合性物流服务商。电子面单模板

    2022年6月20日
    78
  • 关于LSM树_完全m叉树

    关于LSM树_完全m叉树前言推出一个新系列,《看图轻松理解数据结构和算法》,主要使用图片来描述常见的数据结构和算法,轻松阅读并理解掌握。本系列包括各种堆、各种队列、各种列表、各种树、各种图、各种排序等等几十篇的样子。关于LSM树LSM树,即日志结构合并树(Log-StructuredMerge-Tree)。其实它并不属于一个具体的数据结构,它更多是一种数据结构的设计思想。大多NoSQL数据库核心思想都是基于LSM来做的,只是具体的实现不同。所以本来不打算列入该系列,但是有朋友留言了好几次让我讲LSM树,那么就说一下L

    2022年10月28日
    0
  • 什么是跨域及怎么解决跨域问题?[通俗易懂]

    什么是跨域及怎么解决跨域问题?[通俗易懂]什么是跨域?这篇博文解释的挺清楚,我直接引用https://blog.csdn.net/lambert310/article/details/51683775跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器施加的安全限制。所谓同源是指,域名,协议,端口均相同,只要有一个不同,就是跨域。不明白没关系,举个栗子:http://www.123.com/i…

    2022年4月29日
    40

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号