Mybatis-Plus:快速开始(Spring + Mybatis + Mabatis-Plus)
Posted CodeJiao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis-Plus:快速开始(Spring + Mybatis + Mabatis-Plus)相关的知识,希望对你有一定的参考价值。
01:Mybatis-Plus:了解Mybatis-Plus、快速开始(Mybatis + Mybatis-Plus,Mybatis-Plus自动做了属性映射)
02:Mybatis-Plus:快速开始(Spring + Mybatis + Mabatis-Plus)
03:Mybatis-Plus:快速开始(SpringBoot + Mybatis + Mybatis)
1. Spring + Mybatis + Mabatis-Plus
引入了Spring框架,数据源、构建等工作就交给了Spring管理。
1.1 创建子Module
1.2 引入Spring相关依赖
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">
<parent>
<artifactId>mybatis_plus_project</artifactId>
<groupId>com.tian</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis_plus_spring_project</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.version>5.1.6.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>$spring.version</version>
</dependency>
</dependencies>
</project>
1.3 配置数据库连接信息
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
jdbc.username=root
jdbc.password=317525
1.4 编写User对象和UserMapper接口
User.java
package pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User
private Long id;
private String userName;
private String password;
private String name;
private Integer age;
private String email;
UserMapper.java
package mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import pojo.User;
import java.util.List;
public interface UserMapper extends BaseMapper<User>
List<User> findAll();
1.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"
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"/>
<!-- 定义数据源 -->
<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"/>
</bean> <!--扫描mapper接口,使用的依然是Mybatis原生的扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"/>
</bean>
</beans>
1.6 编写测试文件
TestMybatisSpring.java
import mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import pojo.User;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestMybatisSpring
@Autowired
private UserMapper userMapper;
@Test
public void testSelectList()
List<User> users = this.userMapper.selectList(null);
for (User user : users)
System.out.println(user);
运行结果: 我们发现报错了,无法解析$jdbc.url
报错原因:
我们把resources
粘贴到test目录尽可以正常运行
1.7 补充说明:如果需要详细的日志信息
请加上log4j的配置文件
log4j.properties
log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n
以上是关于Mybatis-Plus:快速开始(Spring + Mybatis + Mabatis-Plus)的主要内容,如果未能解决你的问题,请参考以下文章
Mybatis-Plus:了解Mybatis-Plus快速开始(Mybatis + Mybatis-Plus,Mybatis-Plus自动做了属性映射)
MyBatis-Plus学习——Springboot快速开始