目录
1.Mybatis + MP(纯Mybatis与Mybatis-Plus整合)
一、简介
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
文档地址:https://mybatis.plus/guide/
源码地址:https://github.com/baomidou/mybatis-plus
MP架构
二、开发时注意事项:
1.Mybatis + MP(纯Mybatis与Mybatis-Plus整合)
1)将UserMapper继承BaseMapper,将拥有BaseMapper中所有方法
2)出现table表不存在的时候需要在User对象中添加@TableName,指定数据库表名
3)mybatis和mybatis—plus的写法略有不同,在获取方法的时候mybatis需要写出方法比如findAll但是mp自带方法selectList可以直接使用,只需要接口继承基础类
2.Spring + Mybatis + MP
引入了Spring框架,数据源、构建等工作就交给了Spring管理
1)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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:*.properties"/>
<!-- 定义数据源 -->
<!--在测试的时候${jdbc.url}可能会找不到,原因是这个配置文件放在了main文件夹下,而不是test文件夹下,这时有两种解决办法
要么就把${jdbc.url}写死,要么就把文件applicationContest.xml放在test文件夹下,其他的视情况而定
-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="maxActive" value="10"/>
<property name="minIdle" value="5"/>
</bean>
<!--这里使用MP提供的sqlSessionFactory,完成了Spring与MP的整合-->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="globalConfig">
<bean class="com.baomidou.mybatisplus.core.config.GlobalConfig">
<property name="dbConfig">
<bean class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig">
<property name="idType" value="AUTO"/>
</bean>
</property>
</bean>
</prop