springboot集成mybatis项目搭建
2021/12/14 23:47:06
本文主要是介绍springboot集成mybatis项目搭建,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1新建项目
2选择jdk版本,和包名
3选择添加依赖
4查看pom文件,并添加resouces插件
<!--resources插件--> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource>
最终文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.hry</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </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> </dependencies> <build> <!--resources插件--> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
5配置application.properties文件
#设置端口号 server.port=8080 #设置访问应用上下文路径, contextpath server.servlet.context-path=/ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root spring.datasource.password=admin spring.datasource.url=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&allowMultiQueries=true #druid配置 #spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #mybaits配置 #指定mapper文件的位置 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com/hry/pojo #指定mybatis的日志 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
备注此文件可改成yml文件
6创建项目结构
7然后在启动行中添加@MapperScan(basePackages ="com.hry.mapper")扫描包
package com.hry; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan(basePackages ="com.hry.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
8启动运行
备注:项目结构中的各类的代码,注意要创建在对应的包下
BooksController
package com.hry.controller; import com.hry.pojo.Books; import com.hry.service.BooksService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("books") public class BooksController { @Autowired BooksService booksService; @RequestMapping("selectByid") public Books selectById(@RequestParam("cid") Integer id){ return booksService.selectById(id); } }
BooksMapper
package com.hry.mapper; import com.hry.pojo.Books; public interface BooksMapper { int deleteByPrimaryKey(Integer bookid); int insert(Books record); int insertSelective(Books record); Books selectByPrimaryKey(Integer bookid); int updateByPrimaryKeySelective(Books record); int updateByPrimaryKey(Books record); }
Books
package com.hry.pojo; public class Books { private Integer bookid; private String bookname; private Integer bookcounts; private String detail; public Integer getBookid() { return bookid; } public void setBookid(Integer bookid) { this.bookid = bookid; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname == null ? null : bookname.trim(); } public Integer getBookcounts() { return bookcounts; } public void setBookcounts(Integer bookcounts) { this.bookcounts = bookcounts; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail == null ? null : detail.trim(); } }
BooksService
package com.hry.service; import com.hry.pojo.Books; public interface BooksService { Books selectById(Integer id); }
BooksServiceimpl
package com.hry.service.impl; import com.hry.mapper.BooksMapper; import com.hry.pojo.Books; import com.hry.service.BooksService; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class BooksServiceImpl implements BooksService { @Resource BooksMapper booksMapper; @Override public Books selectById(Integer id) { return booksMapper.selectByPrimaryKey(id); } }
数据库代码,建立ssmbuild的数据库,运行下面代码
-- ---------------------------- -- Table structure for `books` -- ---------------------------- DROP TABLE IF EXISTS `books`; CREATE TABLE `books` ( `bookID` int(10) NOT NULL AUTO_INCREMENT COMMENT '书id', `bookName` varchar(100) NOT NULL COMMENT '书名', `bookCounts` int(11) NOT NULL COMMENT '数量', `detail` varchar(200) NOT NULL COMMENT '描述', PRIMARY KEY (`bookID`), KEY `bookID` (`bookID`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of books -- ---------------------------- INSERT INTO `books` VALUES ('1', 'Java', '1', '从入门到放弃'); INSERT INTO `books` VALUES ('2', 'MySQL', '10', '从删库到跑路'); INSERT INTO `books` VALUES ('3', 'Linux', '5', '从进门到进牢'); INSERT INTO `books` VALUES ('6', 'hjfhfg', '123', '东方闪');
这篇关于springboot集成mybatis项目搭建的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-30我的第一个Go命令行工具
- 2024-09-30初学者指南:轻松掌握模块化编程
- 2024-09-30顶级5款免费的IntelliJ插件,助你Java开发之路更顺畅
- 2024-09-30提高应用程序可用性:冗余和持久性
- 2024-09-30Twitter 系统设计面试示例
- 2024-09-30JSON对象入门教程:轻松掌握基础用法
- 2024-09-30封装入门:Java面向对象编程的第一步
- 2024-09-30后台交互入门:轻松掌握基础知识与实践技巧
- 2024-09-30轻松入门:后台交互教程详解
- 2024-09-30后台交互项目实战:新手指南