1、引入相关jar包(版本对应关系3.3.1版本的mybatis和1.1.1的spring-mybatis匹配,3.4.1版本的mybatis和1.3.1的spring-mybatis匹配)

如果不对应会报org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()类似错误

maven的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>com.hjp.mybatisdemo</groupId>
<artifactId>mybatisdemo</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.1.4.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.3</version>
</dependency> <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency> </dependencies> <build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build> </project>

pom.xml

2、在classpath下创建applicationContext.xml文件,并配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!--引用Java配置文件-->
<context:property-placeholder location="db.properties"></context:property-placeholder> <!--配置数据源,使用dbcp数据库连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driver}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="5"></property>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--myBatis配置文件路径-->
<property name="configLocation" value="sqlMapConfig.xml"></property>
<!--数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean> <!--批量mapper配置,bean的名字默认为mapper接口类名首字母小写-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定包名-->
<property name="basePackage" value="com.hjp.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> </beans>

applicationContext.xml

3、测试代码如下

import com.hjp.mapper.UserMapper;
import com.hjp.po.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemoTest { private ApplicationContext context; @Before
public void setUp(){
context=new ClassPathXmlApplicationContext("applicationContext.xml");
} @Test
public void func1() throws Exception {
UserMapper userMapper= (UserMapper) context.getBean("userMapper");
User user=userMapper.findUserById(1);
System.out.println(user);
} }

MyBatis入门程序之整合Spring的更多相关文章

  1. Mybatis入门程序

    作为一个java的学习者,我相信JDBC是大家最早接触也是入门级别的数据库连接方式,所以我们先来回忆一下JDBC作为一种用于执行SQL语句的Java API是如何工作的.下面的一段代码就是最基本的JD ...

  2. Mybatis学习——Mybatis入门程序

    MyBatis入门程序 一.查询用户 1.使用客户编号查询用户 (1).创建一个数据表 USE spring; #创建一个名为t_customer的表 CREATE TABLE t_customer( ...

  3. MyBatis入门程序(基于XML配置)

    创建一个简单的MyBatis入门程序,实现对学生信息的增删改查功能(基于XML配置) 一.新建一个Java工程,导入MyBatis核心jar包.日志相关的jar包以及连接Oracle数据库所需驱动包, ...

  4. MyBatis入门程序(1)

    一.入门程序: 1.mybatis的配置文件SqlMapConfig.xml 配置mybatis的运行环境,数据源.事务等. <?xml version="1.0" enco ...

  5. Mybatis入门程序(一)

    1.入门程序实现需求 根据用户id查询一个用户信息 根据用户名称模糊查询用户信息列表 添加用户(二) 更新用户(二) 删除用户(二) 2.引入Mybatis所需 jar 包(Maven工程) < ...

  6. 搭建Spring + SpringMVC + Mybatis框架之三(整合Spring、Mybatis和Spring MVC)

    整合Spring和SpringMVC 之前已经整合了spring和mybatis,现在在此基础上整合SSM. 项目目录: 思路:SpringMVC的配置文件独立,然后在web.xml中配置整合. (1 ...

  7. Mybatis入门程序编写

    执行原理 入门程序编写 1.pom.xml 文件 <dependencies> <dependency> <groupId>mysql</groupId> ...

  8. mybatis入门程序-(二)

    1. 添加配置文件 log4j.properties # Global logging configuration #开发环境下日志级别设置成DEBUG,生产环境设置成info或者error log4 ...

  9. MyBatis(9)整合spring

    具体的感兴趣可以参考:MyBatis 此时此刻,没用的话不再多说了,直接开始代码工程吧! 整体的代码实现: 具体使用到的我们在进行细说 基本上理解一边就能会使用整合  准备工作:  db.proper ...

随机推荐

  1. -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.

    在执行[maven clean]的时候报错,错误信息如下: -Dmaven.multiModuleProjectDirectory system property is not set. Check ...

  2. node学习笔记2——搭建服务器

    搭建服务器关键词: require('http') —— 请求 node 里面的 http 模块 createServer —— 创建一个服务器 request —— 请求 response—— 响应 ...

  3. Spring,hibernate,struts的面试笔试题(含答案)

    Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久 ...

  4. 【转】MySQL count(*)速度慢优化

    select count(*)是MySQL中用于统计记录行数最常用的方法. count方法可以返回表内精确的行数,每执行一次都会进行一次全表扫描, 以避免由于其他连接进行delete和insert引起 ...

  5. Python 私有方法和引用计数初讲

    #私有方法的定义与调用 class Cat: def __init__(self): self.name = "" self.age = 0 def __changeage(sel ...

  6. Patterns for Asynchronous MVVM Applications: Commands

    Stephen Cleary Download the Code Sample This is the second article in a series on combining async an ...

  7. Reusable async validation for WPF with Prism 5

    WPF has supported validation since the first release in .NET 3.0. That support is built into the bin ...

  8. Opengl绘制我们的小屋(四)第三人称漫游

    本节内容是在第一人称漫游上完成的,请先了解上文中第一人称漫游的实现. 这一节讲下第三人称漫游是如何实现,第三人称,简单来说,就是在你后面会跟着一台摄像机顺着你拍摄. 先看一下失败的尝试.这个方法是把人 ...

  9. 【6】JVM-内存分配与回收策略

        JAVA技术体系中的自动内存管理实际上就是自动化的解决了给对象分配内存以及回收给对象分配的内存这两个问题.回收部分通过之前的<GC设计思路分析>和<垃圾收集器>这两篇博 ...

  10. 杂乱所得之RPC【待整理】

    在计算机的世界里,不仅有程序内部的通信,还需要程序之间的通信,这又包含两大类:同一台主机的程序之间的通信.不同主机的程序之间的通信. 同一台主机的程序之间的通信就是IPC,IPC(Inter-proc ...