博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaEE Spring与MyBatis的整合之传统DAO方式整合(教材学习笔记)
阅读量:3894 次
发布时间:2019-05-23

本文共 3618 字,大约阅读时间需要 12 分钟。

在实际开发中MyBatis都是与Spring整合在一起使用的,在之前学习了MyBatis与Spring,现在来学习如何使他们整合

首先创建一个名为chapter10的web项目

一、环境搭建

1.准备好所有的有关jar包,具体如下:

将上面所有jar包添加到项目lib目录下,并发布到类路径下 

2.编写配置文件

在src目录下创建db.properties文件,Spring配置文件以及MyBatis的配置文件,三个文件分别如下所示:

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatisjdbc.username=rootjdbc.password=itcastjdbc.maxTotal=30jdbc.maxIdle=10jdbc.initialSize=5

3.此外还需要创建log4j.properties文件

# Global logging configurationlog4j.rootLogger=ERROR, stdout# MyBatis logging configuration...log4j.logger.com.itheima=DEBUG# Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

二、基于传统的DAO方式开发整合

1.在src目录下创建com.itheima.po包,并在包中创建持久化类Customer

package com.itheima.po;public class Customer {	private Integer id;	private String username;	private String jobs;	private String phone;	public Integer getId() {		return id;	}	public void setId(Integer 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.在com.itheima.po包中,创建映射文件CustomerMapper.xml在该文件下编写根据id查询的映射语句

3.在mybatis的配置文件mybatis-config.xml中,配置映射文件Customer.xml的位置,具体如下:

4.在src目录下创建一个com.itheima.dao包,并在包中创建接口CustomerDao,在接口中编写一个通过id查询客户的方法findCustomerById()

package com.itheima.dao;import com.itheima.po.Customer;public interface CustomerDao {    public Customer findCustomerById(Integer id);}

5.在src目录下创建一个com.itheima.dao.impl包,并在包中创建接口CustomerDao的实现类

package com.itheima.dao.impl;import org.mybatis.spring.support.SqlSessionDaoSupport;import com.itheima.dao.CustomerDao;import com.itheima.po.Customer;public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerDao {	@Override	public Customer findCustomerById(Integer id) {		return this.getSqlSession().selectOne("com.itheima.po.CustomerMapper.findCustomerById",id);	}}

6.在spring的配置文件中,编写实例化CustomerDaoImpl的配置,并将SqlSessionFactory对象注入之中

7、整合测试,在src目录下创建com.itheima.test包,并在包中创建测试类DaoTest类,在类中编写测试方法findCustomerByIdDaoTest()

package com.itheima.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.itheima.dao.CustomerDao;import com.itheima.po.Customer;public class DaoTest {	@Test	public void findCustomerByIdTest() {		ApplicationContext act= new ClassPathXmlApplicationContext("applicationContext.xml");		CustomerDao customerDao = act.getBean(CustomerDao.class);		Customer customer = customerDao.findCustomerById(1);		System.out.println(customer);	}}

8.测试结果如下:

和数据库进行对比

 

转载地址:http://xfohn.baihongyu.com/

你可能感兴趣的文章
Matlab读取avi视频并播放 你必须要知道的
查看>>
word字体大小与公式编辑器字体对照表
查看>>
visio画图-----如何克服两箭头交叉变形 及 箭头自动重绘?
查看>>
Android开发:安装NDK,移植OpenCV2.3.1,JNI调用OpenCV全过程
查看>>
“金9银10”2020年JVM高频率面试题整理,技术提升就差一个点!
查看>>
简简单单的分享2020常见的MySQL面试题MySQL与答案整理
查看>>
听说只有大厂的Android工程师才能全答对这20道题?我看你在吹牛哦!
查看>>
武功秘籍之 Redis 面试题全掌握,学完马上找面试官对线!
查看>>
50道!2020年!!MySQL高频数据库面试题解析,你都懂了吗?
查看>>
如何用Spring Boot加密配置文件中的特殊内容示例代码详解
查看>>
谈谈这些年面试官给大伙下的那些套,如何解?(面试技巧)
查看>>
5年开发经验的我被几条朋友圈打击到,点燃自己冲击阿里面经!
查看>>
5年工作经验的我放弃安逸,一份来自腾讯魔鬼面试的终极考验!
查看>>
学JAVA吗同学,这篇Sping boot 确定不了解下么?
查看>>
(3年+offer)华为技术岗面试初面+综合面试经验总结
查看>>
男默女泪,努力复习的我终于通过社招进入BAT工作了!(JAVA+JVM+框架+中间件+Spring干货分享)
查看>>
Python 导包
查看>>
dok_matrix
查看>>
theano 后端爆内存
查看>>
os.environ 和 keras.json
查看>>