SpringBoot+Mybatis框架之@Select注解⽅式搭建
1、创建SpringBoot项⽬
两种⽅式:1、通过idea来New⼀个project项⽬。
l⽂件的配置(其中mysql的maven配置可能会和你本地的不服,会报数据库连接的错误,如果出现这种问题,改⼀下数据库版本就可以):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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-services</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 -->
<!-- mvnrepository/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)
#项⽬的启动端⼝
server.port=9090
#数据库的配置
spring.datasource.url=jdbc:mysql://localhost:3306/DBName?useUnicode=true&characterEncoding=utf-8&useSSL=true spring.datasource.username= USERNAME
spring.datasource.password= PASSWORD
spring.datasource.sql.cj.jdbc.Driver
#显⽰标准输出
logging.level.shyroke.mapper=debug
#路径是是实体类的路径,和写在所有Mapper⽂件中⽤来扫描Mapper⽂件的@Mapper注解联⽤
2、项⽬内部结构
3、项⽬的最⼩功能代码
这个例⼦是实现⼀个简单的查询功能:
Controller层:
package ller;
import st.test.service.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 selectTeacher(@PathVariable int id){
return teacherService.selectTeacher(id).toString();
}
//Select All
@RequestMapping("/selectAll")
public String selectAll(){
return teacherService.selectAll().toString();
}
* 有的朋友想在return的时候直接跳转界⾯去,看到@RestController了吗,这个是ResponseBody和Controller的集合,意思是return的数据都变成json的格式,返回到前端,不会跳转界⾯。
如果想跳转页⾯的话,就需要把RestController改成Controller,就会跳转页⾯了。
* 还有些朋友想带着数据返回到重新定义的页⾯去,对于这种需求,我建议使⽤“视图解释器解析”——ModelAndVIew。ModelAndView的⽤法类似于这样:
@RequestMapping
public ModelAndView list(Model model) {
model.addAttribute("userList", userRepository.listUsers());
model.addAttribute("title", "⽤户管理");
// new ModelAndView(⽬标页⾯的路径, ⽬标页⾯接到的返回值名称, api封装好返回的数据)
return new ModelAndView("users/list","userModel",model);
}
Service层:
package com.st.test.service;
import st.test.domain.Teacher;
import java.util.List;
public interface TeacherService {
Teacher selectTeacher(int id);
springboot框架的作用List<Teacher> selectAll();
}
Service层的实现类,在Service⽂件夹下的Impl⽂件夹中:
st.test.Service.impl;
st.test.Service.TeacherService;
st.test.domain.Teacher;
st.test.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Service
public class TeacherImpl implements TeacherService {
@Autowired
TeacherMapper mapper;
@Override
public Teacher selectTeacher(int id) {
return mapper.selectTeacher(id);
}
@Override
public List<Teacher> selectAll() {
return mapper.selectAll();
}
}
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 selectTeacher(int id);
@Select("SELECT * FROM TEACHER WHERE 1=1")
List<Teacher> selectAll();
}
Domain 实体类:
package st.test.domain;
public class Teacher {
private int id;
private String name;
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(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的⽅式写得太死了,不够灵活。更多的还是使⽤另外两种⽅式。这两天我会再介绍另外两种⽅式。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论