SPring整合Mybatis方式一
Spring整合Mybatis
需要maven包:
mysql-connector-java 5.1.47,
mybatis 3.5.2,
spring-webmvc 5.2.2.RELEASE,
spring-jdbc 5.2.2.RELEASE,
aspectjweaver 1.8.13
mybatis-spring 2.0.2,
目录:

pom.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
<artifactId>SpringMybatis</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency> </dependencies>
<!--maven由于他的约定大于配置,我们之后可以能遇到我们写的配置文件,无法被处理或生效问题,解决方案-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
Mybatis-config.xml配置文件:
因为是spring可以完全掌管Mybatis但把mybatis的一些配置留给Mybatis-config比较好
<?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>
<typeAliases>
<package name="com.hdlf.pojo"/>
</typeAliases> <mappers>
<mapper resource="com/hdlf/mapper/UserMapper.xml"/>
</mappers>
</configuration>
UserMapper接口:
package com.hdlf.mapper;
import com.hdlf.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> getalluser();
}
UserMapper.xml配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hdlf.mapper.UserMapper">
<select id="getalluser" resultType="User">
select * from mybatis.user
</select>
</mapper>
User实体类:
package com.hdlf.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
重点,spring-config.xml配置:
根据官网上可以找到:
DataSource使用spring的数据源可以替换mybatis的配置,需要使用DriverManagerDataSource
sqlSessionFactory绑定mybatis配置文件,重点可以配合mybatis配置文件,而mybatis配置文件可以关联mappers
SqlSessionTemplate就是常用的sqlsession只是封装了,但需要构造器注入,把sqlSessionFactory绑定的配置注入到里面即可
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--DataSource:使用spring的数据源替换mybatis的配置
使用spring提供的jdbc:org.springframework.jdbc.datasource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&UseUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--绑定mybatis配置文件-->
<property name="configLocation" value="classpath:Mybatis-config.xml"/>
</bean> <!--SqlSessionTemplate:就是mybatis经常使用的SqlSession-->
<bean id="SqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入sqlSessionFactory,因为没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean> <bean id="usermapper" class="com.hdlf.mapper.UserMapperTemplate">
<property name="SqlSession" ref="SqlSession"></property> </bean>
</beans>
UserMapperTemplate实现类:
实现Usermapper的方法,封装SqlSessionTemplate,然后用getalluesr返回mapper.getalluser();所有数据
package com.hdlf.mapper; import com.hdlf.pojo.User;
import org.mybatis.spring.SqlSessionTemplate; import java.util.List; public class UserMapperTemplate implements UserMapper { private SqlSessionTemplate SqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) {
this.SqlSession = sqlSession;
} public List<User> getalluser() {
UserMapper mapper = SqlSession.getMapper(UserMapper.class);
return mapper.getalluser();
} }
测试类:
不用配置mybatis之类的麻烦的配置
package com.hdlf.test; import com.hdlf.mapper.UserMapper;
import com.hdlf.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException; public class Mytest {
public static void main(String[] args) throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
UserMapper userMapper = context.getBean("usermapper", UserMapper.class);
for (User user : userMapper.getalluser()) {
System.out.println(user); } }
}
结果:

SPring整合Mybatis方式一的更多相关文章
- spring 整合 mybatis 中数据源的几种配置方式
因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下. 一.采用org.mybatis.spring.mapp ...
- Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二
接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...
- spring整合mybatis(hibernate)配置
一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...
- Mybatis学习(六)————— Spring整合mybatis
一.Spring整合mybatis思路 非常简单,这里先回顾一下mybatis最基础的根基, mybatis,有两个配置文件 全局配置文件SqlMapConfig.xml(配置数据源,全局变量,加载映 ...
- Spring整合MyBatis 你get了吗?
Spring整合MyBatis 1.整体架构dao,entity,service,servlet,xml 2..引入依赖 <dependencies> <dependency> ...
- Mybatis(六) Spring整合mybatis
心莫浮躁~踏踏实实走,一步一个脚印,就算不学习,玩,能干嘛呢?人生就是那样,要找点有意思,打发时间的事情来做,而钻研技术,动脑动手的过程,还是比其他工作更有意思些~ so,努力啥的都是强迫自己做自以为 ...
- 简单探讨spring整合mybatis时sqlSession不需要释放关闭的问题
https://blog.csdn.net/RicardoDing/article/details/79899686 近期,在使用spring和mybatis框架编写代码时,sqlSession不需要 ...
- Spring整合MyBatis(三)sqlSessionFactory创建
摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 目录 一.SqlSessionFactoryBean的初始化 二.获取 ...
- Spring整合MyBatis(二)Spring整合MyBatis
摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 了解了MyBatis的独立使用过程后,我们再看看它与Spring整合的 ...
随机推荐
- 压力测试-apachebench
压力测试-apachebench 1. 压力测试 压力测试的概念\定义: 性能测试Performance Test :是指通过自动化的测试工具模拟多种正常.峰值以及异常负载条件来对系统的各项 ...
- 吴裕雄--天生自然TensorFlow2教程:多输出感知机及其梯度
import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...
- express session 实现登录
https://www.cnblogs.com/mingjiatang/p/7495321.html Express + Session 实现登录验证 1. 写在前面 当我们登录了一个网站,在没有 ...
- 修改oracle数据库用户密码的方法
WIN+R打开运行窗口,输入cmd进入命令行: 输入sqlplus ,输入用户名,输入口令(如果是超级管理员SYS的话需在口令之后加上as sysdba)进入sql命令行: 连接成功后,输入“s ...
- Update(Stage4):sparksql:第5节 SparkSQL_出租车利用率分析案例
目录: 1. 业务2. 流程分析3. 数据读取5. 数据清洗6. 行政区信息 6.1. 需求介绍 6.2. 工具介绍 6.3. 具体实现7. 会话统计 导读 本项目是 SparkSQL 阶段的练习项目 ...
- GitHub vs. Bitbucket 不只是功能不同
https://www.oschina.net/translate/bitbucket-vs-github-its-more-than-just-features 让我们回到2005年,Bitkeep ...
- 详解python的数字类型变量与其方法
以下内容引自:https://www.jb51.net/article/97752.htm python数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间.下面 ...
- PTA的Python练习题(四)
从 第3章-1 3-1.大于身高的平均值 开始 1. 引入列表的概念 a=list(map(int,input().split())) b=sum(a) c=len(a) d=b/c for i in ...
- nginx的preaccess 阶段的limit_req模块与limit_conn模块
limit_conn 模块限制并发连接数 [root@python vhast]# vim limit_conn.conf limit_conn_zone $binary_remote_addr zo ...
- 吴裕雄--天生自然ORACLE数据库学习笔记:其它数据对象
create index emp_deptno_index on emp(deptno) pctfree tablespace users; create bitmap index emp_salar ...