zoukankan      html  css  js  c++  java
  • mybatis和spring的整合

    Mybatis与Spring的集成

    1.配置Spring环境

    创建maven工程

    pom.xml导入依赖

     1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     3     <modelVersion>4.0.0</modelVersion>
     4     <parent>
     5         <groupId>cn.itcast.parent</groupId>
     6         <artifactId>itcast-parent</artifactId>
     7         <version>0.0.1-SNAPSHOT</version>
     8     </parent>
     9     <groupId>cn.itcast.mybatis</groupId>
    10     <artifactId>itcast-mybatis-spring</artifactId>
    11     <version>1.0.0-SNAPSHOT</version>
    12 
    13 
    14     <dependencies>
    15         <!-- 单元测试 -->
    16         <dependency>
    17             <groupId>junit</groupId>
    18             <artifactId>junit</artifactId>
    19             <scope>test</scope>
    20         </dependency>
    21         <!-- Mybatis -->
    22         <dependency>
    23             <groupId>org.mybatis</groupId>
    24             <artifactId>mybatis</artifactId>
    25         </dependency>
    26         <!-- MySql -->
    27         <dependency>
    28             <groupId>mysql</groupId>
    29             <artifactId>mysql-connector-java</artifactId>
    30         </dependency>
    31         <dependency>
    32             <groupId>org.slf4j</groupId>
    33             <artifactId>slf4j-log4j12</artifactId>
    34         </dependency>
    35         <dependency>
    36             <groupId>cglib</groupId>
    37             <artifactId>cglib</artifactId>
    38             <version>3.1</version>
    39         </dependency>
    40         <dependency>
    41             <groupId>com.github.pagehelper</groupId>
    42             <artifactId>pagehelper</artifactId>
    43             <version>3.7.5</version>
    44         </dependency>
    45         <dependency>
    46             <groupId>com.github.jsqlparser</groupId>
    47             <artifactId>jsqlparser</artifactId>
    48             <version>0.9.1</version>
    49         </dependency>
    50 
    51         <!-- 整合 -->
    52         <dependency>
    53             <groupId>org.springframework</groupId>
    54             <artifactId>spring-context</artifactId>
    55         </dependency>
    56         <dependency>
    57             <groupId>org.mybatis</groupId>
    58             <artifactId>mybatis-spring</artifactId>
    59         </dependency>
    60 
    61         <!-- 连接池 -->
    62         <dependency>
    63             <groupId>com.jolbox</groupId>
    64             <artifactId>bonecp-spring</artifactId>
    65             <version>0.8.0.RELEASE</version>
    66         </dependency>
    67         <dependency>
    68             <groupId>org.springframework</groupId>
    69             <artifactId>spring-jdbc</artifactId>
    70         </dependency>
    71         <dependency>
    72             <groupId>org.springframework</groupId>
    73             <artifactId>spring-aspects</artifactId>
    74         </dependency>
    75     </dependencies>
    76 </project>

    创建applicationContext.xml文件

    jdbc.properties

    1 jdbc.driver=com.mysql.jdbc.Driver
    2 jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis_0716?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    3 jdbc.username=root
    4 jdbc.password=123456

    applicationContext.xml(配置资源文件替换器、配置数据源)

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
     8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     9 
    10     <!-- 使用spring自带的占位符替换功能 -->
    11     <bean
    12         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    13         <!-- 允许JVM参数覆盖 -->
    14         <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    15         <!-- 忽略没有找到的资源文件 -->
    16         <property name="ignoreResourceNotFound" value="true" />
    17         <!-- 配置资源文件 -->
    18         <property name="locations">
    19             <list>
    20                 <value>classpath:jdbc.properties</value>
    21             </list>
    22         </property>
    23     </bean>
    24 
    25     <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
    26         destroy-method="close">
    27         <!-- 数据库驱动 -->
    28         <property name="driverClass" value="${jdbc.driver}" />
    29         <!-- 相应驱动的jdbcUrl -->
    30         <property name="jdbcUrl" value="${jdbc.url}" />
    31         <!-- 数据库的用户名 -->
    32         <property name="username" value="${jdbc.username}" />
    33         <!-- 数据库的密码 -->
    34         <property name="password" value="${jdbc.password}" />
    35         <!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
    36         <property name="idleConnectionTestPeriod" value="60" />
    37         <!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
    38         <property name="idleMaxAge" value="30" />
    39         <!-- 每个分区最大的连接数 -->
    40         <property name="maxConnectionsPerPartition" value="150" />
    41         <!-- 每个分区最小的连接数 -->
    42         <property name="minConnectionsPerPartition" value="5" />
    43     </bean>
    44 
    45 </beans>

    applicationContext-mybatis.xml(SqlSessionFactory)

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
     8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     9 
    10     <!-- 定义Mybatis的SqlSessionFactory -->
    11     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    12         <!-- 定义数据源 -->
    13         <property name="dataSource" ref="dataSource" />
    14         <!-- 指定mybatis全局配置文件 -->
    15         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    16         
    17         <!-- 扫描mappers目录以及子目录下的所有xml文件 -->
    18           <property name="mapperLocations" value="classpath:mappers/**/*.xml" />
    19           
    20           <!-- 别名扫描包 -->
    21           <property name="typeAliasesPackage" value="cn.itcast.mybatis.pojo"/>
    22     </bean>
    23 
    24 
    25 </beans>

    mybatis-config.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE configuration
     3   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
     4   "http://mybatis.org/dtd/mybatis-3-config.dtd">
     5 <configuration>
     6 
     7     <settings>
     8         <!-- 开启驼峰自动映射 -->
     9         <setting name="mapUnderscoreToCamelCase" value="true" />
    10     </settings>
    11 
    12 </configuration>

    单元测试:

    UserMapperTest

     1 package cn.itcast.mybatis.mapper;
     2 
     3 import org.apache.ibatis.session.SqlSession;
     4 import org.apache.ibatis.session.SqlSessionFactory;
     5 import org.junit.Before;
     6 import org.junit.Test;
     7 import org.springframework.context.ApplicationContext;
     8 import org.springframework.context.support.ClassPathXmlApplicationContext;
     9 
    10 import cn.itcast.mybatis.pojo.User;
    11 
    12 public class UserMapperTest {
    13     
    14     private UserMapper userMapper;
    15 
    16     @Before
    17     public void setUp() throws Exception {
    18         // 初始化SPring容器
    19         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml",
    20                 "applicationContext-mybatis.xml");
    21         //从容器中获取SqlSessionFactory
    22         SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
    23         SqlSession sqlSession = sqlSessionFactory.openSession(true);
    24         this.userMapper = sqlSession.getMapper(UserMapper.class);
    25     }
    26 
    27     @Test
    28     public void testQueryUserById() {
    29         User user = this.userMapper.queryUserById(1L);
    30         System.out.println(user);
    31     }
    32 
    33 }

    测试成功:

    注意:我的mysql数据库用户名和密码是root root

    所以要修改jdbc.properties设置

    1 jdbc.driver=com.mysql.jdbc.Driver
    2 jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    3 jdbc.username=root
    4 jdbc.password=root

    所以对应的改成:

    定义Mapper接口

    在Spring容器中定义Mapper接口,无需手动获取SqlSessionFactory获取Session以及Mapper,只需要从Spring容器中获取即可。

    配置Mapper接口的自动扫描器

    多个包怎么配置,可以用逗号

    通配符配置Mapper.xml和别名扫描包

  • 相关阅读:
    51nod_1445 变色DNA 最短路模板 奇妙思维
    51nod_1459 最短路 dijkstra 特调参数
    UVA_10653 公主与王子 #刘汝佳DP题刷完计划
    HOJ 13819 Height map
    51nod_1255字典序最小的子序列
    电梯设计需求调研报告
    梦断代码读后感
    求一循环数组的最大子数组的和
    求二维数组中最大子数组的和
    四则运算
  • 原文地址:https://www.cnblogs.com/ZHOUVIP/p/7257309.html
Copyright © 2011-2022 走看看