步骤:

  1.引入Spring+MyBatis开发包

    >spring(ioc aop dao)开发包

    >mybatis开发包,dbcp,驱动包

    >mybatis-spring.jar整合包

  2.引入Spring+MyBatis配置文件

    >applicationContext.xml

    >sqlmap-config.xml

  3.写实体类

  4.定义SQL和Mapper映射器接口

  5.在Spring中追加MyBatis整合配置

---------------------------------------------------------------------------------

在applicationContext.xml

引入外部db.properties

 <context:property-placeholder location="classpath:db.properties" />

配置数据源

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>

配置SqlSessionFactoryBean

 <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:sqlmap-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>

配置MapperScannerConfigurer

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描com.xdl.mapper下的Mapper接口创建对象 -->
<property name="basePackage" value="com.xdl.mapper"></property>
</bean>

在sqlmap-config.xml指定SQL定义文件以及打印日志和分页

 <?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>
<!-- 将底层日志打印 -->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<!-- 分页 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper"></plugin>
</plugins>
<!-- 指定SQL定义文件 -->
<mappers>
<mapper class="com.xdl.mapper.DeptMapper" />
</mappers>
</configuration>

写实体类

 package com.xdl.entity;

 import java.io.Serializable;

 public class Dept implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer deptno;// 类型和名称与表保持一致
private String dname;
private String loc; public Dept(String dname, String loc) {
super();
this.dname = dname;
this.loc = loc;
} public Dept() {
super();
} public Dept(Integer deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
} public Integer getDeptno() {
return deptno;
} public void setDeptno(Integer deptno) {
this.deptno = deptno;
} public String getDname() {
return dname;
} public void setDname(String dname) {
this.dname = dname;
} public String getLoc() {
return loc;
} public void setLoc(String loc) {
this.loc = loc;
} }

写DeptMapper接口,里面使用标注实现增删改查(单值DQL返回对象,多值查询返回集合,DML返回int或void)

 package com.xdl.mapper;

 import java.util.List;

 import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import com.xdl.entity.Dept; public interface DeptMapper {
// 查询所有
@Select("select * from dept")
List<Dept> findAll(); // 根据id查询单值
@Select("select * from dept where deptno = #{no}")
Dept findById(int id); // 根据id修改名字
@Update("update dept set dname = #{name} where deptno = #{no}")
int updateById(@Param("no") int id, @Param("name") String name); // 根据id修改名字和地址
@Update("update dept set dname = #{dname},loc = #{loc} where deptno = #{deptno}")
int update(Dept dept); // 插入名字和地址,序列自增长
@Insert("insert into dept (deptno,dname,loc) values (dept_seq.nextval,#{dname},#{loc})")
int insert(Dept dept);
}

最后使用junit测试

 package com.xdl.test;

 import java.util.List;

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xdl.entity.Dept;
import com.xdl.mapper.DeptMapper; public class TestDept {
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
DeptMapper deptDao = ioc.getBean("deptMapper", DeptMapper.class);
@Test
public void testDept() {
// 插入分页设置
Page<Dept> page = PageHelper.startPage(2, 3);
List<Dept> list = deptDao.findAll();
System.out.println("dept: " + list);
for (Dept dept : list) {
System.out.println("\n" + dept.getDeptno() + ":" + dept.getDname() + ":" + dept.getLoc());
}
// getTotal 全部的
System.out.println("\n总行数:" + page.getPageSize() + "\n" + "总页数:" + page.getPageNum());
} @Test
public void testDeptById() {
Dept dept = deptDao.findById(10);
String str = "'查到了' + " + dept + " +'条记录'";
System.out.println(str);
System.out.println(dept.getDname() + ":" + dept.getLoc());
} @Test
public void testUpdateBy() {
int row = deptDao.updateById(20, "wangcai");
String str = "'修改了' + " + row + " +'条记录'";
System.out.println(str);
} @Test
public void testUpdate() {
int row = deptDao.update(new Dept(20, "wanghua", "sx"));
String str = "'插入了' + " + row + " +'条记录'";
System.out.println(str);
} @Test
public void testInsert() {
int row = deptDao.insert(new Dept("wl", "g"));
System.out.println(row);
}
}

注意:

在配置db.properties里的账号和密码

在测试的时候回出现用户名或密码错误,这个原因是因为默认使用本地计算机用户名,为了区分系统用户名,所以在username和password前面加上xxx.

Spring+MyBatis整合过程的更多相关文章

  1. springMVC+spring+mybatis整合过程中遇到的问题

    今天在配置SSM整合的过程中遇到了几个错误,折腾了好久,具体如下 1.java.lang.IllegalArgumentException: Mapped Statements collection ...

  2. Spring+Mybatis整合过程中找不到.properties文件

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' ...

  3. 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)

    前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...

  4. Spring + mybatis整合方案总结 结合实例应用

    Spring + mybatis整合实例应用 项目结构图 (Spring3.0.2 +mybatis3.0.4) 方案一: 通过配置文件整合Spring和mybatis 应用数据库 -- --数据库 ...

  5. SpringMVC+Spring+Mybatis整合

    SpringMVC+Spring+Mybatis整合 导包 配置jdbc.properties.log4j.properties jdbc.driver=com.mysql.jdbc.Driver j ...

  6. SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。

    SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...

  7. Spring+Mybatis整合时 Failed to read candidate component class,Caused by:IllegalArgumentException

    Spring+Mybatis整合时Caused by: java.lang.IllegalArgumentException错误 org.springframework.beans.factory.B ...

  8. Springmvc+Spring+Mybatis整合开发(架构搭建)

    Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...

  9. spring 和 mybatis 整合过程 (包含分页)

    1.spring-mybatis.xml  : 配置 SqlSessionFactory 和  MapperScannerConfigurer  <bean id="sqlSessio ...

随机推荐

  1. 【.NET异步编程系列3】取消异步操作

    在.Net和C#中运行异步代码相当简单,因为我们有时候需要取消正在进行的异步操作,通过本文,可以掌握 通过CancellationToken取消任务(包括non-cancellable任务).  早期 ...

  2. EIGRP 基础实验

    一.环境准备 1. 软件:GNS3 2. 路由:c7200 二.实验操作 实验要求: 1.掌握EIGRP 的基本配置 2.掌握EIGRP 的通配符掩配置方法 3.掌握EIGRP 的自动汇总特性,理解E ...

  3. 邀您参加 | BigData & Alluxio 交流会-成都站

    4月27日,在天府之国,与你共享大数据与Alluxio的技术魅力. 活动介绍 本期技术沙龙将会聚焦在大数据.存储.数据库以及Alluxio应用实践等领域,邀请腾讯技术专家和业界技术专家现场分享关于Al ...

  4. 通过 React Hooks 声明式地使用 setInterval

    本文由云+社区发表 作者:Dan Abramov 接触 React Hooks 一定时间的你,也许会碰到一个神奇的问题: setInterval 用起来没你想的简单. Ryan Florence 在他 ...

  5. 调用pymysql模块操作数据库

    1.创建数据库表: def create_table(tb_name): import pymysql#导入模块 #连接数据库 db = pymysql.Connect(','zabbix_db') ...

  6. 新手学习WEB前端流程以及学习中常见的误区

    学习web前端编程技术肯定是以就业拿到高薪工作为主要目的的,可是高薪不会那么轻易拿到,这是一个最简单的道理.没有付出就没有回报,在整个学习web前端编程技术的过程中,你需要付出时间.精力.金钱.废话不 ...

  7. Android远程桌面助手(B1413)

    ARDC(B1413) 1.解决Android9显示黑屏问题;2.解决向导菜单显示异常问题;3.解决部分手机无法正常连接的问题;4.切换到WiFi连接时,增加显示NetworkID;5.更新图片压缩的 ...

  8. 软件工程通用软件体系结构主机终端模式、B/S 、C/S 结构和多层分布式结构

    软件系统的体系结构经历了主机终端模式.客户机/服务器(C/S)模式.浏览器/服务器(B/S)和多层分布式结构. 主机/终端结构: 早期计算机系统多是单机系统,多个用户是通过联网终端来访问的,没有网络的 ...

  9. 一个能快速写出实体类的Api文档管理工具

    今天各种MVC框架满天飞,大大降低了编码的难度,写实体类就没有办法回避的一件事了,花大把的时间去做一些重复而且繁琐的工作,实在不是一个优秀程序员的作风,所以多次查找和尝试后,找到一个工具类网站——Ap ...

  10. LeetCode算法题-Letter Case Permutation(Java实现)

    这是悦乐书的第315次更新,第336篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第184题(顺位题号是784).给定一个字符串S,将每个字母单独转换为小写或大写以创建另 ...