从零学spring boot--集成mybatis--项目--使用注解方式

转载链接:http://412887952-qq-com.iteye.com/blog/2303121

使用IDEA进行开发的:

项目结构:


第一步中在pom.xml加入相关的jar包。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lk</groupId>
    <artifactId>springbootmybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- spring boot 基本环境 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <!--web应用基本环境配置 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
	
	<!--mybatis-spring结合-->
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- 分页 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.1</version> </dependency> </dependencies> <!-- 打包spring boot应用 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>


第二步:生成application,作用是生成application context (应用上下文)

package com.lk.boot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Created by LK on 2016/11/13.
 */
@Configuration   
@EnableAutoConfiguration
@ComponentScan("com.lk.springboot")  //扫描下面的包@service @controller 等
@MapperScan("com.lk.mapper")  //扫面下面的mapper类
public class application {
    public static void main(String[] args) {
        SpringApplication.run(application .class, args);
    }

}
第三步:在数据库中生成表给程序使用。



第四步:配置datasource,在application.properties中

########################################################
###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
package com.lk.springboot.bean;

/**
 * Created by LK on 2016/11/13.
 */

public class Demo {
    private long id;
    private String name;
    private  int sex;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }
}
package com.lk.mapper;

import com.lk.springboot.bean.Demo;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Created by LK on 2016/11/13.
 */

public interface DemoMappper {
    @Select("select *from Demo where name = #{name}")
    public List<Demo> likeName(String name);

    @Select("select *from Demo where id = #{id}")
    public Demo getById(long id);

    @Select("select name from Demo where id = #{id}")
    public String getNameById(long id);
}
第六步配置controller 和 service
package com.lk.springboot.service;

import com.lk.mapper.DemoMappper;
import com.lk.springboot.bean.Demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by LK on 2016/11/13.
 */
@Service
public class DemoService {
    @Autowired
    private DemoMappper demoMappper;

    public List<Demo> likeName(String name){
        return demoMappper.likeName(name);
    }
}
package com.lk.springboot.controller;

import com.github.pagehelper.PageHelper;
import com.lk.springboot.bean.Demo;
import com.lk.springboot.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by LK on 2016/11/13.
 */
@RestController
public class DemoController {
    @Autowired
    private DemoService demoService;

    /*@RequestMapping("/likeName")
    public List<Demo> likeName(String name){
        return demoService.likeName(name);
    }*/

    @RequestMapping("/likeName")
    public List<Demo> likeName(String name) {
        PageHelper.startPage(1, 1);
        return demoService.likeName(name);
    }
}
第7步加一个分页
package configure;

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

import java.util.Properties;

/**
 * Created by LK on 2016/11/13.
 */
@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;
    }

}
测试结果:







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值