龙空技术网

Spring Boot 入门-mybatis-plus的使用

码农角落 125

前言:

如今各位老铁们对“mybatisplus关闭语句打印”可能比较重视,各位老铁们都想要知道一些“mybatisplus关闭语句打印”的相关文章。那么小编在网络上网罗了一些有关“mybatisplus关闭语句打印””的相关知识,希望姐妹们能喜欢,姐妹们快快来学习一下吧!

Spring Boot使用MyBatis-Plus可以简化数据库操作、提供代码生成工具、支持分页查询和条件查询、支持逻辑删除和自动填充,以及提供兼容性和扩展性。这些特性使得开发人员能够更加高效地进行数据库操作,减少重复劳动,提高开发效率。

在这个教程中,将介绍一个简单的Spring Boot项目,并展示如何集成MyBatis-Plus来实现数据库操作

引入依赖包

  <dependency>            <groupId>com.baomidou</groupId>            <artifactId>mybatis-plus-boot-starter</artifactId>            <version>3.5.3.1</version>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>8.0.33</version>        </dependency>
配置数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis-plus-testspring.datasource.username=rootspring.datasource.password=passwordspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
创建实体类和Mapper接口
@TableName("user")@Datapublic class User {    @TableId(value = "id",type = IdType.AUTO)    private Long id;    private String username;    private String email; }

创建一个Mapper接口,用于定义数据库操作的方法。

public interface UserMapper extends BaseMapper<User> {    // 自定义的查询方法可以在此添加}
编写业务逻辑和控制器
@Servicepublic class UserService {    @Resource    private UserMapper userMapper;    public User getUserById(Long id) {        return userMapper.selectById(id);    }    public void createUser(User user) {        userMapper.insert(user);    }    // 其他数据库操作方法}

接下来,创建一个控制器类,用于处理HTTP请求,并调用相应的Service方法。

@RestController@RequestMapping("/users")public class UserController {    @Resource    private UserService userService;    @GetMapping("/{id}")    public User getUser(@PathVariable Long id) {        return userService.getUserById(id);    }    @PostMapping    public void createUser(@RequestBody User user) {        userService.createUser(user);    }    // 其他请求处理方法}
配置扫描包路径

@MapperScan注解是MyBatis或MyBatis-Plus框架中的一个注解,用于扫描指定的包路径,自动注册Mapper接口的实现类。

在MyBatis中,Mapper接口定义了与数据库交互的方法,例如查询、插入、更新等操作。Mapper接口通常没有实际的实现类,而是由MyBatis框架在运行时动态生成其实现类。为了让MyBatis能够找到并实例化这些Mapper接口的实现类,需要在配置文件或代码中显式地指定这些Mapper接口的位置。

而@MapperScan注解的作用就是简化了这个配置的过程。通过在配置类上添加@MapperScan注解并指定Mapper接口所在的包路径,MyBatis或MyBatis-Plus会自动扫描这个包下的所有Mapper接口,并将其注册为Mapper Bean。

@SpringBootApplication@MapperScan("com.example.demo.mapper")public class DemoApplication {	public static void main(String[] args) {		SpringApplication.run(DemoApplication.class, args);	}}

在Spring Boot中使用MyBatis-Plus时,您可以选择使用XML配置文件来定义和执行数据库操作。下面是一个基本的示例,展示了如何通过XML配置文件操作数据库。

创建XML映射文件

首先,创建一个XML映射文件,用于定义数据库操作语句。例如,创建一个名为UserMapper.xml的文件,并将其放置在资源目录(如src/main/resources)下。在该文件中,定义与User实体类对应的数据库操作语句。

<!-- UserMapper.xml --><mapper namespace="com.example.myapp.mapper.UserMapper">    <select id="getUserById" resultType="com.example.myapp.model.User">        SELECT * FROM user WHERE id = #{id}    </select>        <insert id="createUser" parameterType="com.example.myapp.model.User">        INSERT INTO user (username, email) VALUES (#{username}, #{email})    </insert>        <!-- 其他数据库操作语句 -->    </mapper>
配置MyBatis-Plus

在Spring Boot的配置文件(如application.properties或application.yml)中,配置MyBatis-Plus相关的配置项,包括指定XML映射文件的位置和启用XML映射文件的配置。

mybatis-plus:  mapper-locations: classpath*:mapper/**/*Mapper.xml  # 启动时是否检查MyBatis XML文件是否存在  check-config-location: true  global-config:    # 关闭MP3.0自带的banner    banner: false    db-config:      # 全局默认主键类型      id-type: auto      # 逻辑已删除值(默认为 1)      logic-delete-value: 1      # 逻辑未删除值(默认为 0)      logic-not-delete-value: 0      # 默认数据库表下划线命名      table-underline: true      # 最重要的两行      update-strategy: not_empty      insert-strategy: not_empty  configuration:    # 字段名称下划线转驼峰命名    map-underscore-to-camel-case: true   # 使用的数据库ID名称,在多数据库时非常有用   database-id: mysql    # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用    #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl    # 返回类型为Map,显示null对应的字段    call-setters-on-nulls: true    use-actual-param-name: true

标签: #mybatisplus关闭语句打印