MyBatis与Spring整合过程实例解析

网友投稿 275 2022-12-13


MyBatis与Spring整合过程实例解析

这篇文章主要介绍了MyBatis与Spring整合过程实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

从之前的代码中可以看出直接使用 MyBatis 框架的 SqlSession 访问数据库并不简便。MyBatis 框架的重点是 SQL 映射文件,为方便后续学习,本节讲解 MyBatis 与 Spring 的整合。教程的后续讲解中将使用整合后的框架进行演示。

导入相关JAR包

1)MyBatis 框架所需的 JAR 包

图 1MyBatis相关的JAR包

2)Spring 框架所需的 JAR 包

aopalliance-1.0.jar

aspectjweaver-1.6.9.jar

spring-aop-3.2.13.RELEASE.jar

spring-aspects-3.2.13.RELEASE.jar

spring-beans-3.2.13.RELEASE.jar

spring-context-3.2.13.RELEASE.jar

spring-core-3.2.13.RELEASE.jar

spring-expression-3.2.13.RELEASE.jar

spring-jdbc-3.2.13.RELEASE.jar

spring-tx-3.2.13.RELEASE.jar

3)MyBatis 与 Spring 整合的中间 JAR 包

该中间 JAR 包的版本为 mybatis-spring-1.3.1.jar,此版本可以从网址“http://mvnrepository.com/artifact/org.mybatis/mybatis-spring/1.3.1”下载。

4)数据库驱动 JAR 包

教程所使用的Mybatis数据库驱动包为 mysql-connector-java-5.1.25-bin.jar。

5)数据源所需的 JAR 包

在整合时使用的是 DBCP 数据源,需要准备 DBCP 和连接池的 JAR 包。

本教程所用版本的 DBCP 的 JAR 包为 commons-dbcp2-2.2.0.jar,可以从网址“htttp://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi”下载。

最新版本的连接池的 JAR 包为 commons-pool2-2.5.0.jar,可以从网址“http://commons.apache.org/proper/commons-pool/download_pool.cgi”下载。

在Spring中配置MyBatis工厂

通过与 Spring 的整合,MyBatis 的 SessionFactory 交由 Spring 来构建,在构建时需要在 Spring 的配置文件中添加如下代码:

使用 Spring 管理 MyBatis 的数据操作接口

使用 Spring 管理 MyBatis 数据操作接口的方式有多种,其中最常用、最简洁的一种是基于 MapperScannerConfigurer 的整合。该方式需要在 Spring 的配置文件中加入以下内容:

项目结构

第一步:entity层

public class City implements Serializable {

private long cid;

private String cname;

private long pid;

public long getCid() {

return cid;

}

public void setCid(long cid) {

this.cid = cid;

}

public String getCname() {

return cname;

}

public void setCname(String cname) {

this.cname = cname;

}

public long getPid() {

return pid;

}

public void setPid(long pid) {

this.pid = pid;

}

}

第二步:Dao层

@MapperScan

public interface UserMapper {

public City getUserList(Integer cid);

}

第三步:service层

public interface UserService {

public City getUserList(Integer cid);

}

第四步:service实现层

@Service("userService")

public class UserServiceImpl implements UserService {

@Resource

private UserMapper userMapper;//声明UserMapper接口引用

@Override

public City getUserList(Integer cid) {

return userMapper.getUserList(cid);

}

}

第五步:CityMapper.xml

PUBLIC "-//mybatis.org//DTO Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from city where cid=#{cid}

第六步:applicationContext.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/sQQEPjlRXWpring-tx-2.5.xsd

QQEPjlRXW http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-2.5.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/sQQEPjlRXWpring-tx-2.5.xsd

QQEPjlRXW http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-2.5.xsd">

第七步:jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/test?useUniCode=true&characterEncoding=utf-8

jdbc.username=root

jdbc.password=root

第八步:mybatis-config.xml

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

第九步:测试

@Test

public void shouldAnswerWithTrue()

{

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

UserService userService = (UserService)ctx.getBean("userService");

City userList = userService.getUserList(130000);

System.out.println(userList.getCname());

}

以上代码是基于注解,如果想要XML方式,下面就是,谢谢根据上面的进行修改:修改applicationContext.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-2.5.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-2.5.xsd">

-->

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-2.5.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-2.5.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-2.5.xsd">

-->

修改Dao层

public interface UserMapper {

public City getUserList(Integer cid);

}

修改Service实现类

public class UserServiceImpl implements UserService {

public UserMapper getUserMapper() {

return userMapper;

}

public void setUserMapper(UserMapper userMapper) {

this.userMapper = userMapper;

}

private UserMapper userMapper;//声明UserMapper接口引用

@Override

public City getUserList(Integer cid) {

return userMapper.getUserList(cid);

}

}

以上都是对Spring和Mybatis进行整合,希望对你有一些帮助,谢谢


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring JDK动态代理实现过程详解
下一篇:Spring MVC处理方法返回值过程解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~