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. 虚拟机安装以及PCL的配置(2)

    那我们无论在虚拟机还是在双系统的Ubuntu环境下都是一样的安装过程 我们使用快捷键“ctrl+alt+T”,来打开一个命令窗口如下图 比如我们在命令窗口下输入ls 我们会看到在主目录下的所有文件 下 ...

  2. 总结这两天连续干掉的bug In 创新实训 智能自然语言交流系

    临近项目合并,在pre合并中出现相当多的hug,遂记之 ps:这只是总结一下提纲,具体的方法在前文的博文中都详细记录.总结了. 平台的移植兼容性,虽然是跨平台的java,但是依旧有很多的意外: 1.从 ...

  3. 深入浅出 kvm qemu libvirt

    在所谓的kvm技术中,应用到的其实有2个东西:qemu+kvmkvm负责cpu虚拟化+内存虚拟化,实现了cpu和内存的虚拟化,但kvm不能模拟其他设备:qemu是模拟IO设备(网卡,磁盘),kvm加上 ...

  4. Mysql 配置文件和目录的合理分配

    记得给权限呀, 权限呀, 都是泪 多实例 单实例 配置文件 [client] port = 3301 socket = /data/mysql3301/data/mysqld3301.sock [my ...

  5. CodeCombat地牢关卡Python代码

    最近迷上了玩CodeCombat,特将地牢关卡的Python代码整理如下,供有兴趣的人学习交流探讨 1,Kithgard地牢 hero.moveRight() hero.moveDown() hero ...

  6. 第三百三十三节,web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录—获取Scrapy框架Cookies

    第三百三十三节,web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录 模拟浏览器登录 start_requests()方法,可以返回一个请求给爬虫的起始网站,这个返回的请求相当于star ...

  7. e831. 从JTabbedPane中删除一个卡片

    // To create a tabbed pane, see e828 创建JTabbedPane // Remove the last tab pane.remove(pane.getTabCou ...

  8. com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method

    查看了网友们的错误原因, 需要进行实例化的类没有进行实例化,具体没有实例化的类会在错误信息中显示,在错误信息中搜索“Serializable”即可找到将其实现序列化可消除错误. 是在使用Dubbo提供 ...

  9. Linux使用 tar命令-g参数进行增量+差异备份、还原文件

    原文链接:http://www.cnblogs.com/gnuhpc/ 完整备份: 建立测试路径与档案 mkdir test touch test/{a,b,c} 在test下生成三个文件 执行完整备 ...

  10. PHP进阶。

    老手段,百度“PHP进阶” 不过,今天运气不错,搜到一个“PHP特级内容讲解”,地址是:http://wenku.baidu.com/course/view/fd8e591b6bd97f192279e ...