第八节:SpringBoot集成MyBatis

pom.xml

<dependency>
		    <groupId>org.mybatis.spring.boot</groupId>
		    <artifactId>mybatis-spring-boot-starter</artifactId>
		    <version>1.3.1</version>
		</dependency>
User实体类

package com.xiaowen.model;
public class User {

	private Integer id;
	private String name;
	private Integer age;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}
UserMapper

package com.xiaowen.mapper;
import org.apache.ibatis.annotations.Mapper;  
import org.apache.ibatis.annotations.Select; 
import com.xiaowen.model.User;
@Mapper
public interface UserMaper {

	@Select("select * from t_user where age = #{age}")  
	User Select(int age); 
}

Controller

package com.xiaowen.controller;

import com.xiaowen.mapper.UserMaper;
import com.xiaowen.model.User;

@RestController
public class WebController {
	
	@Autowired
	private UserMaper userMaper;
	
	@RequestMapping("/user")
	public User selectAge(int age){
		return userMaper.Select(age);
	}
	
}

启动类

package com.xiaowen;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootDemoApplication.class, args);
	}
}
浏览器访问:http://localhost:8088/user?age=10


 Mybatis使用分页插件PageHelper

pom.xml配置

<dependency>  
		    <groupId>com.github.pagehelper</groupId>  
		    <artifactId>pagehelper</artifactId>  
		    <version>4.1.0</version>  
		</dependency>  


package com.xiaowen.util;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.github.pagehelper.PageHelper;

/**
 * 注册MyBatis分页插件PageHelper
 * @author xiaowen
 *
 */
@Configuration
public class MybatisConf {
    @Bean
	public PageHelper 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;  
	}
}

Controller

@RequestMapping("/user")
	public User selectAge(int age){
		//第一个参数是第几页;第二个参数是每页显示条数。
		PageHelper.startPage(1, 2);
		return userMaper.Select(age);
	}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值