首先,贴上官网
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
新建一个pojo类,对应表的字段
package com.xff.pojo;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.enums.IdType;
import java.util.Date;
/**
* * 这里可以使用@TableName()指定映射的表名 */public class User {
@TableId(value = "id",type = IdType.AUTO)//value是对应数据库字段的名称,type为主键策略
private int id;
private String name;
private Date birth;
private int age;
private String dog;
/**
* 如果变量名和表里的字段名不符 * 可以使用@TableField指定对应表中的字段 * * @return
*/
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDog() {
return dog;
}
public void setDog(String dog) {
this.dog = dog;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", birth=" + birth +
", age=" + age +
", dog='" + dog + '\'' +
'}';
}
}
新建一个Mapper并继承Mybatis Plus里的BaseMapper类
public interface UserMapper extends BaseMapper<User> {
}
<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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<context:component-scan base-package="com.xff.service"/>
<context:component-scan base-package="com.xff.mapper" />
<context:annotation-config></context:annotation-config>
<!-- 加载配置文件 -->
<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>
<!-- sqlSessinFactory -->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="mybatis-config.xml" />
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
<property name="globalConfig" ref="globalConfiguration" />
</bean>
<!--mybatisplus全局配置-->
<bean id="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!--
在2.3版本以后,dbColumnUnderline 默认值就是true 驼峰命名转换为下划线命名 user_name对应userName --> <property name="dbColumnUnderline" value="true"></property>
<!--
全局的主键策略 省去了@TableId(value = "id", type = IdType.AUTO)为主键策略 --> <!-- <property name="idType" value="0"></property> -->
<!-- 全局的表前缀策略配置 省略了@TableName("employee") ,如果你的表名为bt1_user --> <!-- <property name="tablePrefix" value="tbl_"></property> --> </bean>
<!--Mapper注册-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xff.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!--事务配置-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mpdemo
jdbc.username=root
jdbc.password=olonn
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
import com.xff.mapper.UserMapper;
import com.xff.pojo.User;
import com.xff.service.UserService;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Date;
import java.util.List;
public class TestMP {
public TestMP() throws BeansException {
}
@Test
public void test() throws BeansException {
ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper=ioc.getBean(UserMapper.class);
UserService userService=ioc.getBean(UserService.class);
System.out.println(userService.selectById(1));
User user=new User();
user.setAge(111);
user.setBirth(new Date());
user.setName("嘻哈哈");
user.setDog("MDOG");
userService.insert(user);
List<User> alist=userService.selectList(null);
System.out.println(alist.get(0).toString()+alist.get(1).toString());
/* new com.xff.Test().selectMM();*/
}
}
打印信息
User{id=1, name='呜哈哈', birth=Wed Jan 09 00:00:00 CST 2019, age=43, dog='tony'}User{id=2, name='嘻哈哈', birth=Wed Jan 09 00:00:00 CST 2019, age=111, dog='MDOG'}
可以看到mybatis plus并不需要我们写对应的mapperxml文件,就能进行数据库访问。
实际上好像是封装了一个mapperProxy代理类,会生成对应的sql语句,只需要继承BaseMapper接口类即可。后续再追详细代码…
# org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 1; 文件提前结束。