SpringBoot day02

org.springframework.boot spring-boot-starter-web
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>


    <!-- mysql 数据库驱动. -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>


    <!--
                spring-boot mybatis依赖:

                请不要使用1.0.0版本,因为还不支持拦截器插件,
                1.1.1 是博主写帖子时候的版本,大家使用最新版本即可
             -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>

    <!--
	MyBatis提供了拦截器接口,我们可以实现自己的拦截器,
	将其作为一个plugin装入到SqlSessionFactory中。
	Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。
	Github项目地址: https://github.com/pagehelper/Mybatis-PageHelper
 -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>4.1.0</version>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!--                如果依赖父亲是spring-boot-starter-parent(不是多模块),就不需要添加,在里面已经配置了-->
            <configuration>
                <mainClass>cn.bq.App</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

application.yml

server:
 port: 80
spring:
 datasource:
  url: jdbc:mysql://localhost:3306/p1
  username: root
  password: 123456
  driver-class-name: com.mysql.jdbc.Driver
mybatis:
 type-aliases-package: cn.bq.domain,cn.bq.query

mapper


public interface UserMapper {

    //    @Delete()
//    @Update()
    @Insert("insert into t_user (name) values(#{name})")
    //@ResultMap()
    //自动递增注释
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void save(User user);

    @Select("select * from t_user")
    List<User> loadAll();
}

查询的时候设置参数
PageHelper.startPage(2,2);

MybatisConfig


import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class MyBatisConfiguration {

    @Bean
    public PageHelper pageHelper() {
        System.out.println("MyBatisConfiguration.pageHelper()");
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值