<!--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>
#设置端口号 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文件
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); } }
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', '东方闪');