步骤:

  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. c#Socket服务器与客户端的开发(2)

    上一篇文章我们使用原生的socket分别实现了服务器和客户端, 本篇文章使用SuperSocket来开发实现服务器, 之前也介绍了SuperSocket是一个轻量级, 跨平台而且可扩展的 .Net/M ...

  2. flask下载excel

    flask 应用的基本结构: htmlweb.py -- static -- templates 将 bootstrap.min.css 放到 static 文件夹下,在 templates 文件夹下 ...

  3. SoEasyPlatform 代码生成器

    介绍 一款轻量级开源的代码生成器,相对较动软代码生成器而言要轻量的多,支持多种数据库,所用到dll组件也都在github有源码,代码非常的简单有点基础的看源码可以把生成的项目改成自已的风格. 特色 该 ...

  4. WPF TextBox 正则验证 大于等于0 小于等于1 的两位小数

    正则:^(0\.\d+|[1-9][0-9]|1)$ TextBox绑定正则验证 <TextBox x:Name="txb"   MaxLength="6" ...

  5. Echarts 南海诸岛简图显示位置调整

        最近需要echart同时显示海南岛和南海诸岛,开始想寻找南海诸岛的数据,经过查找,这种简图数据是没有的(china.js地图数据一一找过了),下图是echarts的一些示例,没有满足我们的要求 ...

  6. Docker 创建 Crucible4.6.1 以及与 Crowd3.3.2 实现 SSO 单点登录

    目录 目录 1.介绍 1.1.什么是 Crucible? 2.Crucible 的官网在哪里? 3.如何下载安装? 4.对 Crucible 进行配置 4.1.破解 Crucible 第一步 4.2. ...

  7. lombok的安装

    Lombok简介 Lombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法.官方地址:https:/ ...

  8. 逆向-攻防世界-crackme

    查壳,nSpack壳,直接用软件脱壳,IDA载入程序. 很明显,就是将402130的数据和输入的数据进行异或,判断是否等于402150处的数据.dwrd占4字节. 这道题主要记录一下刚学到的,直接在I ...

  9. Amqp整合com.rabbitmq.client.ShutdownSignalException: channel error; protocol method异常处理

    java.io.IOException at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:126) at com.rabbitmq ...

  10. asp.net core导出导入excel

    使用NPOI导入导出excel,已经封装好Action可以直接调用 导出 效果图 使用方法 定义导出实体 class Student { public int Id { get; set; } pub ...