Spring Boot 整合Mybatis Annotation

和上一章一样创建一个Spring Boot项目

修改application.yml

server:
  port: 8099
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&&characterEncoding=utf8
    username: root
    password: u2tc7c/F8N31S
mybatis:
  type-aliases-package: com.wya.springboot.entity
  

创建实体类、Mapper接口、业务层、Controller层


Mapper接口代码

public interface StudentMapper {

	@Select("select * from t_student")
	@Results({
		@Result(property="studentId",column="student_id"),
		@Result(property="studentName",column="student_name"),
		@Result(property="studentAge",column="student_age")
	})
	List<Student> findAll();
	
	@Select("select * from t_student where student_id = #{id}")
	@Results({
		@Result(property="studentId",column="student_id"),
		@Result(property="studentName",column="student_name"),
		@Result(property="studentAge",column="student_age")
	})
	Student findById(Integer id);
}

Controller层代码

@RestController
public class StudentController {

	@Resource
	private StudentService studentService;
	
	@GetMapping("student")
	public List<Student> findAll(){
		return this.studentService.findAll();
	}
	
	@GetMapping("student/{id}")
	public Student findById(@PathVariable("id") Integer id){
		return this.studentService.findById(id);
	}
	
}

启动应用类代码

@SpringBootApplication
@MapperScan("com.wya.springboot.dao")
public class SpringbootMybatisAnnotationApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootMybatisAnnotationApplication.class, args);
	}
}

启动应用


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值