入门案例
1.1 创建测试表
CREATE DATABASE mp;
USE mp;
CREATE TABLE tbl_employee(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(50),
email VARCHAR(50),
gender CHAR(1),
age INT
);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom','tom@test.com',1,22);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Jerry','jerry@test.com',0,25);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Black','black@test.com',1,30);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('White','white@test.com',0,35);
1.2 创建Javabean
@Data
@TableName(value = "tbl_employee")
public class Employee {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField(value = "last_name")
private String lastName;
private String email;
private Integer gender;
private Integer age;
@TableField(exist = false)
private Double salary;
}
1.3 依赖配置
1.3.1 pom添加
在 pom.xml 中加入对 MP、Spring、连接池、Junit、Mysql 驱动、lombok等依赖特别说明: Mybatis 及 Mybatis-Spring 依赖请勿加入项目配置,以免引起版本冲突!!! Mybatis-Plus 会自动帮你维护
1.3.2 添加mybatis全局配置文件
1.3.3 添加log4j.xml
1.3.4 加入db.properties连接信息配置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mp
jdbc.username=root
jdbc.password=123456
1.3.5 加入spring的配置文件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:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 事务管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 基于注解的事务管理 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!-- 配置SqlSessionFactoryBean
Mybatis提供的: org.mybatis.spring.SqlSessionFactoryBean
MP提供的:com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean
-->
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 别名处理 -->
<property name="typeAliasesPackage" value="com.atguigu.mp.beans"/>
<!-- 注入全局MP策略配置 -->
<property name="globalConfig" ref="globalConfiguration"/>
</bean>
<!-- 定义MybatisPlus的全局策略配置-->
<bean id="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!-- 在2.3版本以后,dbColumnUnderline 默认值就是true -->
<property name="dbColumnUnderline" value="true"/>
<!-- 全局的主键策略 -->
<property name="idType" value="0"/>
<!-- 全局的表前缀策略配置 -->
<property name="tablePrefix" value="tbl_"/>
</bean>
<!-- 配置mybatis 扫描mapper接口的路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.don.mp.mapper"/>
</bean>
</beans>
1.4 测试
测试 Spring-Mybatis 的环境
public class TestMP01 {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testDataSource() throws Exception {
DataSource ds = ioc.getBean("dataSource", DataSource.class);
System.out.println(ds);
Connection conn = ds.getConnection();
System.out.println(conn);
}
}
1.5 集成MP
Mybatis-Plus 的集成非常简单,对于 Spring,我们仅仅需要把 Mybatis 自带的
MybatisSqlSessionFactoryBean 替换为 MP 自带的即可
配置SqlSessionFactoryBean
Mybatis提供的: org.mybatis.spring.SqlSessionFactoryBean
MP提供的:com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean