Spring+MyBatis整合过程
步骤:
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整合过程的更多相关文章
- springMVC+spring+mybatis整合过程中遇到的问题
今天在配置SSM整合的过程中遇到了几个错误,折腾了好久,具体如下 1.java.lang.IllegalArgumentException: Mapped Statements collection ...
- Spring+Mybatis整合过程中找不到.properties文件
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' ...
- 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)
前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...
- Spring + mybatis整合方案总结 结合实例应用
Spring + mybatis整合实例应用 项目结构图 (Spring3.0.2 +mybatis3.0.4) 方案一: 通过配置文件整合Spring和mybatis 应用数据库 -- --数据库 ...
- SpringMVC+Spring+Mybatis整合
SpringMVC+Spring+Mybatis整合 导包 配置jdbc.properties.log4j.properties jdbc.driver=com.mysql.jdbc.Driver j ...
- SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。
SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...
- Spring+Mybatis整合时 Failed to read candidate component class,Caused by:IllegalArgumentException
Spring+Mybatis整合时Caused by: java.lang.IllegalArgumentException错误 org.springframework.beans.factory.B ...
- Springmvc+Spring+Mybatis整合开发(架构搭建)
Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...
- spring 和 mybatis 整合过程 (包含分页)
1.spring-mybatis.xml : 配置 SqlSessionFactory 和 MapperScannerConfigurer <bean id="sqlSessio ...
随机推荐
- Spark学习之键值对操作总结
键值对 RDD 是 Spark 中许多操作所需要的常见数据类型.键值对 RDD 通常用来进行聚合计算.我们一般要先通过一些初始 ETL(抽取.转化.装载)操作来将数据转化为键值对形式.键值对 RDD ...
- Protobuf 语言指南(proto3)
Protobuf 语言指南(proto3) Protocol Buffer是Google的语言中立的,平台中立的,可扩展机制的,用于序列化结构化数据 - 对比XML,但更小,更快,更简单.您可以定义数 ...
- 理解ASP.NET Core 依赖注入
目录: 一.什么是依赖注入 1.1.什么是依赖? 1.2. 什么是注入? 1.3.依赖注入解决的问题 二.服务的生命周期(.Net Core DI) 三.替换默认服务容器 3.1.为什么替换默认服务容 ...
- MobileForm控件的使用方式-用.NET(C#)开发APP的学习日志
今天继续Smobiler开发APP的学习日志,这次是做一个title.toolbar.侧边栏三种效果 样式一 一. Toolbar 1. 目标样式 我们要实现上图中的效果 ...
- Eclipse4JavaEE安装Gradle,并导入我们的Gradle项目
第一步:下载Gradle Gradle下载链接,如下图,下载最新版本即可.下载下来的zip包,解压到一个目录即可,如F盘 第二步:配置环境变量 首先添加GRADLE_HOME,如下图 然后在Path下 ...
- 学web前端的第一天
大家好,我是蓝颜.上次写博客是18年的4月份,不是不想写,是不知道怎么写,求写博客的技巧.从今天开始一天一更,不管写的怎么样,坚持的写下去.闲话不多说,第一次接触前端,什么都不懂,因为对这玩意的热爱, ...
- Windows Server 2016-Netdom Join之客户端加域(二)
上章节我们介绍了如何通过图形化界面实现用户加域操作,本章节为大家简单介绍如何通过netdom join命令实现计算机加域操作.具体信息如下: 将工作站或成员服务器加入域.将计算机加入域的行为会为域中的 ...
- 配置IIS网站,我遇到的那些坑~
配置错误 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的(overrideModeDefault="Deny"),或者是通过包含 over ...
- nn.ConvTranspose2d的参数output_padding的作用
参考:https://blog.csdn.net/qq_41368247/article/details/86626446 使用前提:stride > 1 补充:same卷积操作 是通过padd ...
- 从0到1打造自己的VOIP网络电话系统(基于FreePBX)
从0到1打造自己的网络电话系统 最近流量卡越来越便宜了,看看自己手里的"坑不死老用户"的联通卡,顿时感觉到深深的恶意,但是iPhone没有双卡功能,所以只好自己动手打造一个网络电话 ...