玩转SpringBoot整合Mybatis连接访问MySQL数据库

本文详细介绍了如何使用SpringBoot框架整合MyBatis,包括搭建项目环境、配置数据源、编写实体类、Mapper接口及Service层,最终实现数据库操作。通过具体的代码示例,展示了从项目初始化到数据读写的全过程。
摘要由CSDN通过智能技术生成

在这里插入图片描述
步骤1.导入相关的依赖

<?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 http://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.1.5.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.suke</groupId>
	<artifactId>springboot_1-0</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot_1-0</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.43</version>
		</dependency>
		<!--导入依赖-->
      <!-- 数据库连接池: druid -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.3</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<!-- spring-boot mabatis依赖 不要使用1.0.0版本,因为1.0.0版本还不支持拦截器插件 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.2.0</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

步骤2.创建启动类App.java

package com.example.controller;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication 
@ComponentScan(basePackages="org.suke")//扫描注解
@MapperScan(basePackages="org.suke.mapper") //扫描Mapper接口,自动生成Mapper的代理对象
public class App extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);

	}

	
	 @Override
	 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
	      return application.sources(App.class);
	 }

}

步骤3.在application.properties添加配置文件

################################
####DataSource
################################
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/house?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
druid.maxSize=100
druid.minSize=5
druid.initSize=5
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#日志文件
logging.config=classpath:logConfig.xml
#激活那个环境
spring.profiles.active=dev
#Mybatis配置
mybatis.type-aliases-package=com.zl.app.entity
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml

步骤4.编写实体类

@Data
public class Employee {
	private Integer empno;
	private String ename;
	private String job;
	private Integer mgr;
	private Date hiredate;
	private Double sal;
	private Double comm;
}

步骤5.编写Mapper接口

@Mapper
public interface EmployeeMapper {
	public List<Employee> selectByPage(@Param("startNo")int startNo, @Param("pageSize")int pageSize) throws Exception;
	public void add(Employee emp) throws Exception;
}

步骤6.编写Service

public interface IEmployeeService {
	public List<Employee>  queryByPage(int pageIndex,int pageSize) throws Exception;
	public void addEmp(Employee emp) throws Exception;
}
@Service("empService")
public class EmployeeServiceImpl implements IEmployeeService {
	@Autowired
	private EmployeeMapper empDap;
	
	@Override
	public List<Employee> queryByPage(int pageIndex, int pageSize) throws Exception {
		return empDap.selectByPage((pageIndex-1)*pageSize, pageSize);
	}

	@Transactional
	@Override
	public void addEmp(Employee emp) throws Exception {
		empDap.add(emp);
	}
}

编写Mapper映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper  
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.zl.app.dao.EmployeeMapper">
 	<select id="selectByPage" resultType="Employee">
 		select * from  emp limit #{startNo},#{pageSize}
 	</select>
 	
 	<insert id="add" parameterType="Employee">
 		INSERT  INTO `emp`(`empno`,`ename`,`job`,`mgr`,`hiredate`,`sal`,`comm`) 
 		   VALUE(#{empno,jdbcType=INTEGER},#{ename,jdbcType=VARCHAR},#{job,jdbcType=VARCHAR}
 		   ,#{mgr,jdbcType=INTEGER},#{hiredate,jdbcType=DATE},#{sal,jdbcType=DECIMAL},#{comm,jdbcType=DECIMAL}
 		   )
 	</insert>

 </mapper>

测试结果
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值