mybatis与spring整合需要添加几个jar包,mybatis-spring, spring-context, spring-jdbc

1. spring ioc只要一个jar包就ok

2. 我用了c3p3数据库连接池

跟笔记<一>比,需要修改的地方

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.gxf.mybatis</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.4.RELEASE</version>
</dependency> </dependencies> </project>

添加beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:mapper/*.xml"/>
</bean>
<bean id="personMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.gxf.mybatis.mapper.PersonMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> <bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <context:property-placeholder location="jdbc.properties"/> </beans>

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/db_test
jdbc.username=root
jdbc.password=luckygxf

测试类main方法

package com.gxf.mybatis.util;

import com.gxf.mybatis.mapper.PersonMapper;
import com.gxf.mybatis.mapper.PersonMapper1;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class PersonTableOps {
private static SqlSessionFactory sqlSessionFactory = SqlSessionFactoryHelper.getSqlSessionFactory();
private static PersonMapper personMapper; public static void main(String[] args) {
testSpringMybatis();
} private static void testSpringMybatis(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
personMapper = (PersonMapper) applicationContext.getBean("personMapper");
Person person = personMapper.selectPerson(1);
System.out.println(person);
} /**
* test mybatis select method
* */
private static void testSelectOnes(){
// selectOne1();
// selectOne2();
selectOne3();
} /**
* 第一种方式
* */
public static void selectOne1(){
SqlSession session = sqlSessionFactory.openSession();
try {
Person person = session.selectOne(
"com.gxf.mybatis.mapper.PersonMapper.selectPerson", 1);
System.out.println(person);
} finally {
session.close();
}
} /**
* 第二种方式
* */
public static void selectOne2(){
SqlSession session = sqlSessionFactory.openSession();
try {
PersonMapper mapper = session.getMapper(PersonMapper.class);
Person person = mapper.selectPerson(1);
System.out.println(person);
} finally {
session.close();
} } /**
* 第三种方式
* */
public static void selectOne3(){
SqlSession session = sqlSessionFactory.openSession();
try {
sqlSessionFactory.getConfiguration().addMapper(PersonMapper1.class);
PersonMapper1 mapper = session.getMapper(PersonMapper1.class);
Person person = mapper.selectPerson(1);
System.out.println(person);
} finally {
session.close();
}
} public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
} public static void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
PersonTableOps.sqlSessionFactory = sqlSessionFactory;
} public PersonMapper getPersonMapper() {
return personMapper;
} public void setPersonMapper(PersonMapper personMapper) {
this.personMapper = personMapper;
}
}

以前没有想到的

1. spring ioc 一个jar包搞定

2. 使用配置文件,占位符

接下来要看下mybatis源码,还有spring整合到一起的源码,出个笔记

mybatis笔记<二> 整合spring的更多相关文章

  1. MyBatis笔记二:配置

    MyBatis笔记二:配置 1.全局配置 1.properites 这个配置主要是引入我们的 properites 配置文件的: <properties resource="db.pr ...

  2. java框架之MyBatis(2)-进阶&整合Spring&逆向工程

    进阶内容 准备 jdbc.url=jdbc:mysql://192.168.208.192:3306/test?characterEncoding=utf-8 jdbc.driver=com.mysq ...

  3. Mybatis笔记二:接口式编程

    目录 旧方法的弊端 接口式编程 接口式编程的好处 接口式编程的增删改查 旧方法的弊端 在Mybatis笔记一中,我们使用命名空间+id的方式实现了Mybatis的执行,不过这里的命名空间是我们随便写的 ...

  4. SpringBoot学习笔记二之Spring整合Mybatis

    原文链接: https://www.toutiao.com/i6803235766274097678/ 在learn-admin-component子工程中加入搭建环境所需要的具体依赖(因为比较长配置 ...

  5. Spring整合MyBatis(二)Spring整合MyBatis

    摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 了解了MyBatis的独立使用过程后,我们再看看它与Spring整合的 ...

  6. (原创)mybatis学习二,spring和mybatis的融合

    mybatis学习一夯实基础 上文介绍了mybatis的相关知识,这一节主要来介绍mybaits和spring的融合 一,环境搭建 1,jar包下载,下载路径为jar包 2,将包导入到java工程中 ...

  7. Mybatis笔记二

    一对一查询 案例:查询所有订单信息,订单信息中显示下单人信息. 注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则 ...

  8. MyBatis使用总结+整合Spring

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .20 ...

  9. Mybatis笔记二:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    错误异常:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.test.dao.N ...

随机推荐

  1. [ActionScripte 3.0] swf和网页通信

    很多时候,我们需要将swf放到网页加载,并且将相应的资源都放到网页上,在网页中加载swf通常是通过"*.swf?p=..&p2=..."这样的方式来调用这个swf和传参数的 ...

  2. 991 AlvinZH的奇幻猜想----整数乘积plus(背包DP大作战P)

    914 AlvinZH的奇幻猜想----整数乘积puls 思路 难题.动态规划. 将数字串按字符串输入,处理起来更方便些. dp[i][j]:表示str[0~i]中插入j个乘号时的乘积最大值.状态转移 ...

  3. USART列子

    #include "stm32f10x.h" void USART_INit(void) { GPIO_InitTypeDef GPIO_Initstructe; USART_In ...

  4. Python——Django学习笔记

    Django——一个封装好的神奇框架 若本文有任何内容错误,望各位大佬指出批评,并请直接联系作者修改,谢谢!小白学习不易. 一.简要模型 模型类操作数据表: python manage.py shel ...

  5. PIE SDK组件式开发综合运用示例

    1. 功能概述 关于PIE SDK的功能开发,在我们的博客上已经分门别类的进行了展示,点击PIESat博客就可以访问,为了初学者入门,本章节将对从PIE SDK组件式二次开发如何搭建界面.如何综合开发 ...

  6. vue 无限级分类导航

    递归组件,实现无限级分类导航 https://cn.vuejs.org/v2/guide/components-edge-cases.html#%E9%80%92%E5%BD%92%E7%BB%84% ...

  7. PHP Variable handling 函数

    Variable handling 函数: boolval — 获取变量的布尔值debug_zval_dump — 将内部zend值的字符串表示转储为输出doubleval — floatval 的别 ...

  8. CDH集群安装配置(二)- 公共环境的配置和虚拟机的克隆

    1. 配置网络-ip地址设置静态 vi /etc/sysconfig/network-scripts/ifcfg-eth33 增加如下配置 ONBOOT=yes BOOTPROTO=static IP ...

  9. web.py简易示例

    http://webpy.org/cookbook/index.zh-cn code.py import web urls = ( '/', 'index' ) class index: def GE ...

  10. 【python】-matplotlib.pylab常规用法

    目的: 了解matplotlib.pylab常规用法 示例 import matplotlib.pylab as pl x = range(10) y = [i * i for i in x] pl. ...