写点什么

springBoot 集成 Mybatis,Java 资料下载

作者:Java高工P7
  • 2021 年 11 月 10 日
  • 本文字数:2433 字

    阅读完需:约 8 分钟

<artifactId>springBoot-mybatis</artifactId>


<version>1.0-SNAPSHOT</version>


<parent>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-parent</artifactId>


<version>2.1.6.RELEASE</version>


</parent>


<properties>


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>


<maven.compiler.source>1.8</maven.compiler.source>


<maven.compiler.target>1.8</maven.compiler.target>


</properties>


<dependencies>


<dependency>


<groupId>junit</groupId>


<artifactId>junit</artifactId>


<version>4.11</version>


<scope>test</scope>


</dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-web</artifactId>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-devtools</artifactId>


<optional>true</optional>


<scope>true</scope>


</dependency>


<dependency>


<groupId>javax.servlet</groupId>


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


<artifactId>javax.servlet-api</artifactId>


<scope>provided</scope>


</dependency>


<dependency>


<groupId>javax.servlet</groupId>


<artifactId>jstl</artifactId>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-tomcat</artifactId>


<scope>provided</scope>


</dependency>


<dependency>


<groupId>org.apache.tomcat.embed</groupId>


<artifactId>tomcat-embed-jasper</artifactId>


<scope>provided</scope>


</dependency>


<dependency>


<groupId>mysql</groupId>


<artifactId>mysql-connector-java</artifactId>


</dependency>


<dependency>


<groupId>org.mybatis.spring.boot</groupId>


<artifactId>mybatis-spring-boot-starter</artifactId>


<version>2.0.0</version>


</dependency>


<!--


MyBatis 提供了拦截器接口,我们可以实现自己的拦截器,


将其作为一个 plugin 装入到 SqlSessionFactory 中。


Github 上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。


Github 项目地址: https://github.com/pagehelper/Mybatis-PageHelper


-->


<dependency>


<groupId>com.github.pagehelper</groupId>


<artifactId>pagehelper</artifactId>


<version>4.2.1</version>


</dependency>


</dependencies>


<build>


<finalName>spring-web</finalName>


<plugins>


<plugin>


<artifactId>maven-clean-plugin</artifactId>


<version>3.1.0</version>


</plugin>


<plugin>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-maven-plugin</artifactId>


<configuration>


<fork>true</fork>


</configuration>


</plugin>


</plugins>


</pluginManagement>


</build>


</project>


2.添加配置文件


==============


########################################################


###datasource


########################################################


#后面加的一长串是为了解决乱码


spring.datasource.url = jdbc:mysql://localhost:3306/springboot?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC


spring.datasource.username = root


spring.datasource.password = Root@123


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


3.创建持久化 bean


=================


package com.huawei.beans;


public class Student {


private Integer id;


private String name;


public Student() {


}


public Student(String name) {


this.name = name;


}


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;


}


@Override


public String toString() {


return "Student{" + "id=" + id + ", name='" + name + ''' + '}';


}


}


4.创建 dao


=============


package com.huawei.mapper;


import com.huawei.beans.Student;


import org.apache.ibatis.annotations.Select;


import java.util.List;


public interface StudentMapper {


@Select("select * from student where name = #{name}")


public List<Student> likeName(String name);


@Select("select *from student where id = #{id}")


public Student getById(long id);


@Select("select name from student where id = #{id}")


public String getNameById(long id);


}


5.创建 service


=================


package com.huawei.service;


import com.huawei.beans.Student;


import com.huawei.mapper.StudentMapper;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;


import java.util.List;


@Service


public class StudentService {


@Autowired


private StudentMapper studentMapper;


public List<Student> likeName(String name) {


return studentMapper.likeName(name);


}


}


6.创建 controller


====================


package com.huawei.controller;


import com.github.pagehelper.PageHelper;


import com.huawei.beans.Student;


import com.huawei.service.StudentService;


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;


@RestController


public class StudentController {


@Autowired


StudentService studentService;


@RequestMapping("/likeName")


public List<Student> likeName(String name){


PageHelper.startPage(1,2);


return studentService.likeName(name);


}


}

用户头像

Java高工P7

关注

还未添加个人签名 2021.11.08 加入

还未添加个人简介

评论

发布
暂无评论
springBoot集成Mybatis,Java资料下载