SpringBoot+Mybatis框架之@Select注解⽅式搭建
最近两天在帮同学搭建SpringBoot框架,我以往使⽤的是xml映射⽂件的⽅式,这次我的同学要我使⽤@Select注解的⽅式搭建的⼀次。感觉挺有意思的,分享给⼤家。
1、创建SpringBoot项⽬
两种⽅式:1、通过idea来New⼀个project项⽬。
l⽂件的配置(其中mysql的maven配置可能会和你本地的不服,会报数据库连接的错误,如果出现这种问题,改⼀下数据库版本就可以):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="maven.apache/POM/4.0.0" xmlns:xsi="www.w3/2001/XMLSchema-instance"
xsi:schemaLocation="maven.apache/POM/4.0.0 maven.apache/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId&le</groupId>
春季开的花
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
哈尼贝贝>蒙氏数学<groupId>org.springframework.boot</groupId>
难忘的瞬间<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-rvices</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--集成⽇志logback+slf4j -->
<!-- /artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
<!--<packaging>jar</packaging>-->
<build>
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties⽂件中的配置项(端⼝、数据库配置、debug、mybatis)
#项⽬的启动端⼝
rver.port=9090
#数据库的配置
spring.datasource.url=jdbc:mysql://localhost:3306/DBName?uUnicode=true&characterEncoding=utf-8&uSSL=true spring.datasource.urname= USERNAME
spring.datasource.password= PASSWORD
spring.datasource.sql.cj.jdbc.Driver
#显⽰标准输出
shyroke.mapper=debug
#路径是是实体类的路径,和写在所有Mapper⽂件中⽤来扫描Mapper⽂件的@Mapper注解联⽤
2、项⽬内部结构
3、项⽬的最⼩功能代码
这个例⼦是实现⼀个简单的查询功能:
Controller层:
package ller;
import st.test.rvice.TeacherService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController
public class TeacherController {
@Autowired
private TeacherService teacherService;
代父从军
麂皮绒怎么清洗
//Select By Id
@RequestMapping("/showTeacher/{id}")
public String lectTeacher(@PathVariable int id){
return teacherService.lectTeacher(id).toString();
}
//Select All
@RequestMapping("/lectAll")
public String lectAll(){
return teacherService.lectAll().toString();
}
* 有的⼩朋友想在return的时候直接跳转界⾯去,看到@RestController了吗,这个是ResponBo
dy和Controller的集合,意思是return的数据都变成json的格式,返回到前端,不会跳转界⾯。
如果想跳转页⾯的话,就需要把RestController改成Controller,就会跳转页⾯了。
* 还有些朋友想带着数据返回到重新定义的页⾯去,对于这种需求,我建议使⽤“视图解释器解析”——ModelAndVIew。ModelAndView的⽤法类似于这样:
@RequestMapping
public ModelAndView list(Model model) {
model.addAttribute("urList", urRepository.listUrs());
model.addAttribute("title", "⽤户管理");
// new ModelAndView(⽬标页⾯的路径, ⽬标页⾯接到的返回值名称, api封装好返回的数据)
关心则乱return new ModelAndView("urs/list","urModel",model);
}竞职演讲
Service层:
package com.st.test.rvice;
import st.test.domain.Teacher;
import java.util.List;
public interface TeacherService {
Teacher lectTeacher(int id);
List<Teacher> lectAll();
}
Mapper 相当于DAO层:
package com.st.test.mapper;
import st.test.domain.Teacher;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface TeacherMapper {
@Select ("SELECT * FROM Teacher WHERE id = #{id}")
Teacher lectTeacher(int id);
@Select("SELECT * FROM TEACHER WHERE 1=1")
List<Teacher> lectAll();
}
Domain 实体类:
package st.test.domain;
public class Teacher {
private int id;
private String name;
public void tId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void tName(String name) {
this.name = name;
}
public int getId() {
return id;
}
@Override
public String toString() {
return "Teacher{" +"id=" + id +", name='" + name + '\'' +'}';
}
}
这样就能实现@Select标签的⽅式搭建SpringBoot的Mybatis框架了。挺简单的,但是我不太喜欢⽤这种当时,Mybatis的继承⽅式三种:@Select、@Provider、还有xml映射⽂件的⽅式。
@Select的⽅式写得太死了,不够灵活。更多的还是使⽤另外两种⽅式。这两天我会再介绍另外两种⽅式。