[四]Spring Boot 整合Mybatis

添加pom.xml中的架包

在pom.xm文件中的dependencys标签中添加

1)        数据库驱动包:例如mysql

<dependency>

        <groupId>mysql</groupId>

        <artifactId>mysql-connector-java</artifactId>

    </dependency>

2)        添加mybatis依赖包

<dependency>

        <groupId>org.mybatis.spring.boot</groupId>

        <artifactId>mybatis-spring-boot-starter</artifactId>

        <version>1.1.1</version>

    </dependency>

3)        添加mybatis的分页拦截器依赖(使用的是github开发者写的一个分页插件:git地址:https://github.com/pagehelper/Mybatis-PageHelper)

<dependency>

        <groupId>com.github.pagehelper</groupId>

        <artifactId>pagehelper</artifactId>

        <version>4.1.0</version>

    </dependency>  

添加配置文件

在配置文件application.properties中添加配置信息(如果不存在需要创建,需要放在src/main/resources文件中)

1)        添加数据库的配置信息

########################################################

###datasource

########################################################

spring.datasource.url = jdbc:mysql://localhost:3306/test

spring.datasource.username = root

spring.datasource.password = root

spring.datasource.driverClassName = com.mysql.jdbc.Driver

spring.datasource.max-active=20

spring.datasource.max-idle=8

spring.datasource.min-idle=8

spring.datasource.initial-size=10

编写Mapper接口

public interfaceUserInfoDao {

   

    @Select("select * from userinfo where id=#{id}")

    public UserInfogetUserInfoById(Integerid);

   

    @Insert("insert into userinfo(name) values(#{name})")

    public void addUserinfo(UserInfo userinfo);

   

@Select("select *from userinfo ")

    public List<UserInfo>getUserInfos();

}

 

加入mybatis的mapper信息的扫描

在启动类上加上扫描

@SpringBootApplication

@MapperScan("com.sb.dao")//扫描mbatismapper信息

public classAPP {

    public static void main(String[] args) {

       

        SpringApplication.run(APP.class,args);

    }

   

}

加入PageHelper

1.修改SpringBoot启动类添加分页插件

    @SpringBootApplication

@MapperScan("com.sb.dao")//扫描mbatismapper信息

public class APP {

   

    //========================addpage begin

    @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);

        returnpageHelper;

    }

    //========================addpage end

 

        public static void main(String[] args) {

       

            SpringApplication.run(APP.class,args);

        }

   

}

2.在使用查询的前面添加代码:

    @RequestMapping("getUserInfos")

    public List<UserInfo>getUserInfos(){

        PageHelper.startPage(2,5);//设置分页信息

        returnUserInfoDao.getUserInfos();

    }

实现自增长列

在mapper接口中的insert方法上添加注解

@Insert("insertinto userinfo(name) values(#{name})")

    @Options(useGeneratedKeys=true,keyColumn="id",keyProperty="id")//获取自增长列的值

    public void addUserinfo(UserInfo userinfo);


自定义主键ID

@Insert("insert into class (id,name) values(#{id},#{name})")
@SelectKey(statement = "select uuid_short()",before = true,resultType = String.class,keyProperty = "id",keyColumn = "id")
public void save(Class c);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值