[四]Spring Boot 整合Mybatis

来源:互联网 发布:老版本mac如何升级系统 编辑:程序博客网 时间:2024/06/06 02:41

添加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);


原创粉丝点击