步骤:

  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. 【STM32H7教程】第7章 STM32H7下载和调试方法(IAR8)

    完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=86980 第7章   STM32H7下载和调试方法(IAR8) 本 ...

  2. 记录Ocelot + SignalR 多服务端测试

    前言 分两个项目,一个Gatway,一个SignalR 贴代码 1.Gatway 1.引用Ocelot 2.添加一点点代码 Startup.cs 3.简单配置ocelot ocelot.json { ...

  3. github下载和上传项目

    git下载和上传项目 下载: git clone +地址 上传: 1.git init 在当前项目的目录中生成本地的git管理(多一个.git文件夹,为隐藏文件) 2.git add .(注意最后面有 ...

  4. 从一到万的运维之路,说一说VM/Docker/Kubernetes/ServiceMesh

    摘要:本文从单机真机运营的历史讲起,逐步介绍虚拟化.容器化.Docker.Kubernetes.ServiceMesh的发展历程.并重点介绍了容器化阶段之后,各项重点技术的安装.使用.运维知识.可以说 ...

  5. Elasticsearch最佳实践之分片使用优化

    本文由云+社区发表 作者:老生姜 一.遇到的问题 与大多数分布式系统一样,Elasticsearch按照一定的Hash规则把用户数据切分成多个分片,然后打散到不同机器进行存储,从而实现大规模数据的分布 ...

  6. VirtualAPK的简单使用

    VirtualApk引入步骤: 一.宿主应用引入VirtualApk 1.在项目的build.gradle文件中加入依赖: dependencies { classpath 'com.didi.vir ...

  7. 关于int main( int argc, char* argv[] ) 中arg和argv参数的解析及调试

    https://blog.csdn.net/LYJ_viviani/article/details/51873961 https://stackoverflow.com/questions/30241 ...

  8. 第16次CCF CSP认证-第5题-317 号子任务(subtask317)-图论最短路径

    [题目背景]“你在平原上走着走着,突然迎面遇到一堵墙,这墙向上无限高,向下无限深,向左无限远,向右无限远,这墙是什么?”——<流浪地球>原著我们带着地球去流浪了,为了处理流浪过程中可能会发 ...

  9. 【Spring源码分析系列】ApplicationContext 相关接口架构分析

    [原创文章,转载请注明出处][本文地址]http://www.cnblogs.com/zffenger/p/5813470.html 在使用Spring的时候,我们经常需要先得到一个Applicati ...

  10. JS的常用正则表达式 验证密码(转载自用)

    JS的正则表达式 强:字母+数字+特殊字符   ^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^& ...