【SpringBoot(二)之SpringBoot集成Mybatis】

本文详细介绍了如何在Spring Boot项目中集成MyBatis框架,包括pom文件依赖导入、application.properties配置、实体类定义、Mapper接口及XML文件编写、Service与Controller实现,并通过一个简单的例子验证了配置的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、pom文件

导入myatis依赖

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>

数据库依赖

<!--数据库-->
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <scope>runtime</scope>
         </dependency>
         <dependency>
             <groupId>org.mybatis.spring.boot</groupId>
             <artifactId>mybatis-spring-boot-starter</artifactId>
             <version>1.3.2</version>
         </dependency>
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>druid-spring-boot-starter</artifactId>
             <version>1.1.9</version>
         </dependency>

数据源依赖

        <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>druid-spring-boot-starter</artifactId>
             <version>1.1.9</version>
         </dependency>

2、application.properties

## 端口
server.port=8888

##项目名
server.servlet.context-path=/
##数据库连接
spring.datasource.url=jdbc:mysql://localhost:3306/kc?useUnicode=true&characterEncoding=utf8
spring.datasource.name=root
spring.datasource.password=12345678
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
##打印到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
##mybatis配置
mybatis.type-aliases-package=com.kc
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.typeAliasesPackage=com.kc.*.*.entity
#使用驼峰命名法转换字段。
mybatis.configuration.map-underscore-to-camel-case=true
#使全局的映射器启用或禁用缓存。
mybatis.configuration.cache-enabled=true
#全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。
mybatis.configuration.lazy-loading-enabled=true
#当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。
mybatis.configuration.aggressive-lazy-loading=true
#是否允许单条sql 返回多个数据集  (取决于驱动的兼容性) default:true
mybatis.configuration.multiple-result-sets-enabled=true
#是否可以使用列的别名 (取决于驱动的兼容性) default:true
mybatis.configuration.use-column-label=true
#允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。  default:false
mybatis.configuration.use-generated-keys=true
#指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射\u3000PARTIAL:部分  FULL:全部
mybatis.configuration.auto-mapping-behavior=partial
#这是默认的执行类型  (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新)
mybatis.configuration.default-executor-type=simple
#设置本地缓存范围 session:就会有数据的共享  statement:语句范围 (这样就不会有数据的共享 ) defalut:session
mybatis.configuration.local-cache-scope=session
#设置但JDBC类型为空时,某些驱动程序 要指定值,default:OTHER,插入空值时不需要指定类型
mybatis.configuration.jdbc-type-for-null=null
#如果数据为空的字段,则该字段省略不显示,可以通过添加配置文件,规定查询数据为空是则返回null。
mybatis.configuration.call-setters-on-nulls=true
## 连接池配置
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=20
spring.datasource.druid.max-wait=30000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.test-on-return=false
spring.datasource.druid.validation-query=SELECT 'x'
##spring.datasource.druid.pool-prepared-statements=false
##spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.druid.remove-abandoned=true
spring.datasource.druid.remove-abandoned-timeout=1800
spring.datasource.druid.filters=stat,wall
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.reset-enable=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.aop-patterns=com.kc.*.service.*

3、配置完以后做个测试

实体类:

public class UserEntity {
    private String id;
    private String name;
    private String sex;
    private Integer age;
    private String phone;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

Mapper

@Mapper
public interface UserMapper  {
    List<UserEntity> selectList();
}

Mapper.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.kc.admin.mapper.UserMapper">
    <select id="selectList" resultType="com.kc.admin.entity.UserEntity">
        SELECT * from user
    </select>
</mapper>

service

public interface UserService {
    List<UserEntity> list();
}

serviceImpl

@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserEntity> list() {
        return userMapper.selectList();
    }
}

controller

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/w")
    public List<UserEntity> Hello(){
       List<UserEntity> list = userService.list();
        return list;
    }
}

访问结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值