Mybatis和Mybatis-Plus的配置

目录

一、springMVC中Mybatis的配置

1、添加 MyBatis 和 MyBatis-Spring 的依赖

 2、配置数据源

3、配置 MyBatis

4、编写 Mapper 接口和对应的 XML 文件

 二、springnboot里mybatis的yml配置

1、添加 MyBatis 和 MyBatis-Spring-Boot-Starter 依赖

2、配置数据源

3、编写 Mapper 接口和对应的 XML 文件

4、编写对应的 XML 文件 userMapper.xml

三、springmvc里mybatis-plus的配置

1、添加 MyBatis-Plus 的依赖

2、配置数据源

 3、使用 MyBatis-Plus 提供的注解和功能

 4、运行 Spring MVC 

四、springboot里mybatis-plus的配置

1、添加 MyBatis-Plus 的依赖

2、配置数据源

3、使用 MyBatis-Plus 提供的注解和功能

 4、运行 Spring MVC 

控制台显示sql日志


一、springMVC中Mybatis的配置

1、添加 MyBatis 和 MyBatis-Spring 的依赖

<dependencies>
    <!-- MyBatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
    <!-- MyBatis-Spring -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis-spring.version}</version>
    </dependency>
</dependencies>

 2、配置数据源

application.propertiesapplication.yml 中添加数据源的配置。例如,使用 MySQL 数据库的配置示例如下:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
spring.datasource.username=your_username
spring.datasource.password=your_password

3、配置 MyBatis

application-context.xml 中添加 MyBatis 的配置信息,示例如下:

<!-- MyBatis 配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="com.example.entity"/>
    <property name="mapperLocations" value="classpath:mapper/**/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.example.mapper"/>
</bean>

这里的 typeAliasesPackage 指定实体类所在的包名, mapperLocations 指定 Mapper 文件的位置。 MapperScannerConfigurer 用于指定 Mapper 接口所在的包名,这里的 com.example.mapper 是示例的包名,你需要替换为实际使用的包名。

4、编写 Mapper 接口和对应的 XML 文件

定义 Mapper 接口

// 定义 Mapper 接口
public interface UserMapper {
    @Select("SELECT * FROM user WHERE id = #{id}")
    public User getUserById(int id);
}

对应的 XML 文件 userMapper.xml

// 对应的 XML 文件 userMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
    <select id="getUserById" resultType="com.example.entity.User">
        SELECT * FROM user WHERE id = #{id}
    </select>
</mapper>

 二、springnboot里mybatis的yml配置

1、添加 MyBatis 和 MyBatis-Spring-Boot-Starter 依赖

<dependencies>
    <!-- MyBatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
    <!-- MyBatis-Spring-Boot-Starter -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.spring.boot.starter.version}</version>
    </dependency>
</dependencies>

2、配置数据源

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
spring.datasource.username=your_username
spring.datasource.password=your_password

3、编写 Mapper 接口和对应的 XML 文件

// 定义 Mapper 接口
@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user WHERE id = #{id}")
    User getUserById(int id);
}

4、编写对应的 XML 文件 userMapper.xml

// 对应的 XML 文件 userMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
    <select id="getUserById" resultType="com.example.entity.User">
        SELECT * FROM user WHERE id = #{id}
    </select>
</mapper>

三、springmvc里mybatis-plus的配置

1、添加 MyBatis-Plus 的依赖

<dependencies>
    <!-- MyBatis-Plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${mybatis.plus.version}</version>
    </dependency>
</dependencies>

2、配置数据源

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
spring.datasource.username=your_username
spring.datasource.password=your_password

# 配置MyBatis-Plus属性
mybatis-plus.mapper-locations=classpath*:/mapper/**/*Mapper.xml
mybatis-plus.type-enums-package=com.example.enums

 3、使用 MyBatis-Plus 提供的注解和功能

// 定义实体类
@Data
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String username;
    private String password;
    private Integer age;
}
// 定义 Mapper 接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
    // 可以直接使用继承自 BaseMapper 的方法,不需要编写具体的 SQL 语句
}

 4、运行 Spring MVC 

注意:MyBatis-Plus 默认情况下会根据实体类的命名规则映射到数据库中相应的表,如果实体类和表名不一致,可以使用 @TableName 注解进行指定。同时,@TableId 注解指定了主键的生成策略。

四、springboot里mybatis-plus的配置

1、添加 MyBatis-Plus 的依赖

<dependencies>
    <!-- MyBatis-Plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${mybatis.plus.version}</version>
    </dependency>
</dependencies>

2、配置数据源

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_database?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=your_username
spring.datasource.password=your_password

# 配置MyBatis-Plus属性
mybatis-plus.mapper-locations=classpath*:/mapper/**/*Mapper.xml
mybatis-plus.type-enums-package=com.example.enums

3、使用 MyBatis-Plus 提供的注解和功能

// 定义实体类
@Data
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String username;
    private String password;
    private Integer age;
}
// 定义 Mapper 接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
    // 可以直接使用继承自 BaseMapper 的方法,不需要编写具体的 SQL 语句
}

 4、运行 Spring MVC 

注意:MyBatis-Plus 默认情况下会根据实体类的命名规则映射到数据库中相应的表,如果实体类和表名不一致,可以使用 @TableName 注解进行指定。同时,@TableId 注解指定了主键的生成策略。

控制台显示sql日志

 springboot配置mybatis 控制台显示sql日志,在application.properties

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

springboot配置mybatis-plus 控制台显示sql日志,在application.properties

logging.level.com.baomidou.mybatisplus=DEBUG
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

猜你喜欢

转载自blog.csdn.net/qi341500/article/details/132655395