SpringBoot + MyBatis +MySQL8 +Druid数据库连接池,基于注解实现查询功能解析

哈哈 阅读:244 2021-03-31 20:58:23 评论:0

1、整体项目结构:

功能模块的依赖说明:

mysql-boot :springboot基础环境依赖
mysql-boot-api :无
mysql-boot-dao :依赖 mysql-boot-api 和mybatis-spring-boot-starter(springboot与mybatis 集成)
mysql-boot-service:依赖api 、dao、数据库连接池(druid)、和mysql驱动

第一、依赖pom.xml

mysql-boot的pom.xml 文件

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <groupId>com.zzg</groupId> 
  <artifactId>mysql-boot</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
  <packaging>pom</packaging> 
   
  <parent> 
		<groupId>org.springframework.boot</groupId> 
		<artifactId>spring-boot-starter-parent</artifactId> 
		<version>2.1.2.RELEASE</version> 
	</parent> 
	 
	<dependencies> 
		<!--web 模块依赖 --> 
		<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter-web</artifactId> 
		</dependency> 
		<!--web 热部署 --> 
		<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-devtools</artifactId> 
			<optional>true</optional> 
		</dependency> 
		<!--web 单元测试 --> 
		<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter-test</artifactId> 
			<scope>test</scope> 
		</dependency> 
	</dependencies> 
	<modules> 
	<module>mysql-boot-api</module> 
	<module>mysql-boot-service</module> 
	<module>mysql-boot-web</module> 
	<module>mysql-boot-dao</module> 
	</modules> 
</project>

mysql-boot-dao的pom.xml 文件

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <parent> 
    <groupId>com.zzg</groupId> 
    <artifactId>mysql-boot</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
  </parent> 
  <artifactId>mysql-boot-dao</artifactId> 
  <dependencies> 
  		<!--依赖api  --> 
  		<dependency> 
			<groupId>com.zzg</groupId> 
			<artifactId>mysql-boot-api</artifactId> 
			<version>0.0.1-SNAPSHOT</version> 
		</dependency> 
		<!--springboot 与 mybatis 集成 --> 
		<dependency> 
			<groupId>org.mybatis.spring.boot</groupId> 
			<artifactId>mybatis-spring-boot-starter</artifactId> 
			<version>1.3.2</version> 
		</dependency> 
  </dependencies> 
</project>

mysql-boot-service的pom.xml 文件

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
	<modelVersion>4.0.0</modelVersion> 
	<parent> 
		<groupId>com.zzg</groupId> 
		<artifactId>mysql-boot</artifactId> 
		<version>0.0.1-SNAPSHOT</version> 
	</parent> 
	<artifactId>mysql-boot-service</artifactId> 
 
	<dependencies> 
		<!--api 层依赖  --> 
		<dependency> 
			<groupId>com.zzg</groupId> 
			<artifactId>mysql-boot-api</artifactId> 
			<version>0.0.1-SNAPSHOT</version> 
		</dependency> 
		<!--dao 层依赖  --> 
		<dependency> 
			<groupId>com.zzg</groupId> 
			<artifactId>mysql-boot-dao</artifactId> 
			<version>0.0.1-SNAPSHOT</version> 
		</dependency> 
		<!-- 数据库连接池druid --> 
		<dependency> 
			<groupId>com.alibaba</groupId> 
			<artifactId>druid-spring-boot-starter</artifactId> 
			<version>1.1.10</version> 
		</dependency> 
		<!--mysql 驱动程序 --> 
		<dependency> 
			<groupId>mysql</groupId> 
			<artifactId>mysql-connector-java</artifactId> 
			<version>8.0.12</version> 
		</dependency> 
	</dependencies> 
</project>

第二、核心代码文件:

mysql-boot-api:实体和接口文件

package com.zzg.entity; 
 
import java.io.Serializable; 
 
public class Book implements Serializable { 
 
	/**    
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)    
	 */    
	private static final long serialVersionUID = 1L; 
	 
	private Long id; 
	 
	private String name; 
 
	public Long getId() { 
		return id; 
	} 
 
	public void setId(Long id) { 
		this.id = id; 
	} 
 
	public String getName() { 
		return name; 
	} 
 
	public void setName(String name) { 
		this.name = name; 
	} 
}
package com.zzg.api; 
 
import java.util.List; 
import com.zzg.entity.Book; 
 
public interface BookService { 
	 List<Book> getBooks(); 
} 

mysql-boot-dao:mapper 注解配置文件

package com.zzg.mapper; 
 
import java.util.List; 
import org.apache.ibatis.annotations.Select; 
import com.zzg.entity.Book; 
 
public interface BookMapper { 
	@Select("select * from book where 1=1") 
	List<Book> getBooks(); 
} 

mysql-boot-service:程序入口、服务实现和接口测试

package com.zzg; 
 
import org.mybatis.spring.annotation.MapperScan; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
 
@SpringBootApplication 
@MapperScan("com.zzg.mapper") 
public class Application { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		SpringApplication.run(Application.class, args); 
	} 
 
} 
package com.zzg.service; 
 
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
 
import com.zzg.api.BookService; 
import com.zzg.entity.Book; 
import com.zzg.mapper.BookMapper; 
 
@Service 
public class BookServiceImpl implements BookService{ 
	@Autowired 
	private BookMapper mapper; 
 
	@Override 
	public List<Book> getBooks() { 
		// TODO Auto-generated method stub 
		return mapper.getBooks(); 
	} 
 
} 
package com.zzg.controller; 
 
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 
 
import com.zzg.api.BookService; 
import com.zzg.entity.Book; 
 
@Controller 
@RequestMapping 
public class BookController { 
	@Autowired 
	private BookService service; 
	 
	@RequestMapping(value = "/books", method = { RequestMethod.GET }) 
	@ResponseBody 
	public List<Book> getBooks() { 
		List<Book> result = service.getBooks(); 
		return result; 
	} 
} 

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们