其他分享
首页 > 其他分享> > Spring Boot整合Mybatis

Spring Boot整合Mybatis

作者:互联网

一.创建项目

将STS与eclipse集成,新建SpringBoot项目,勾选如下选项

 一路next,pom.xml文件会帮我们配置好.

但我们还需要引入thymeleaf模板引擎,需要我们手动向pom.xml文件中添加

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

数据源我们引用阿里的druid,需要我们向pom.xml文件中手动添加

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>druid</artifactId>
       <version>1.1.21</version>
</dependency>

完整的pom.xml文件

<?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.2.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</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.1.1</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>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
       <dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>druid</artifactId>
       <version>1.1.21</version>
</dependency>
        
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

接下来需要在application.properties 中指定数据库连接信息与数据源

spring.datasource.username=root
spring.datasource.password=20182022
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/my?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

二 .创建数据表

表结构:

相应的java bean

package com.example.demo.pojo;

public class Custom {
	private String sname;
	private String passwords;
	private Integer age;
	public Custom() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Custom(String sname, String passwords, Integer age) {
		super();
		this.sname = sname;
		this.passwords = passwords;
		this.age = age;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getPasswords() {
		return passwords;
	}
	public void setPasswords(String passwords) {
		this.passwords = passwords;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Custom [sname=" + sname + ", passwords=" + passwords + ", age=" + age + "]";
	}
	

}

 三 .书写mapper

在mappper包下新建CustomMapper.xml文件与CustomMapper.java接口

<?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.example.demo.mapper.CustomMapper">
  <insert id="insert" parameterType="com.example.demo.pojo.Custom">
   insert into custom(sname,passwords,age) values(#{sname},#{passwords},#{age})
  </insert>
</mapper>
import com.example.demo.pojo.Custom;

public interface CustomMapper {
	public void insert(Custom custom);

}

四.创建Service

在service包下新建CustomService.java接口与CustomServiceImpl.java实现类

package com.example.demo.mapper;

import com.example.demo.pojo.Custom;

public interface CustomMapper {
	public void insert(Custom custom);

}
package com.example.demo.service;

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

import com.example.demo.mapper.CustomMapper;
import com.example.demo.pojo.Custom;
@Service
public class CustomServiceImpl implements CustomService{
	@Autowired
	private CustomMapper customMapper;
	@Override
	public void insertCustom(Custom custom) {
		// TODO Auto-generated method stub
		customMapper.insert(custom);
		
	}

}

五.创建controller

四维空间@ 发布了51 篇原创文章 · 获赞 0 · 访问量 615 私信 关注

标签:passwords,Spring,boot,sname,Boot,spring,Mybatis,com,public
来源: https://blog.csdn.net/qq_42681787/article/details/104098833