赞
踩
愿景
我们的愿景是成为 MyBatis 最好的搭档,就像魂斗罗中的 1P、2P,基友搭配,效率翻倍
MyBatis-Plus官方推荐使用Spring Boot,在此我们以Spring整合MyBatis为基础,再加入MyBatis-plus,以此来学习MyBatis-Plus相关内容
CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL COMMENT '主键ID',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
<packaging>jar</packaging>
<properties> <spring.version>5.3.15</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <!-- junit测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> <!-- 日志 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.36</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.10</version> </dependency> <!-- lombok用来简化实体类 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency> <!--MyBatis-Plus的核心依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.5.1</version> </dependency> </dependencies>
Spring整合MyBatis,需要MyBatis以及Spring整合MyBatis的依赖
但是,在以上的依赖列表中,并没有MyBatis以及Spring整合MyBatis的依赖,因为当我们引入了MyBatis-Plus的依赖时,就可以间接的引入这些依赖
并且依赖和依赖之间的版本必须兼容,所以我们不能随便引入其他版本的依赖,以免发生冲突,在官网上有明确提示
public class User { private Long id; private String name; private Integer age; private String email; public User() { } public User(Long id, String name, Integer age, String email) { this.id = id; this.name = name; this.age = age; this.email = email; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", email='" + email + '\'' + '}'; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="jdbc.properties"/> <typeAliases> <package name=""/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <package name=""/> </mappers> </configuration>
public interface TestMapper {
/**
* 获取所有用户
*
* @return {@link List}<{@link User}>
*/
List<User> getAllUser();
}
<mapper namespace="com.atguigu.mp.mapper.TestMapper">
<!--SQL片段,记录基础字段-->
<sql id="BaseColumns">id,name,age,email</sql>
<!--List<User> getAllUser();-->
<select id="getAllUser" resultType="User">
select <include refid="BaseColumns"></include> from user
</select>
</mapper>
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=123456
<?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 https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 引入jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!-- 配置Druid数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置用于创建SqlSessionFactory的工厂bean --> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 设置MyBatis配置文件的路径(可以不设置) --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!-- 设置数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 设置类型别名所对应的包 --> <property name="typeAliasesPackage" value="com.atguigu.mp.pojo"></property> <!-- 设置映射文件的路径 若映射文件所在路径和mapper接口所在路径一致,则不需要设置 --> <!-- <property name="mapperLocations" value="classpath:mapper/*.xml"></property> --> </bean>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。